Power function

208 views
Skip to first unread message

Jon

unread,
Jun 3, 2008, 4:55:15 AM6/3/08
to Clojure
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. ;-)

/Jon

Phil Jordan

unread,
Jun 3, 2008, 5:33:32 AM6/3/08
to clo...@googlegroups.com

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

Jon

unread,
Jun 3, 2008, 6:42:53 AM6/3/08
to Clojure
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

Phil Jordan

unread,
Jun 3, 2008, 8:29:57 AM6/3/08
to clo...@googlegroups.com
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

Stuart Sierra

unread,
Jun 3, 2008, 10:16:11 PM6/3/08
to Clojure
On Jun 3, 8:29 am, Phil Jordan <li...@philjordan.eu> wrote:
> 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

That would be cool. My favorite trick to show off Common Lisp is
(expt 2 128) => 340282366920938463463374607431768211456
-Stuart

Karl Brodowsky

unread,
Feb 1, 2016, 10:07:04 PM2/1/16
to Clojure, li...@philjordan.eu
I think that since it is agreed in many circumstances that 0^0 is 1, I would make that the default...

James Reeves

unread,
Feb 1, 2016, 10:25:11 PM2/1/16
to clo...@googlegroups.com, li...@philjordan.eu
What power function do you mean?

- James

On 2 February 2016 at 03:02, Karl Brodowsky <b...@gmx.net> wrote:
I think that since it is agreed in many circumstances that 0^0 is 1, I would make that the default...

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mikera

unread,
Feb 2, 2016, 3:24:34 AM2/2/16
to Clojure, jon.k...@usit.uio.no
There is a `pow` function in core.matrix, that computes powers over whole arrays at the same time.

Tests / patches to improve this always welcome.

Jon

unread,
Feb 2, 2016, 3:54:22 AM2/2/16
to Clojure, jon.k...@usit.uio.no
Thanks, all of you! I had totally forgotten about this topic. After all, it's almost 8 years since I posted my question to this group. ;-)

Mars0i

unread,
Feb 3, 2016, 9:04:38 AM2/3/16
to Clojure, jon.k...@usit.uio.no
https://github.com/clojure/math.numeric-tower:
  • (expt x y) - x to the yth power


Reply all
Reply to author
Forward
0 new messages