File tree Expand file tree Collapse file tree 1 file changed +11
-14
lines changed
Expand file tree Collapse file tree 1 file changed +11
-14
lines changed Original file line number Diff line number Diff line change 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 )))))
You can’t perform that action at this time.
0 commit comments