Skip to content

Commit 0762fac

Browse files
committed
Clean up
1 parent 73620ea commit 0762fac

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/same/platform.cljc

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,30 @@
7474
(Math/ulp (double f)))
7575
:cljs (ulp* f)))
7676

77-
(defn abs-val
78-
[v]
79-
(cond
80-
(= v Long/MIN_VALUE) (- (bigint v))
81-
(neg? v) (- v)
82-
:else v))
77+
(defn abs-diff
78+
[v1 v2]
79+
(let [d #?(:clj (- (bigint v1) (bigint v2))
80+
:cljs (- v1 v2))]
81+
(if (neg? d) (- d) d)))
8382

8483
(defn bit-diff-double
8584
"Difference between two doubles in ULPs (i.e. number of representable numbers between them + 1)."
8685
[f1 f2]
87-
#?(:clj (abs-val (- (bigint (Double/doubleToLongBits f1))
88-
(bigint (Double/doubleToLongBits f2))))
86+
#?(:clj (abs-diff (Double/doubleToLongBits f1)
87+
(Double/doubleToLongBits f2))
8988
:cljs (let [buf (js/ArrayBuffer. 16)
9089
dv (js/DataView. buf)]
9190
(.setFloat64 dv 0 (double f1))
9291
(.setFloat64 dv 8 (double f2))
93-
(Math/abs
94-
(+ (* (- (.getUint32 dv 0) (.getUint32 dv 8)) 0x100000000)
95-
(- (.getUint32 dv 4) (.getUint32 dv 12)))))))
92+
(abs-diff (.getBigUint64 dv 0) (.getBigUint64 dv 8)))))
9693

9794
(defn bit-diff-float
9895
"Difference between two floats in ULPs (i.e. number of representable numbers between them + 1)."
9996
[f1 f2]
100-
#?(:clj (abs-val (- (Float/floatToIntBits f1)
101-
(Float/floatToIntBits f2)))
97+
#?(:clj (abs-diff (Float/floatToIntBits f1)
98+
(Float/floatToIntBits f2))
10299
:cljs (let [buf (js/ArrayBuffer. 8)
103100
dv (js/DataView. buf)]
104101
(.setFloat32 dv 0 (float f1))
105102
(.setFloat32 dv 4 (float f2))
106-
(Math/abs (- (.getUint32 dv 0) (.getUint32 dv 4))))))
103+
(abs-diff (.getUint32 dv 0) (.getUint32 dv 4)))))

0 commit comments

Comments
 (0)