Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit c4f0ba4

Browse files
committed
Merge pull request scala#4527 from nicky-zs/fix_BigDecimal
fix BigDecimal losing MathContext
2 parents 23bfa69 + 5ab4010 commit c4f0ba4

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/library/scala/math/BigDecimal.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ object BigDecimal {
4949

5050
/** Constructs a `BigDecimal` using the decimal text representation of `Double` value `d`, rounding if necessary. */
5151
def decimal(d: Double, mc: MathContext): BigDecimal =
52-
new BigDecimal(new BigDec(java.lang.Double.toString(d), mc))
52+
new BigDecimal(new BigDec(java.lang.Double.toString(d), mc), mc)
5353

5454
/** Constructs a `BigDecimal` using the decimal text representation of `Double` value `d`. */
5555
def decimal(d: Double): BigDecimal = decimal(d, defaultMathContext)
@@ -59,7 +59,7 @@ object BigDecimal {
5959
* `0.1 != 0.1f`.
6060
*/
6161
def decimal(f: Float, mc: MathContext): BigDecimal =
62-
new BigDecimal(new BigDec(java.lang.Float.toString(f), mc))
62+
new BigDecimal(new BigDec(java.lang.Float.toString(f), mc), mc)
6363

6464
/** Constructs a `BigDecimal` using the decimal text representation of `Float` value `f`.
6565
* Note that `BigDecimal.decimal(0.1f) != 0.1f` since equality agrees with the `Double` representation, and

test/junit/scala/math/BigDecimalTest.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,36 @@ class BigDecimalTest {
228228
def test_SI8970() {
229229
assert((0.1).## == BigDecimal(0.1).##)
230230
}
231+
232+
// Motivated by the problem of MathContext lost
233+
@Test
234+
def testMathContext() {
235+
def testPrecision() {
236+
val p = 1000
237+
val n = BigDecimal("1.1", MC.UNLIMITED).pow(p)
238+
239+
// BigDecimal(x: Float, mc: MC), which may not do what you want, is deprecated
240+
assert(BigDecimal(1.1f, MC.UNLIMITED).pow(p) == BigDecimal(java.lang.Double.toString(1.1f.toDouble), MC.UNLIMITED).pow(p))
241+
assert(BigDecimal(1.1d, MC.UNLIMITED).pow(p) == n)
242+
assert(BigDecimal(new BD("1.1"), MC.UNLIMITED).pow(p) == n)
243+
244+
assert(BigDecimal.decimal(1.1f, MC.UNLIMITED).pow(p) == n)
245+
assert(BigDecimal.decimal(1.1d, MC.UNLIMITED).pow(p) == n)
246+
assert(BigDecimal.decimal(new BD("1.1"), MC.UNLIMITED).pow(p) == n)
247+
248+
assert((BigDecimal(11, MC.UNLIMITED) / 10).pow(p) == n)
249+
assert((BigDecimal.decimal(11, MC.UNLIMITED) / 10).pow(p) == n)
250+
}
251+
252+
def testRounded() {
253+
// the default rounding mode is HALF_UP
254+
assert((BigDecimal(1.23f, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24")) // deprecated api
255+
assert((BigDecimal(1.23d, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24"))
256+
assert((BigDecimal.decimal(1.23f, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24"))
257+
assert((BigDecimal.decimal(1.23d, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24"))
258+
}
259+
260+
testPrecision()
261+
testRounded()
262+
}
231263
}

0 commit comments

Comments
 (0)