use-cases for BigInt versus BigInteger in Clojure

后端 未结 1 1003
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 16:52

I\'m looking for guidance on when to use Clojure BigInt versus Java BigInteger in Clojure. Both work just fine, and I am assuming that the main reason to use BigInt is to ta

相关标签:
1条回答
  • 2020-12-16 17:27

    In "Clojure Programming" by C. Emerick et. al., p.428, there is a sidebar topic, "Why Does Clojure Have Its Own BigInt Class When Java Already Provides One in BigInteger?"

    They note two reasons to prefer BigInt to Java's BigInteger. First, the latter's .hashCode implementation is inconsistent with that of Long (the same number expressed in each type gives a different hash value). This is generally not what you want when comparing equivalent values in e.g. hash maps.

    The other reason is that BigInts are optimized to use primitive types when possible, so performance should be better for many cases.

    I would use Clojure's numeric types unless you have a good reason not to (your use of .isProbablePrime suggests you might have a good enough reason).

    0 讨论(0)
提交回复
热议问题