Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added type consistency for (expt _ 0)

(expt 3M 0) should return 1M
(expt 3N 0) should return 1N
  • Loading branch information...
commit 46a91e50a1b8992300106f925d7474b9366e77f5 1 parent 04dfa56
@Engelberg Engelberg authored
View
13 README.md
@@ -32,7 +32,7 @@ More documentation in docstrings.
Releases and Dependency Information
========================================
-Latest stable release: 0.0.3
+Latest stable release: 0.0.4
* [All Released Versions](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22math.numeric-tower%22)
@@ -41,7 +41,7 @@ Latest stable release: 0.0.3
[Leiningen](https://github.com/technomancy/leiningen) dependency information:
```clojure
-[org.clojure/math.numeric-tower "0.0.3"]
+[org.clojure/math.numeric-tower "0.0.4"]
```
[Maven](http://maven.apache.org/) dependency information:
@@ -50,7 +50,7 @@ Latest stable release: 0.0.3
<dependency>
<groupId>org.clojure</groupId>
<artifactId>math.numeric-tower</artifactId>
- <version>0.0.2</version>
+ <version>0.0.4</version>
</dependency>
```
@@ -102,9 +102,14 @@ Developer Information
Changelog
========================================
+* Release 0.0.4 on 2014-01-16
+ * Adjust return type of expt to match base when power is 0.
+ * (expt 3M 0) -> 1M
+ * (expt 3N 0) -> 1N
+
* Release 0.0.3 on 2013-12-29
* Minor improvement to sqrt of ratio.
-
+
* Release 0.0.2 on 2012-11-23
* Added type hints to remove some reflective calls.
View
23 src/main/clojure/clojure/math/numeric_tower.clj
@@ -60,6 +60,14 @@ exact-integer-sqrt - Implements a math function from the R6RS Scheme
(def ^{:private true} plus (first [+' +]))
(def ^{:private true} dec* (first [dec' dec]))
(def ^{:private true} inc* (first [inc' inc]))
+
+;; feature testing macro, based on suggestion from Chas Emerick:
+(defmacro when-available
+ [sym & body]
+ (try
+ (when (resolve sym)
+ (list* 'do body))
+ (catch ClassNotFoundException _#)))
(defn- expt-int [base pow]
(loop [n pow, y (num 1), z base]
@@ -76,7 +84,12 @@ Returns an exact number if the base is an exact number and the power is an integ
(if (and (not (float? base)) (integer? pow))
(cond
(pos? pow) (expt-int base pow)
- (zero? pow) 1
+ (zero? pow) (cond
+ (= (type base) BigDecimal) 1M
+ (= (type base) java.math.BigInteger) (java.math.BigInteger. "1")
+ (when-available clojure.lang.BigInt (= (type base) clojure.lang.BigInt))
+ (when-available clojure.lang.BigInt (bigint 1))
+ :else 1)
:else (/ 1 (expt-int base (minus pow))))
(Math/pow base pow)))
@@ -101,14 +114,6 @@ round always returns an integer. Rounds up for values exactly in between two in
(declare sqrt-ratio)
(declare sqrt-decimal)
-;; feature testing macro, based on suggestion from Chas Emerick:
-(defmacro when-available
- [sym & body]
- (try
- (when (resolve sym)
- (list* 'do body))
- (catch ClassNotFoundException _#)))
-
(extend-type
Integer MathFunctions
(floor [n] n)
View
10 src/test/clojure/clojure/math/test_numeric_tower.clj
@@ -10,7 +10,15 @@
(expt 2 -10) 1/1024
(expt 0.5M 2) 0.25M
(expt 5 4.2) (Math/pow 5 4.2)
- (expt 5.3 4) (Math/pow 5.3 4)))
+ (expt 5.3 4) (Math/pow 5.3 4)
+ (expt 5.3 4) (Math/pow 5.3 4)
+ (expt 2 0) 1
+ (when-available clojure.lang.BigInt (expt 4N 0))
+ (when-available clojure.lang.BigInt 1N)
+ (expt (java.math.BigInteger. "4") 0) (java.math.BigInteger. "1")
+ (expt 4M 0) 1M
+ (expt 8M 1) 8M
+ (expt 16M 16) 18446744073709551616M))
(deftest test-abs
(are [x y] (= x y)
Please sign in to comment.
Something went wrong with that request. Please try again.