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

Commit 5ab4010

Browse files
author
Zhong Sheng
committed
make BigDecimalTest.testMathContext a bit easier to understand
1 parent b8a4a97 commit 5ab4010

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

test/junit/scala/math/BigDecimalTest.scala

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,27 +233,28 @@ class BigDecimalTest {
233233
@Test
234234
def testMathContext() {
235235
def testPrecision() {
236-
val e = 1000
236+
val p = 1000
237+
val n = BigDecimal("1.1", MC.UNLIMITED).pow(p)
237238

238-
val n = BigDecimal("1.1", MC.UNLIMITED).pow(e)
239-
assert(BigDecimal(1.1d, MC.UNLIMITED).pow(e) == n)
240-
assert(BigDecimal.decimal(1.1d, MC.UNLIMITED).pow(e) == n)
241-
assert(BigDecimal.decimal(1.1f, MC.UNLIMITED).pow(e) == n)
242-
assert(BigDecimal.decimal(new BD("1.1"), MC.UNLIMITED).pow(e) == n)
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)
243243

244-
val m = BigDecimal(java.lang.Double.toString(1.1f.toDouble), MC.UNLIMITED).pow(e)
245-
assert(BigDecimal(1.1f, MC.UNLIMITED).pow(e) == m) // deprecated
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)
246247

247-
val l = BigDecimal("11", MC.UNLIMITED).pow(e)
248-
assert(BigDecimal(11, MC.UNLIMITED).pow(e) == l)
249-
assert(BigDecimal.decimal(11, MC.UNLIMITED).pow(e) == l)
248+
assert((BigDecimal(11, MC.UNLIMITED) / 10).pow(p) == n)
249+
assert((BigDecimal.decimal(11, MC.UNLIMITED) / 10).pow(p) == n)
250250
}
251251

252252
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
253255
assert((BigDecimal(1.23d, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24"))
254-
assert((BigDecimal(1.23f, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24")) // deprecated
255-
assert((BigDecimal.decimal(1.23d, new MC(3)) + BigDecimal("0.005")).rounded == BigDecimal("1.24"))
256256
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"))
257258
}
258259

259260
testPrecision()

0 commit comments

Comments
 (0)