diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/math/BigDecimal.scala | 13 | ||||
-rw-r--r-- | src/library/scala/math/BigInt.scala | 13 |
2 files changed, 19 insertions, 7 deletions
diff --git a/src/library/scala/math/BigDecimal.scala b/src/library/scala/math/BigDecimal.scala index 16fdf0897e..a4056c173c 100644 --- a/src/library/scala/math/BigDecimal.scala +++ b/src/library/scala/math/BigDecimal.scala @@ -183,9 +183,18 @@ extends ScalaNumber with ScalaNumericConversions with Serializable { case that: BigDecimal => this equals that case that: BigInt => this.toBigIntExact exists (that equals _) case _: Float | _: Double => unifiedPrimitiveEquals(that) - case _ => fitsInLong && unifiedPrimitiveEquals(that) + case _ => isValidLong && unifiedPrimitiveEquals(that) + } + override def isValidByte = noArithmeticException(toByteExact) + override def isValidShort = noArithmeticException(toShortExact) + override def isValidChar = isValidInt && toIntExact >= Char.MinValue && toIntExact <= Char.MaxValue + override def isValidInt = noArithmeticException(toIntExact) + def isValidLong = noArithmeticException(toLongExact) + + private def noArithmeticException(body: => Unit): Boolean = { + try { body ; true } + catch { case _: ArithmeticException => false } } - private def fitsInLong = isWhole && this <= Long.MaxValue && this >= Long.MinValue protected[math] def isWhole = (this remainder 1) == BigDecimal(0) def underlying = bigDecimal diff --git a/src/library/scala/math/BigInt.scala b/src/library/scala/math/BigInt.scala index caffbe6133..5b6fc6ffd5 100644 --- a/src/library/scala/math/BigInt.scala +++ b/src/library/scala/math/BigInt.scala @@ -113,11 +113,10 @@ object BigInt { * @author Martin Odersky * @version 1.0, 15/07/2003 */ -class BigInt(val bigInteger: BigInteger) extends ScalaNumber with ScalaNumericConversions with Serializable -{ +class BigInt(val bigInteger: BigInteger) extends ScalaNumber with ScalaNumericConversions with Serializable { /** Returns the hash code for this BigInt. */ override def hashCode(): Int = - if (fitsInLong) unifiedPrimitiveHashcode + if (isValidLong) unifiedPrimitiveHashcode else bigInteger.## /** Compares this BigInt with the specified value for equality. @@ -125,9 +124,13 @@ class BigInt(val bigInteger: BigInteger) extends ScalaNumber with ScalaNumericCo override def equals(that: Any): Boolean = that match { case that: BigInt => this equals that case that: BigDecimal => that.toBigIntExact exists (this equals _) - case x => fitsInLong && unifiedPrimitiveEquals(x) + case x => isValidLong && unifiedPrimitiveEquals(x) } - private def fitsInLong = this >= Long.MinValue && this <= Long.MaxValue + override def isValidByte = this >= Byte.MinValue && this <= Byte.MaxValue + override def isValidShort = this >= Short.MinValue && this <= Short.MaxValue + override def isValidChar = this >= Char.MinValue && this <= Char.MaxValue + override def isValidInt = this >= Int.MinValue && this <= Int.MaxValue + def isValidLong = this >= Long.MinValue && this <= Long.MaxValue protected[math] def isWhole = true def underlying = bigInteger |