iriscouch / bigdecimal.js

Arbitrary-precision Javascript BigInteger and BigDecimal real numbers
Apache License 2.0
243 stars 25 forks source link

1 / new BigInteger("-1").compareTo(new BigInteger("-1")) === -Infinity #16

Open Yaffle opened 10 years ago

Yaffle commented 10 years ago

negative zero is a result of "compareTo"

gmelika commented 9 years ago

I'm getting a zero

var bd = require('bigdecimal') undefined new bd.BigInteger("-1").compareTo(new bd.BigInteger("-1")) 0

thatguy commented 9 years ago

gmelika, even though it displays as "0", it is apparently actually a -0. You will see this if you divide one by that value as Yaffle has done. Here is a JSFiddle: http://jsfiddle.net/ue0mru55/

It basically shows that

new BigInteger("-1").compareTo(new BigInteger("-1")) => 0

and

1 / new BigInteger("-1").compareTo(new BigInteger("-1")) => -Infinity

Would it be possible and reasonable to wrap the result of compareTo() in Math.abs()?

Yaffle, just for my reference, does it matter if compareTo() returns -0?

Yaffle commented 9 years ago

@thatguy ,

Yaffle, just for my reference, does it matter if compareTo() returns -0?

Possibly, no. Although, if we want to have some standart...