Why does the following Clojure code print "true"?
(please notice that the numbers differ in the last digit)
(== (Math/sqrt 10252519345963644753026N)
(Math/sqrt 10252519345963644753025N))
Not sure whether this question is only about Clojure or if it also applies to other languages (Java's BigInteger?).
Printing them results in:
(str (Math/sqrt 10252519345963644753026N) " "
(Math/sqrt 10252519345963644753025N))
1.01254725055E11 1.01254725055E11
The Java Math.sqrt
function
double
argument anddouble
result.As various comments suggest, the conversions that this induces lose the precision to distinguish between the numbers. In fact, conversion to double
does so:
(= (double 10252519345963644753026N)
(double 10252519345963644753025N))
;true
A double
has a 53 bit precision. Since 10 bits is about 3 decimal digits, this is about 16 decimal digits precision. Your numbers are 23 digits long, so the last few digits are lost in conversion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With