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
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
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.