diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2012-09-08 18:43:22 +0200 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2012-09-08 18:52:51 +0200 |
commit | 815f60ff9c50d22a23fbc9d980570fb6941a7d71 (patch) | |
tree | a313adaf9493d35eecb858c1d6bcdba6e272979f /src | |
parent | c619f94a9cfbddc12c9c5df3affb4636f8982a0a (diff) | |
download | scala-815f60ff9c50d22a23fbc9d980570fb6941a7d71.tar.gz scala-815f60ff9c50d22a23fbc9d980570fb6941a7d71.tar.bz2 scala-815f60ff9c50d22a23fbc9d980570fb6941a7d71.zip |
Refine equality of Constant types over floating point values.
The constant types for 0d and -0d should not be equal.
This is implemented by checking equality of the result of
doubleToRawLongBits / floatToRawIntBits, which also correctly
considers two NaNs of the same flavour to be equal.
Followup to SI-6331.
Diffstat (limited to 'src')
-rw-r--r-- | src/reflect/scala/reflect/internal/Constants.scala | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/reflect/scala/reflect/internal/Constants.scala b/src/reflect/scala/reflect/internal/Constants.scala index e5a543da46..4d512e3864 100644 --- a/src/reflect/scala/reflect/internal/Constants.scala +++ b/src/reflect/scala/reflect/internal/Constants.scala @@ -83,8 +83,16 @@ trait Constants extends api.Constants { */ override def equals(other: Any): Boolean = other match { case that: Constant => - this.tag == that.tag && - (this.value == that.value || this.isNaN && that.isNaN) + // Consider two NaNs to be identical, despite non-equality + // Consider -0d to be distinct from 0d, despite equality + import java.lang.Double.doubleToRawLongBits + import java.lang.Float.floatToRawIntBits + + this.tag == that.tag && ((value, that.value) match { + case (f1: Float, f2: Float) => floatToRawIntBits(f1) == floatToRawIntBits(f2) + case (d1: Double, d2: Double) => doubleToRawLongBits(d1) == doubleToRawLongBits(d2) + case (v1, v2) => v1 == v2 + }) case _ => false } |