Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashCode of BigDecimal and Double do not match in 2.11 #8970

Closed
scabug opened this issue Nov 9, 2014 · 4 comments
Closed

hashCode of BigDecimal and Double do not match in 2.11 #8970

scabug opened this issue Nov 9, 2014 · 4 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Nov 9, 2014

The docs of BigDecimal say the following:

"Within the range of IEEE binary128 numbers, BigDecimal will agree with BigInt for both equality and hash codes (and will agree with primitive types as well)."

In some cases in Scala 2.11 this does not appear to be the case. The following example shows this regressing from 2.10 to 2.11:

2.10

scala> BigDecimal(123.312).hashCode()
res0: Int  -1903704720

scala> (123.312).##
res1: Int = -1903704720

2.11

scala> BigDecimal(123.312).hashCode()
res0: Int = 342240935

scala> (123.312).##
res1: Int = -1903704720

It might be that this was simply dropped as a feature of BigDecimal. That would break some applications and it would be good to update the docs at least with a warning.

@scabug
Copy link
Author

scabug commented Nov 9, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8970?orig=1
Reporter: Patrick Wendell (pwendell)
Affected Versions: 2.11.4

@scabug
Copy link
Author

scabug commented Nov 10, 2014

@retronym said:
@Ichoran This changed in your BigDecimal consistency patch, scala/scala@29541ce3

% scala-hash 29541ce3~1 -nc -e 'println(BigDecimal(123.312).hashCode())'
-1903704720
% scala-hash 29541ce3 -nc -e 'println(BigDecimal(123.312).hashCode())'
342240935

Intentional? Unavoidable?

@scabug
Copy link
Author

scabug commented Nov 10, 2014

@Ichoran said:
Ugh. I'd forgotten about hash codes. Floating-point is such a pain.

scala> new java.math.BigDecimal(123.312)
res0: java.math.BigDecimal = 123.3119999999999976125764078460633754730224609375

So, that's not actually 123.312, is it? But my patch tried to make BigDecimal(123.312) be exactly 123.312 (i.e. the decimal fractional value, not the Double approximation), since there were a lot of consistency gotchas around BigDecimal/Double interconversion.

I'll see if it's practical to fix isValidDouble or hashCode to agree with the string representation of the Double, not the actual value. I think I just forgot to switch this out for equivalentToDouble.

@scabug
Copy link
Author

scabug commented Nov 21, 2014

@Ichoran said:
Just needed isDecimalDouble instead of isValidDouble. Junit test added.

scala/scala#4141

@scabug scabug closed this as completed Dec 4, 2014
@scabug scabug added the has PR label Apr 7, 2017
@scabug scabug added this to the 2.11.5 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants