summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-06-21 00:21:16 +0000
committerPaul Phillips <paulp@improving.org>2009-06-21 00:21:16 +0000
commit85a7be90dabecb162c3c865085e32fc42d19c411 (patch)
tree797924fcdd999e07ae88ba533cb4745687697492
parent8cf7228f8c1e11d67f4b7053b4ff2772e2bd79fc (diff)
downloadscala-85a7be90dabecb162c3c865085e32fc42d19c411.tar.gz
scala-85a7be90dabecb162c3c865085e32fc42d19c411.tar.bz2
scala-85a7be90dabecb162c3c865085e32fc42d19c411.zip
More equality shenanigans.
and the various Int types all use different hashCode algorithms. This means there is very broken behavior afoot among the types. I see no option but to make numeric equals significantly less tricky.
-rw-r--r--src/library/scala/BigDecimal.scala13
-rw-r--r--src/library/scala/BigInt.scala31
-rw-r--r--test/files/jvm/bigints.scala4
3 files changed, 9 insertions, 39 deletions
diff --git a/src/library/scala/BigDecimal.scala b/src/library/scala/BigDecimal.scala
index 27d1c19b26..b4d4732951 100644
--- a/src/library/scala/BigDecimal.scala
+++ b/src/library/scala/BigDecimal.scala
@@ -145,11 +145,6 @@ object BigDecimal
* @since 2.8
*/
implicit def bigInt2bigDecimal(x: BigInt): BigDecimal = apply(x)
-
- // Anyone can subclass Number, so we can't just assume .longValue is an unrounded
- // representation (as it cannot be for anything larger than Long.) So we also confirm
- // that at least x thinks it's equal to x.longValue.
- private[scala] def equalsOwnLongValue(that: Number): Boolean = that == that.longValue
}
/**
@@ -164,7 +159,6 @@ extends java.lang.Number
{
def this(bigDecimal: BigDec) = this(bigDecimal, BigDecimal.defaultMathContext)
import BigDecimal.RoundingMode._
- import BigDecimal.equalsOwnLongValue
/** Cuts way down on the wrapper noise. */
private implicit def bigdec2BigDecimal(x: BigDec): BigDecimal = new BigDecimal(x, mc)
@@ -173,16 +167,11 @@ extends java.lang.Number
override def hashCode(): Int = this.bigDecimal.hashCode()
/** Compares this BigDecimal with the specified value for equality.
+ * Will only claim equality with scala.BigDecimal and java.math.BigDecimal.
*/
override def equals (that: Any): Boolean = that match {
case that: BigDecimal => this equals that
case that: BigDec => this equals BigDecimal(that)
- case that: BigInt => this equals BigDecimal(that)
- case that: java.math.BigInteger => this equals BigDecimal(new BigInt(that), mc)
- case that: java.lang.Double => this equals BigDecimal(that.doubleValue)
- case that: java.lang.Float => this equals BigDecimal(that.floatValue)
- case that: java.lang.Number => equalsOwnLongValue(that) && (this equals BigDecimal(that.longValue))
- case that: java.lang.Character => this equals BigDecimal(that.charValue.asInstanceOf[Int])
case _ => false
}
diff --git a/src/library/scala/BigInt.scala b/src/library/scala/BigInt.scala
index abed8c748d..6ec02d0100 100644
--- a/src/library/scala/BigInt.scala
+++ b/src/library/scala/BigInt.scala
@@ -12,6 +12,7 @@
package scala
import java.math.BigInteger
+import java.{ lang => jl }
/**
* @author Martin Odersky
@@ -108,38 +109,22 @@ object BigInt {
* @version 1.0, 15/07/2003
*/
@serializable
-class BigInt(val bigInteger: BigInteger) extends java.lang.Number
+class BigInt(val bigInteger: BigInteger) extends jl.Number
{
- import BigDecimal.equalsOwnLongValue
-
/** Returns the hash code for this BigInt. */
override def hashCode(): Int = this.bigInteger.hashCode()
/** Compares this BigInt with the specified value for equality.
*/
- override def equals (that: Any): Boolean = that match {
+ override def equals(that: Any): Boolean = that match {
case that: BigInt => this equals that
case that: BigInteger => this equals new BigInt(that)
- case that: BigDecimal => this equals that
- case that: java.math.BigDecimal => this equals BigDecimal(that)
- case that: java.lang.Double => this equals BigDecimal(that.doubleValue)
- case that: java.lang.Float => this equals BigDecimal(that.floatValue)
- case that: java.lang.Number => equalsOwnLongValue(that) && (this equals BigInt(that.longValue))
- case that: java.lang.Character => this equals BigInt(that.charValue.asInstanceOf[Int])
case _ => false
}
/** Compares this BigInt with the specified BigInt for equality.
*/
- def equals (that: BigInt): Boolean =
- this.bigInteger.compareTo(that.bigInteger) == 0
-
- /** Compares this BigInt with the specified BigDecimal for equality.
- */
- def equals(that: BigDecimal): Boolean = that.toBigIntExact match {
- case None => false
- case Some(x) => this equals x
- }
+ def equals (that: BigInt): Boolean = compare(that) == 0
/** Compares this BigInt with the specified BigInt
*/
@@ -147,19 +132,19 @@ class BigInt(val bigInteger: BigInteger) extends java.lang.Number
/** Less-than-or-equals comparison of BigInts
*/
- def <= (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) <= 0
+ def <= (that: BigInt): Boolean = compare(that) <= 0
/** Greater-than-or-equals comparison of BigInts
*/
- def >= (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) >= 0
+ def >= (that: BigInt): Boolean = compare(that) >= 0
/** Less-than of BigInts
*/
- def < (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) < 0
+ def < (that: BigInt): Boolean = compare(that) < 0
/** Greater-than comparison of BigInts
*/
- def > (that: BigInt): Boolean = this.bigInteger.compareTo(that.bigInteger) > 0
+ def > (that: BigInt): Boolean = compare(that) > 0
/** Addition of BigInts
*/
diff --git a/test/files/jvm/bigints.scala b/test/files/jvm/bigints.scala
index efb5139180..4fae613ad5 100644
--- a/test/files/jvm/bigints.scala
+++ b/test/files/jvm/bigints.scala
@@ -31,8 +31,6 @@ object Test_BigInt extends TestCase("BigInt") with Assert {
assertEquals("int_mul_bigint", 2*x*y, y*x*2)
assertTrue("z_<=_3", z <= 3)
assertFalse("3_<_z", 3 < z)
- assertTrue("z_==_3", z == 3)
- assertTrue("3_==_z", 3 == z)
}
}
@@ -52,8 +50,6 @@ object Test_BigDecimal extends TestCase("BigDecimal") with Assert {
val z = 1 + y
assertTrue("z_<=_3", z <= 3)
assertFalse("3_<_z", 3 < z)
- assertTrue("z_==_3", z == 3)
- assertTrue("3_==_z", 3 == z)
val a: BigDecimal= Math.MAX_LONG
val b: BigDecimal = 1