diff options
-rw-r--r-- | src/library/scala/math/BigDecimal.scala | 7 | ||||
-rw-r--r-- | test/files/run/bigDecimalCache.scala | 9 |
2 files changed, 14 insertions, 2 deletions
diff --git a/src/library/scala/math/BigDecimal.scala b/src/library/scala/math/BigDecimal.scala index 6bd6b33484..bb6965fcdc 100644 --- a/src/library/scala/math/BigDecimal.scala +++ b/src/library/scala/math/BigDecimal.scala @@ -29,6 +29,8 @@ object BigDecimal private val minCached = -512 private val maxCached = 512 + + /** Cache ony for defaultMathContext using BigDecimals in a small range. */ private lazy val cache = new Array[BigDecimal](maxCached - minCached + 1) val defaultMathContext = MathContext.UNLIMITED @@ -50,12 +52,13 @@ object BigDecimal */ def apply(i: Int): BigDecimal = apply(i, defaultMathContext) def apply(i: Int, mc: MathContext): BigDecimal = - if (minCached <= i && i <= maxCached) { + if (mc == defaultMathContext && minCached <= i && i <= maxCached) { val offset = i - minCached var n = cache(offset) if (n eq null) { n = new BigDecimal(BigDec.valueOf(i), mc); cache(offset) = n } n - } else new BigDecimal(BigDec.valueOf(i), mc) + } + else new BigDecimal(BigDec.valueOf(i), mc) /** Constructs a <code>BigDecimal</code> whose value is equal to that of the * specified long value. diff --git a/test/files/run/bigDecimalCache.scala b/test/files/run/bigDecimalCache.scala new file mode 100644 index 0000000000..c0c709a50f --- /dev/null +++ b/test/files/run/bigDecimalCache.scala @@ -0,0 +1,9 @@ +object Test { + def main(args: Array[String]): Unit = { + val bd5a = BigDecimal(5) + val mc = java.math.MathContext.DECIMAL32 + val bd5b = BigDecimal(5,mc) + + assert(bd5b.mc == mc) + } +} |