bug icon indicating copy to clipboard operation
bug copied to clipboard

`NumericRange.length` fails for wide`Long` ranges.

Open AminMal opened this issue 2 years ago • 5 comments

Reproduction steps

Scala version: 2.13.4, 2.13.8, 2.13.12, 2.13.13

scala> import scala.collection.immutable.NumericRange
import scala.collection.immutable.NumericRange

scala> val r = NumericRange(1L, Long.MinValue, -1L)
val r: collection.immutable.NumericRange.Exclusive[Long] = NumericRange 1 until -9223372036854775808 by -1

scala> r.length
val res0: Int = 1

Problem

Trying to get the length of a wide Long range, somehow 1 is returned, normally, I expected it to throw an IllegalArgumentException much like wide Int or BigInt or BigDecimal. The same exact values but with BigInt is working fine though:

scala> val r = NumericRange(BigInt(1L), BigInt(Long.MinValue), BigInt(-1L)) 
val r: collection.immutable.NumericRange.Exclusive[scala.math.BigInt] = NumericRange 1 until -9223372036854775808 by -1

scala> r.length
java.lang.IllegalArgumentException: More than Int.MaxValue elements.
  at scala.collection.immutable.NumericRange$.check$1(NumericRange.scala:324)
  at scala.collection.immutable.NumericRange$.count(NumericRange.scala:367)
  at scala.collection.immutable.NumericRange.length$lzycompute(NumericRange.scala:75)
  at scala.collection.immutable.NumericRange.length(NumericRange.scala:75)
  ... 32 elided

AminMal avatar Jan 16 '23 01:01 AminMal