diff options
author | Paul Phillips <paulp@improving.org> | 2011-05-05 19:04:33 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-05-05 19:04:33 +0000 |
commit | d70e69e8a8a736e2a7f55e2abf651507b0c542a7 (patch) | |
tree | 666fd4fff53adf760db380a5deb7b6ffaddea759 | |
parent | 5e499c5e4346c0951a3841ce73ccd990d47597cd (diff) | |
download | scala-d70e69e8a8a736e2a7f55e2abf651507b0c542a7.tar.gz scala-d70e69e8a8a736e2a7f55e2abf651507b0c542a7.tar.bz2 scala-d70e69e8a8a736e2a7f55e2abf651507b0c542a7.zip |
Makes BigInt's isValidThing methods make some k...
Makes BigInt's isValidThing methods make some kind of sense. I wish I
hadn't written so much code for the numerical classes which languishes
in git tributaries. Closes #4540, no review.
-rw-r--r-- | src/library/scala/math/BigDecimal.scala | 13 | ||||
-rw-r--r-- | src/library/scala/math/BigInt.scala | 13 | ||||
-rw-r--r-- | test/files/run/is-valid-num.scala | 20 |
3 files changed, 39 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 diff --git a/test/files/run/is-valid-num.scala b/test/files/run/is-valid-num.scala new file mode 100644 index 0000000000..6e423e946e --- /dev/null +++ b/test/files/run/is-valid-num.scala @@ -0,0 +1,20 @@ +object Test { + def x = BigInt("10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") + def y = BigDecimal("" + (Short.MaxValue + 1) + ".0") + + def l1 = Int.MaxValue.toLong + 1 + def l2 = Int.MinValue.toLong - 1 + + def main(args: Array[String]): Unit = { + assert(!x.isValidLong, x) + assert(!x.isValidInt, x) + assert(!x.isValidChar, x) + assert(!x.isValidByte, x) + assert(!y.isValidShort, y) + assert(y.isValidChar, y) + assert(y.isValidInt, y) + + assert(!l1.isValidInt && (l1 - 1).isValidInt, l1) + assert(!l2.isValidInt && (l2 + 1).isValidInt, l2) + } +} |