diff options
author | Lukas Rytz <lukas.rytz@typesafe.com> | 2015-06-18 09:02:10 +0200 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@typesafe.com> | 2015-06-18 09:02:10 +0200 |
commit | c4f0ba4c108132e9e289032b99c519e0a0b7b056 (patch) | |
tree | 989ebfc259217502cb5f2deec4228dbac83e4267 /test | |
parent | 23bfa691e2c7372f52958912ff7dbf1cadc1a50c (diff) | |
parent | 5ab401084141d37c03dc29c9028917b92e56ca68 (diff) | |
download | scala-c4f0ba4c108132e9e289032b99c519e0a0b7b056.tar.gz scala-c4f0ba4c108132e9e289032b99c519e0a0b7b056.tar.bz2 scala-c4f0ba4c108132e9e289032b99c519e0a0b7b056.zip |
Merge pull request #4527 from nicky-zs/fix_BigDecimal
fix BigDecimal losing MathContext
Diffstat (limited to 'test')
-rw-r--r-- | test/junit/scala/math/BigDecimalTest.scala | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/test/junit/scala/math/BigDecimalTest.scala b/test/junit/scala/math/BigDecimalTest.scala index c7a63da890..a9e2481f37 100644 --- a/test/junit/scala/math/BigDecimalTest.scala +++ b/test/junit/scala/math/BigDecimalTest.scala @@ -228,4 +228,36 @@ class BigDecimalTest { def test_SI8970() { assert((0.1).## == BigDecimal(0.1).##) } + + // Motivated by the problem of MathContext lost + @Test + def testMathContext() { + def testPrecision() { + val p = 1000 + val n = BigDecimal("1.1", MC.UNLIMITED).pow(p) + + // BigDecimal(x: Float, mc: MC), which may not do what you want, is deprecated + assert(BigDecimal(1.1f, MC.UNLIMITED).pow(p) == BigDecimal(java.lang.Double.toString(1.1f.toDouble), MC.UNLIMITED).pow(p)) + assert(BigDecimal(1.1d, MC.UNLIMITED).pow(p) == n) + assert(BigDecimal(new BD("1.1"), MC.UNLIMITED).pow(p) == n) + + assert(BigDecimal.decimal(1.1f, MC.UNLIMITED).pow(p) == n) + assert(BigDecimal.decimal(1.1d, MC.UNLIMITED).pow(p) == n) + assert(BigDecimal.decimal(new BD("1.1"), MC.UNLIMITED).pow(p) == n) + + assert((BigDecimal(11, MC.UNLIMITED) / 10).pow(p) == n) + assert((BigDecimal.decimal(11, MC.UNLIMITED) / 10).pow(p) == n) + } + + def testRounded() { + // the default rounding mode is HALF_UP + assert((BigDecimal(1.23f, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24")) // deprecated api + assert((BigDecimal(1.23d, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24")) + assert((BigDecimal.decimal(1.23f, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24")) + assert((BigDecimal.decimal(1.23d, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24")) + } + + testPrecision() + testRounded() + } } |