-
Notifications
You must be signed in to change notification settings - Fork 21
Closed
Description
I think there are two reasons.
not infinite loop when toArray
because toArray
does not use NumericRange#iterator
.
Welcome to Scala 2.13.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_181).
Type in expressions for evaluation. Or try :help.
scala> (BigDecimal(1) to BigDecimal(s"1.${"0" * 32}1") by BigDecimal(s"0.${"0" * 33}1")).toArray
res0: Array[scala.math.BigDecimal] = Array(1, 1.000000000000000000000000000000000, 1.000000000000000000000000000000000, 1.000000000000000000000000000000000, 1.000000000000000000000000000000000, 1.000000000000000000000000000000000, 1.000000000000000000000000000000000, 1.000000000000000000000000000000000, 1.000000000000000000000000000000000, 1.000000000000000000000000000000000, 1.000000000000000000000000000000000)
toArray
result different from Scala 2.12.x due to MathContext changes
Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_181).
Type in expressions for evaluation. Or try :help.
scala> (BigDecimal(1) to BigDecimal(s"1.${"0" * 32}1") by BigDecimal(s"0.${"0" * 33}1")).toArray
res0: Array[scala.math.BigDecimal] = Array(1.0000000000000000000000000000000000, 1.0000000000000000000000000000000001, 1.0000000000000000000000000000000002, 1.0000000000000000000000000000000003, 1.0000000000000000000000000000000004, 1.0000000000000000000000000000000005, 1.0000000000000000000000000000000006, 1.0000000000000000000000000000000007, 1.0000000000000000000000000000000008, 1.0000000000000000000000000000000009, 1.0000000000000000000000000000000010)
BigDecimal#+
difference Scala 2.12 <=> 2.13
scala> new java.math.BigDecimal(1).add(new java.math.BigDecimal(s"0.${"0" * 33}1")) // Scala 2.12.x BigDecimal#+
res0: java.math.BigDecimal = 1.0000000000000000000000000000000001
scala> res0 compareTo (new java.math.BigDecimal(1))
res1: Int = 1
scala> new java.math.BigDecimal(1).add(new java.math.BigDecimal(s"0.${"0" * 33}1"), java.math.MathContext.DECIMAL128) // Scala 2.13.x BigDecimal#+
res2: java.math.BigDecimal = 1.000000000000000000000000000000000
scala> res2 compareTo (new java.math.BigDecimal(1)) // this cause infinite loop in NumericRangeIterator
res3: Int = 0
DECIMAL128
is default MathContext inscala.BigDecimal
https://github.com/scala/scala/blob/v2.12.6/src/library/scala/math/BigDecimal.scala#L30scala.BigDecimal#equals
usejava.math.BigDecimal#compareTo
notjava.math.BigDecimal#equals