osdir.com
mailing list archive F.A.Q. -since 2001!



Subject: Re: Power function - msg#00039

List: java.clojure.user

Mail Archive Navigation:
by Date: Prev Next Date Index by Thread: Prev Next Thread Index


On Jun 3, 11:33 am, Phil Jordan <li...@xxxxxxxxxxxxx> wrote:
> Jon wrote:
> > As I could not find a ** function in boot.clj, I wrote this one:
>
> > (defn **
> >    "nth power of x"
> >    [x n]
> >    (if (== n 0)
> >            1
> >            (* (if (bit-test n 0) x 1)
> >                    (** (* x x) (bit-shift-right n 1)) ) ) )
>
> > In my opinion, it has one weakness: It loops forever when n<0, like in
> > (** 2 -3). The reason is that (bit-shift-right -1 1) returns -1.
> > If anybody knows a faster/better Clojure power function, let me
> > know. ;-)
>
> Well, Java has functions for powers. In the case of integers:
>
> => (. (. java.math.BigInteger (valueOf 2)) (pow 5))
> 32
>
> or doubles:
>
> => (. java.lang.Math (pow 2 -3))
> 0.125
>
> These should be fairly easy to wrap into something more idiomatic.
>
> ~phil

Thanks Phil. Your BigInteger solution seems to be a bit (5%) faster
than my "all Clojure" solution. You've taught me something about Java
that I didn't know too well. ;-)

/Jon


Thread at a glance:

Previous Message by Date:

Re: Power function

Jon wrote: > As I could not find a ** function in boot.clj, I wrote this one: > > (defn ** > "nth power of x" > [x n] > (if (== n 0) > 1 > (* (if (bit-test n 0) x 1) > (** (* x x) (bit-shift-right n 1)) ) ) ) > > In my opinion, it has one weakness: It loops forever when n<0, like in > (** 2 -3). The reason is that (bit-shift-right -1 1) returns -1. > If anybody knows a faster/better Clojure power function, let me > know. ;-) Well, Java has functions for powers. In the case of integers: => (. (. java.math.BigInteger (valueOf 2)) (pow 5)) 32 or doubles: => (. java.lang.Math (pow 2 -3)) 0.125 These should be fairly easy to wrap into something more idiomatic. ~phil

Next Message by Date:

Re: Power function

Jon wrote: > Thanks Phil. Your BigInteger solution seems to be a bit (5%) faster > than my "all Clojure" solution. You've taught me something about Java > that I didn't know too well. ;-) After thinking about it a little more, I think it's probably worth coming up with a catch-all solution to this that can go into Clojure itself (or, if Rich doesn't want it in Clojure, then in contrib) as neither of those functions handle rational numbers, and it's not as obvious as it first seems, as: (power 0 0) -> undefined (power 0 any) -> 0 (power any 0) -> 1 (power 1 any) -> 1 (power integer +integer) -> integer (power integer -integer) -> rational (power rational +-integer) -> rational (power any rational) -> real (power real any) -> real (power any real) -> real Obviously, integers can be int, long, BigInteger, etc., real can be float and double. I haven't done much with numerics in Clojure, so I don't know the implementation details. I'll have to look into how the other mathematical operations handle type promotion/demotion. I was very impressed when I discovered: => (class (* 5/4 4/5)) class java.lang.Integer :) ~phil

Previous Message by Thread:

Re: Power function

Jon wrote: > As I could not find a ** function in boot.clj, I wrote this one: > > (defn ** > "nth power of x" > [x n] > (if (== n 0) > 1 > (* (if (bit-test n 0) x 1) > (** (* x x) (bit-shift-right n 1)) ) ) ) > > In my opinion, it has one weakness: It loops forever when n<0, like in > (** 2 -3). The reason is that (bit-shift-right -1 1) returns -1. > If anybody knows a faster/better Clojure power function, let me > know. ;-) Well, Java has functions for powers. In the case of integers: => (. (. java.math.BigInteger (valueOf 2)) (pow 5)) 32 or doubles: => (. java.lang.Math (pow 2 -3)) 0.125 These should be fairly easy to wrap into something more idiomatic. ~phil

Next Message by Thread:

Re: Power function

Jon wrote: > Thanks Phil. Your BigInteger solution seems to be a bit (5%) faster > than my "all Clojure" solution. You've taught me something about Java > that I didn't know too well. ;-) After thinking about it a little more, I think it's probably worth coming up with a catch-all solution to this that can go into Clojure itself (or, if Rich doesn't want it in Clojure, then in contrib) as neither of those functions handle rational numbers, and it's not as obvious as it first seems, as: (power 0 0) -> undefined (power 0 any) -> 0 (power any 0) -> 1 (power 1 any) -> 1 (power integer +integer) -> integer (power integer -integer) -> rational (power rational +-integer) -> rational (power any rational) -> real (power real any) -> real (power any real) -> real Obviously, integers can be int, long, BigInteger, etc., real can be float and double. I haven't done much with numerics in Clojure, so I don't know the implementation details. I'll have to look into how the other mathematical operations handle type promotion/demotion. I was very impressed when I discovered: => (class (* 5/4 4/5)) class java.lang.Integer :) ~phil
blog comments powered by Disqus

Home | News | Sitemap | FAQ | advertise | OSDir is an Inevitable website. GBiz is too!