diff options
author | Paul Phillips <paulp@improving.org> | 2009-06-21 00:21:16 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-06-21 00:21:16 +0000 |
commit | 85a7be90dabecb162c3c865085e32fc42d19c411 (patch) | |
tree | 797924fcdd999e07ae88ba533cb4745687697492 /src | |
parent | 8cf7228f8c1e11d67f4b7053b4ff2772e2bd79fc (diff) | |
download | scala-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.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/BigDecimal.scala | 13 | ||||
-rw-r--r-- | src/library/scala/BigInt.scala | 31 |
2 files changed, 9 insertions, 35 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 */ |