Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range returns empty Collection with Int boundaries and large power of 2 step size #9388

Closed
scabug opened this issue Jul 9, 2015 · 4 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Jul 9, 2015

Range returns empty Collection when using Int boundaries and large power of 2 as step size.
When converting to a list or vector, the collection is still allegedly empty, while converting to an Array yields presumably correct values.

Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_75).
Type in expressions to have them evaluated.
Type :help for more information.

scala> (Int.MinValue to Int.MaxValue by 65536*128)
res0: scala.collection.immutable.Range = Range()

scala> (Int.MinValue to Int.MaxValue by 65536*128).toVector
res1: Vector[Int] = Vector()

scala> (Int.MinValue to Int.MaxValue by 65536*128).toList
res2: List[Int] = List()

scala> (Int.MinValue to Int.MaxValue by 65536*128).toArray
res3: Array[Int] = Array(-2147483648, -2139095040, -2130706432, -2122317824, -2113929216, -2105540608, -2097152000, -2088763392, -2080374784, -2071986176, 
...

The range is non-empty, presumably correct, when step size is smaller than 2^23:

scala> (Int.MinValue to Int.MaxValue by 65536*64)
res4: scala.collection.immutable.Range = Range(-2147483648, -2143289344, -2139095040, -2134900736, -2130706432, -2126512128, -2122317824, -2118123520, -2113929216, 
...

The range is also presumably correct when not using a power of 2 as the step size:

scala> (Int.MinValue to Int.MaxValue by 65536*129)
res9: scala.collection.immutable.Range = Range(-2147483648, -2139029504, -2130575360, -2122121216, -2113667072, -2105212928, -2096758784, -2088304640, -2079850496, 
...
@scabug
Copy link
Author

scabug commented Jul 9, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9388?orig=1
Reporter: Jost Berthold (jberthold)
Affected Versions: 2.10.4, 2.11.6

@scabug
Copy link
Author

scabug commented Jul 31, 2015

Kevin Mas Ruiz (kmruiz) said:
I found some interesting effects regarding to this bug:

(Int.MinValue to Int.MaxValue by 65536128).length == (Int.MinValue to Int.MaxValue by 65536128).toArray.length

Seems that the computation is done correctly but the string representation fails, even if using mkString.

Also seems that:

(Int.MinValue to Int.MaxValue by 65536*128).toList.length == 0

and

(Int.MinValue to Int.MaxValue by 65536*128).toVector.length == 0

So those methods are failing to create the new collections.

@scabug
Copy link
Author

scabug commented Aug 1, 2015

Kevin Mas Ruiz (kmruiz) said:
Found the error.

The problem is at Range at the method terminalElement because it computes the element after the last element as an Int. Computing it as an Int causes an overflow, causing that start and end are equal.

This only happens on toList and on toVector because are the unique methods of Range that relies on foreach, the method that uses terminalElement.

I added the patch to my Pull Request: scala/scala#4676

@scabug
Copy link
Author

scabug commented Aug 29, 2015

@Ichoran said:
scala/scala#4716

fixes this and related issues.

@scabug scabug closed this as completed Sep 21, 2015
@scabug scabug added the has PR label Apr 7, 2017
@scabug scabug added this to the 2.11.8 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants