Skip to content

Commit 53a57b6

Browse files
committed
* add hash-long
* add hash-double * reported test case
1 parent 22e3dc4 commit 53a57b6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/main/cljs/cljs/core.cljs

+13-1
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,16 @@
960960
h1 (m3-mix-H1 m3-seed k1)]
961961
(m3-fmix h1 4))))
962962

963+
(defn hash-long [high low]
964+
(bit-xor high low))
965+
966+
(defn hash-double [f]
967+
(let [arr (doto (js/Float64Array. 1) (aset 0 f))
968+
buf (.-buffer arr)
969+
low (.getInt32 (js/DataView. buf 0 4))
970+
high (.getInt32 (js/DataView. buf 4 4))]
971+
(hash-long high low)))
972+
963973
(defn ^number m3-hash-unencoded-chars [in]
964974
(let [h1 (loop [i 1 h1 m3-seed]
965975
(if (< i (.-length in))
@@ -1021,7 +1031,9 @@
10211031

10221032
(number? o)
10231033
(if ^boolean (js/isFinite o)
1024-
(js-mod (Math/floor o) 2147483647)
1034+
(if-not (.isInteger js/Number o)
1035+
(hash-double o)
1036+
(js-mod (Math/floor o) 2147483647))
10251037
(case o
10261038
##Inf
10271039
2146435072

src/test/cljs/cljs/hashing_test.cljs

+5
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,8 @@
9393
(deftest test-cljs-1818
9494
(is (= (hash true) 1231))
9595
(is (= (hash false) 1237)))
96+
97+
(deftest test-cljs-3410
98+
(testing "Small floats should not hash the same"
99+
(is (not= (hash-double -0.32553251) (hash-double -0.0000032553251)))
100+
(is (not= (hash -0.32553251) (hash -0.0000032553251)))))

0 commit comments

Comments
 (0)