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 broken when working with MinValue/MaxValue #3232

Closed
scabug opened this issue Mar 30, 2010 · 5 comments
Closed

Range broken when working with MinValue/MaxValue #3232

scabug opened this issue Mar 30, 2010 · 5 comments
Assignees

Comments

@scabug
Copy link

scabug commented Mar 30, 2010

  • Range with an upper bound of MaxValue results in an empty Range, example:
(Int.MaxValue -5 to Int.MaxValue)
res0: scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range()

(0 to 2147483647) 
res28: scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range()
  • This works again:
(Int.MaxValue -5 to Int.MaxValue -1)  
res3: scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range(2147483642, 2147483643, 2147483644, 2147483645, 2147483646)
  • This works:
(0 to Int.MaxValue -1)
res8: scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16...
  • This not:
(Int.MinValue to 0)
java.lang.OutOfMemoryError: Java heap space
	at scala.collection.mutable.StringBuilder.ensureCapacity(StringBuilder.scala:121)
	at scala.collection.mutable.StringBuilder.append(StringBuilder.scala:262)
	at scala.collection.TraversableLike$$$$anonfun$$addString$$1.apply(TraversableLike.scala:1083)
	at scala.collection.TraversableLike$$$$anonfun$$addString$$1.apply(TraversableLike.scala:1081)
	at scala.collection.immutable.Range.foreach(Range.scala:47)
	at scala.collection.TraversableLike$$class.addString(TraversableLike.scala:1081)
	at scala.collection.immutable.Range.addString(Range.scala:29)
	at scala.collection.TraversableLike$$class.mkString(TraversableLike.scala:1045)
	at scala.collection.immutable.Range.mkString(Range.scala:29)
	at scala.collection.immutable.Range.toString(Range.scala:133)
	at scala.runtime.ScalaRunTime$$.stringOf(ScalaRunTime.scala:195)
	at RequestResult$$.<init>(<console>:9)
	at RequestResult$$.<clinit>(<console>)
	at RequestResult$$scala_repl_result(<console>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at scala.tools.nsc.Interpreter$$Request$$$$anonfun$$loadAndRun$$1$$$$anonfun$$apply$$13.apply(Interpreter.scala:827)
	at scala.tools.nsc.Interpreter$$Request$$$$anonfun$$loadAndRun$$1$$$$anonfun$$apply$$13.apply(Interpreter.scala:827)
	at scala.util.control.Exception$$Catch.apply(Exception.scala:79)
	at scala.tools.nsc.Interpreter$$Request$$$$anonfun$$loadAndRun$$1.apply(Interpreter.scala:826)
	at scala.tools.nsc.Interpreter$$Request$$$$anonfun$$loadAndRun$$1.apply(Interpreter.scala:826)
	at scala.util.control.Exception$$Catch.apply(Exception.scala:79)
	at scala.tools.nsc.Interpreter$$Request.loadAndRun(Interpreter.scala:825)
	at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:467)
	at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:457)
	at scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scala:391)
	at scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:367)
	at scala.tools.nsc.InterpreterLoop.processLine$$1(InterpreterLoop.scala:249)
	at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:267)
	at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:439)
  • With Longs:
(0L to Long.MaxValue)
res13: scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange()
  • This might be the same bug as the Int.MaxValue one, so I tried:
(0L to Long.MaxValue -1)
java.lang.IllegalArgumentException: Implementation restricts ranges to Int.MaxValue elements.
	at scala.collection.immutable.NumericRange.fail(NumericRange.scala:44)
	at scala.collection.immutable.NumericRange.<init>(NumericRange.scala:54)
	at scala.collection.immutable.NumericRange$$Inclusive.<init>(NumericRange.scala:201)
	at scala.collection.immutable.NumericRange$$.inclusive(NumericRange.scala:220)
	at scala.collection.immutable.Range$$Long$$.inclusive(Range.scala:185)
	at scala.runtime.RichLong.to(RichLong.scala:35)
	at .<init>(<console>:5)
	at .<clinit>(<console>)
	at RequestResult$$.<init>(<console>:4)
	at RequestResult$$.<clinit>(<console>)
	at RequestResult$$scala_repl_result(<console>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at scala.tools.nsc.Interpreter$$Request$$$$anonfun$$loadAndRun$$1$$$$anonfun$$apply$$13.apply(Interpreter.scala:827)
	at scala.tools.nsc.Interpreter$$Request$$$$anonfun$$loadAndRun$$1$$$$anonfun$$apply$$13.apply(Interpreter.scala:827)
	at scala.util.control.Exception$$Catch.apply(Exception.scala:79)
	at scala.tools.nsc.Interpreter$$Request$$$$anonfun$$loadAndRun$$1.apply(Interpreter.scala:826)
	at scala.tools.nsc.Interpreter$$Request$$$$anonfun$$loadAndRun$$1.apply(Interpreter.scala:826)
	at scala.util.control.Exception$$Catch.apply(Exception.scala:79)
	at scala.tools.nsc.Interpreter$$Request.loadAndRun(Interpreter.scala:825)
	at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:467)
	at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:457)
	at scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scala:391)
	at scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:367)
	at scala.tools.nsc.InterpreterLoop.processLine$$1(InterpreterLoop.scala:249)
	at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:267)
	at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:439)
	at scala.tools.nsc.MainGenericRunner$$.createLoop$$1(MainGenericRunner.scala:118)
	at scala.tools.nsc.MainGenericRunner$$.main(MainGenericRunner.scala:143)
	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
  • These are both weird:
(Long.MinValue to 0)
res18: scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange(-9223372036854775808)

(Long.MinValue to 0L)
res19: scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange(-9223372036854775808)
  • Shorts seem to work (I guess because they are converted to Ints internally)

  • I'm not sure if this is right:

 (Byte.MinValue to 0)      
<console>:5: error: type mismatch;
 found   : Byte(-128)
 required: Char
       (Byte.MinValue to 0)
             ^

((-5).toByte to 0.toByte)
res36: scala.collection.IndexedSeqView[Char,IndexedSeq[Char]] = IndexedSeqView()
  • Chars:
(Char.MinValue to 0.toChar)       
res47: scala.collection.IndexedSeqView[Char,IndexedSeq[Char]] = IndexedSeqView()

(0.toChar to Char.MaxValue)
res0: scala.collection.IndexedSeqView[Char,IndexedSeq[Char]] = IndexedSeqView()

(0.toChar to Char.MaxValue -1)
res1: scala.collection.IndexedSeqView[Char,IndexedSeq[Char]] = 
IndexedSeqView(, , , , , , , ,, 	, 
, 
  , 
, , , , , , , , , , , , , , , , , , ,  , !, ", #, $$, %, &, ', (, ), *, +, ,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, [, \, ], ^, _, `, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, {, |, }, ~, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, �, ...

I guess there are much more things like that ...

PS: Is there a way to increase the size of the text input box?

@scabug
Copy link
Author

scabug commented Mar 30, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3232?orig=1
Reporter: @soc

@scabug
Copy link
Author

scabug commented Mar 30, 2010

@soc said:
Version:

Welcome to Scala version 2.8.0.Beta1-prerelease (Java HotSpot(TM) Client VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.

@scabug
Copy link
Author

scabug commented Mar 30, 2010

@paulp said:
There are two classes here, each with separate bugs. The standard Range bug being reported is a duplicate of #2535. NumericRange is apparently doing some weird stuff, but keep in mind that no kind of range can have more than 2^32 values because Seq.apply takes an Int.

@scabug
Copy link
Author

scabug commented Jun 22, 2010

@soc said:

== Status update (2.8.0 RC6) ==

Long

Long.MinValue to -2L
_java.lang.IllegalArgumentException: Implementation restricts ranges to Int.MaxValue elements._ // This is correct.

Long.MinValue to -1L                   
_scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange()_ // This should result in the same Error as above...

Long.MinValue to 0L   
_scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange(-9223372036854775808)_ // This too

Long.MinValue to 1L       
_scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange(-9223372036854775808, -9223372036854775807)_

-1L to Long.MaxValue
_scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange(-1)_

0L to Long.MaxValue 
_scala.collection.immutable.NumericRange.Inclusive[Long] = NumericRange()_

1L to Long.MaxValue 
_java.lang.IllegalArgumentException: Implementation restricts ranges to Int.MaxValue elements._

Int

Int.MinValue to Int.MaxValue
_scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range()_

Int.MinValue to -2
_scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range(-2147483648, -2147483647, -2147483646, -2147483645, -2147483644, -2147483643, -2147483642, -2147483641, -2147483640, -2147483639, -2147483638, ..._

Int.MinValue to -1
_scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range()_

Int.MinValue to 0 
_scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range()_

Short
Seems to work.

Chars

Char.MinValue to Char.MaxValue
_scala.collection.immutable.NumericRange[Char] = NumericRange()_

(Char.MinValue:Int) to (Char.MaxValue) 
_scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, ..._

Byte
Seems to work.

Until this is fixed it might be a good thing to add a check like

if(step > 0) require(start < end)
else (step < 0) require(start > end)

to at least warn people that their code looks right, but won't work.

@scabug
Copy link
Author

scabug commented Nov 1, 2010

@paulp said:
(In r23422) Achieved similar simplicity gains in NumericRange to those now in Range.
Obvious remaining task is to specialize NumericRange and after verifying
the performance, eliminate one or the other. For now, both soldier
onward despite near-convergence of implementation.

Closes #3232, no review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants