How can I do exponentiation in clojure? For now I'm only needing integer exponentiation, but the question goes for fractions too.
相关问题
- Better Sequence Duplicate Remover
- Installation of Leiningen 2.X in Mac OS X
- Questions about Lists and other stuff in Clojure
- How do I add CORS to a compojure-api app?
- How do I use Clojure in Android Studio using Gracl
相关文章
- Factor Clojure code setting many different fields
- Does learning one Lisp help in learning the other?
- Better way to nest if-let in clojure
- Idiomatic approach for structuring Clojure source
- Is a “transparent” macrolet possible?
- Detect operating system in Clojure
- Using quote in Clojure
- Raise 10 to a power in javascript, are there bette
You can use java's
Math.pow
orBigInteger.pow
methods:SICP inspired full iterative fast version of 'sneaky' implementation above.
A simple one-liner using reduce
classic recursion (watch this, it blows stack)
tail recursion
functional
sneaky (also blows stack, but not so easily)
library
Clojure has a power function that works well: I'd recommend using this rather than going via Java interop since it handles all the Clojure arbitrary-precision number types correctly.
It's called
expt
for exponentiation rather thanpower
orpow
which maybe explains why it's a bit hard to find..... anyway here's a small example: