@@ -228,4 +228,36 @@ class BigDecimalTest {
228
228
def test_SI8970 () {
229
229
assert((0.1 ).## == BigDecimal (0.1 ).## )
230
230
}
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
+ }
231
263
}
0 commit comments