ClojureScript 浮点数散列为整数

ClojureScript floats hashed as ints

起初我认为这是一个错误,但查看源代码显然是故意的。有人知道为什么这样做吗?它与 Clojure 不一致并且是错误的微妙来源。

(hash 1)   ; => 1
(hash 1.5) ; => 1

https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L985

(defn hash
  "Returns the hash code of its argument. Note this is the hash code
   consistent with =."
  [o]
  (cond
    (implements? IHash o)
    (bit-xor (-hash ^not-native o) 0)

    (number? o)
    (if (js/isFinite o)
      (js-mod (Math/floor o) 2147483647)
      (case o
        Infinity
        2146435072
        -Infinity
        -1048576
        2146959360))
  ...))

JavaScript 只有一种数字类型:64-bit float between -(2^53)-1 and (2^53)-1. However, bitwise operations work on 32-bit signed integers。因此,当将浮点数转换为使用按位运算符的散列时,需要进行有损转换。 core.cljs/hash 中模运算的幻数 2147483647 是通过 32 位有符号数表示的最大整数。请注意,值 Infinity-Infinity.

也有特殊处理