Skip to content

'(BigDecimal(1) to BigDecimal(s"1.${"0" * 32}1") by BigDecimal(s"0.${"0" * 33}1")).toList' cause infinite loop in Scala 2.13.0-M5 #11152

@xuwei-k

Description

@xuwei-k

I think there are two reasons.

  1. Use MathContext in BigDecimal operations
  2. NumericRange.NumericRangeIterator

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions