Skip to content

Commit 13285a6

Browse files
committed
Improve tests and fix error with recent Java
When moving to Java 17 some of the tests started failing. It turns out it was because `Math/abs` was being used instead of `abs` in the Clojure branch of `same.platform/bit-diff-double` and `bit-diff-float`. For large differences this would overflow, so `(bit-diff-double 1.0 2.0)` would return 0 instead of a large number.
1 parent ae878a3 commit 13285a6

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/same/platform.cljc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
(defn bit-diff-double
7878
"Difference between two doubles in ULPs (i.e. number of representable numbers between them + 1)."
7979
[f1 f2]
80-
#?(:clj (Math/abs ^long (- (Double/doubleToLongBits f1)
81-
(Double/doubleToLongBits f2)))
80+
#?(:clj (abs (- (Double/doubleToLongBits f1)
81+
(Double/doubleToLongBits f2)))
8282
:cljs (let [buf (js/ArrayBuffer. 16)
8383
dv (js/DataView. buf)]
8484
(.setFloat64 dv 0 (double f1))
@@ -90,8 +90,8 @@
9090
(defn bit-diff-float
9191
"Difference between two floats in ULPs (i.e. number of representable numbers between them + 1)."
9292
[f1 f2]
93-
#?(:clj (Math/abs ^long (- (Float/floatToIntBits f1)
94-
(Float/floatToIntBits f2)))
93+
#?(:clj (abs ^long (- (Float/floatToIntBits f1)
94+
(Float/floatToIntBits f2)))
9595
:cljs (let [buf (js/ArrayBuffer. 8)
9696
dv (js/DataView. buf)]
9797
(.setFloat32 dv 0 (float f1))

test/same/platform_test.cljc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,23 @@
4545
(is (p/nan? (p/ulp nan))))
4646

4747
(deftest bit-diff-double-test
48+
(is (zero? (p/bit-diff-double 1.0 1.0)))
4849
(is (= 1 (p/bit-diff-double 1.0 1.0000000000000002)))
49-
(is (= 1 (p/bit-diff-double 1.0000000000000002 1.0))))
50+
(is (= 1 (p/bit-diff-double 1.0000000000000002 1.0)))
51+
(is (= 0x8000000000000 (p/bit-diff-double 1.0 1.5)))
52+
(is (= 0x8000000000000 (p/bit-diff-double 1.5 1.0)))
53+
(is (= 0x10000000000000 (p/bit-diff-double 1.0 2.0)))
54+
(is (= 0x10000000000000 (p/bit-diff-double 2.0 1.0)))
55+
(is (= 0x100000000000000 (p/bit-diff-double 1.0 65536.0)))
56+
(is (= 0x100000000000000 (p/bit-diff-double 65536.0 1.0))))
57+
58+
(deftest bit-diff-float-test
59+
(is (zero? (p/bit-diff-float 1.0 1.0)))
60+
(is (= 1 (p/bit-diff-float 1.0 1.0000001)))
61+
(is (= 1 (p/bit-diff-float 1.0000001 1.0)))
62+
(is (= 0x400000 (p/bit-diff-float 1.0 1.5)))
63+
(is (= 0x400000 (p/bit-diff-float 1.5 1.0)))
64+
(is (= 0x800000 (p/bit-diff-float 1.0 2.0)))
65+
(is (= 0x800000 (p/bit-diff-float 2.0 1.0)))
66+
(is (= 0x8000000 (p/bit-diff-float 1.0 65536.0)))
67+
(is (= 0x8000000 (p/bit-diff-float 65536.0 1.0))))

0 commit comments

Comments
 (0)