Skip to content
Browse files

BigDecimal adjustments.

More sensible use of MathContext, plus some BigDecimal tests.
Contributed by Erik Osheim. Closes SI-4981, no review.
  • Loading branch information...
1 parent 82eb1aa commit 3a1332c451c8bd9b987ab3dbe775ef5a08360705 @paulp paulp committed Sep 28, 2011
View
14 src/library/scala/math/BigDecimal.scala
@@ -225,11 +225,11 @@ extends ScalaNumber with ScalaNumericConversions with Serializable {
/** Addition of BigDecimals
*/
- def + (that: BigDecimal): BigDecimal = this.bigDecimal.add(that.bigDecimal, mc)
+ def + (that: BigDecimal): BigDecimal = this.bigDecimal.add(that.bigDecimal)
/** Subtraction of BigDecimals
*/
- def - (that: BigDecimal): BigDecimal = this.bigDecimal.subtract(that.bigDecimal, mc)
+ def - (that: BigDecimal): BigDecimal = this.bigDecimal.subtract(that.bigDecimal)
/** Multiplication of BigDecimals
*/
@@ -243,14 +243,14 @@ extends ScalaNumber with ScalaNumericConversions with Serializable {
* divideToIntegralValue and the remainder.
*/
def /% (that: BigDecimal): (BigDecimal, BigDecimal) =
- this.bigDecimal.divideAndRemainder(that.bigDecimal, mc) match {
+ this.bigDecimal.divideAndRemainder(that.bigDecimal) match {
case Array(q, r) => (q, r)
}
/** Divide to Integral value.
*/
def quot (that: BigDecimal): BigDecimal =
- this.bigDecimal.divideToIntegralValue(that.bigDecimal, mc)
+ this.bigDecimal.divideToIntegralValue(that.bigDecimal)
/** Returns the minimum of this and that
*/
@@ -262,7 +262,7 @@ extends ScalaNumber with ScalaNumericConversions with Serializable {
/** Remainder after dividing this by that.
*/
- def remainder (that: BigDecimal): BigDecimal = this.bigDecimal.remainder(that.bigDecimal, mc)
+ def remainder (that: BigDecimal): BigDecimal = this.bigDecimal.remainder(that.bigDecimal)
/** Remainder after dividing this by that.
*/
@@ -274,11 +274,11 @@ extends ScalaNumber with ScalaNumericConversions with Serializable {
/** Returns a BigDecimal whose value is the negation of this BigDecimal
*/
- def unary_- : BigDecimal = this.bigDecimal.negate(mc)
+ def unary_- : BigDecimal = this.bigDecimal.negate()
/** Returns the absolute value of this BigDecimal
*/
- def abs: BigDecimal = this.bigDecimal abs mc
+ def abs: BigDecimal = this.bigDecimal abs
/** Returns the sign of this BigDecimal, i.e.
* -1 if it is less than 0,
View
6 test/files/run/bigDecimalTest.check
@@ -0,0 +1,6 @@
+34
+83
+0
+0
+0
+14
View
35 test/files/run/bigDecimalTest.scala
@@ -0,0 +1,35 @@
+object Test {
+ def main(args: Array[String]): Unit = {
+
+ // SI-4981: avoid being limited by math context when not needed
+ val big = BigDecimal("32432875832753287583275382753288325325328532875325")
+ val f = big % BigDecimal(scala.math.Pi)
+
+ // SI-1812: use math context to limit decimal expansion
+ val a = BigDecimal(1) / BigDecimal(3)
+ val b = BigDecimal(1) / big
+
+ // SI-2199: implicit conversions from java.math.BigDecimal to BigDecimal
+ val c = BigDecimal(1) + (new java.math.BigDecimal(3))
+
+ // SI-2024: correctly use BigDecimal.valueOf
+ assert(BigDecimal(123) + 1.1 == BigDecimal("124.1"))
+
+ // SI-3206: BigDecimal cache errors
+ val d = BigDecimal(2, new java.math.MathContext(33))
+ val e = BigDecimal(2, new java.math.MathContext(34))
+ assert(d.mc != e.mc)
+
+ // SI-921
+ assert(BigDecimal(2) / BigDecimal(0.5) == BigDecimal(4))
+
+ // SI-2304: enforce equals/hashCode contract
+ assert(BigDecimal("2").hashCode == BigDecimal("2.00").hashCode)
+
+ // SI-4547: implicit conversion
+ assert(5 + BigDecimal(3) == BigDecimal(8))
+
+ // meaningless sanity check
+ List[BigDecimal](a, b, c, d, e, f) map (_.scale) foreach println
+ }
+}

0 comments on commit 3a1332c

Please sign in to comment.
Something went wrong with that request. Please try again.