• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Iterating over all int values.

 
Bartender
Posts: 2220
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a little problem with iterating over all integer values.

Assume I need to execute a method with an int argument for all possible values. Don't ask me why, it's just hypothetical

I might do this:
This of course is bad as it skips Integer.MAX_VALUE.

I can not put i <= Integer.MAX_VALUE in for because this condition is always true and the loop will never terminate.

I could write this like below:
But this could not be nice if I needed something more than one-liner.

I could use long i instead of int i. That would work. But assume I need to iterate over all long values. And we are back to square one and there is no integer primitive bigger than long.

I could write......but this is just ugly.

Do you have a better idea?
 
Marshal
Posts: 65457
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are ways to do it, but I can't remember where I saw them; what about a do loop?
 
Marshal
Posts: 4671
305
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell I assume you mean Integer.MAX_VALUE in your while criteria? Either way I think your solution behaves the same way a Pawel's for loop with the exit clause of i < Integer.MAX_VALUE in that you will never call foo(i) with i equal to Integer.MAX_VALUE.

The following will achieve what you want

Putting the i++ inside the while condition, and checking the value before incrementing, causes the loop to iterate one more time when i is Integer.MAX_VALUE - 1.
 
Paweł Baczyński
Bartender
Posts: 2220
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah... why didn't I think about do loop? Maybe because I don't remember when is the last last time I used it

Tim Cooke wrote:Campbell I assume you mean Integer.MAX_VALUE in your while criteria? Either way I think your solution behaves the same way a Pawel's for loop with the exit clause of i < Integer.MAX_VALUE in that you will never call foo(i) with i equal to Integer.MAX_VALUE.


Actually Campbell's solution works . Except for the small syntax error
 
Tim Cooke
Marshal
Posts: 4671
305
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:Actually Campbell's solution works . Except for the small syntax error


Really? For me foo(i) is not called where i is Integer.MAX_VALUE. I corrected his syntax to give:

- When i is Integer.MAX_VALUE - 1 then foo(i) is called.
- i is incremented to Integer.MAX_VALUE.
- (i != Integer.MAX_VALUE) is false.
- Loop exits.
- foo(i) is never called where i is Integer.MAX_VALUE.
 
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:
I could use long i instead of int i. That would work. But assume I need to iterate over all long values. And we are back to square one and there is no integer primitive bigger than long.



Well, I am not sure if using a long is the best idea -- but I will try to argue for it regardless.

How long does it take to run the program that iterates through all the int values? Is it instantaneous? Does it take a few seconds? Or does it take more than a minute? Now, keep in mind that your test() method does very little -- so a real use case will likely take longer.

Anyway, back to your task of iterating over all the long values (which puts you back to square one), it will take longer than that. Approximately, 4 billion times longer. Doing the math, assuming that iterating over all of the ints only take a second, I get something of over 125 years to iterate over all of the longs.


Now, I am not saying to not take care of the edge case -- just noting that the reason for not using the long data type is a bit not reasonable. Why not use a long, if a bigger primitive range is needed? And if a data type greater than long is needed, you can worry about that when the requirement happens.

Henry
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Cooke wrote:Campbell I assume you mean Integer.MAX_VALUE in your while criteria?


No he didn't.
i will be processed when it gets to the value Integer.MAX_VALUE. It will then be incremnetd, wrap around around so it becomes Integer.MIN_VALUE and the loop will exit.

The small syntax error Pawel referred to was the while being inside the brackets
 
Tim Cooke
Marshal
Posts: 4671
305
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart, yes I see what he's done now.

That's a very subtle condition based on the language specific knowledge of what happens when you increment an int outside of its bounds. Most unintuitive.
 
Tim Cooke
Marshal
Posts: 4671
305
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry makes a great point about the practical consideration of iterating over all long values.

But it is interesting as an academic exercise nonetheless.
 
Paweł Baczyński
Bartender
Posts: 2220
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, iterating all long values is just hypothetical

Fot int I think I decided to like long variable .
It is more clear that relying on Integer.MAX_VALUE + 1 yielding Integer.MIN_VALUE.

Also, I disagree this behavior is language specific. It is described by Two's complement.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't you use longs to iterate all ints, and BigInteger to iterate all longs?

Or something like that.
 
Paweł Baczyński
Bartender
Posts: 2220
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Luke wrote:Couldn't you use longs to iterate all ints, and BigInteger to iterate all longs?

Or something like that.



I wouldn't dare .
As already mentioned iterating over all long walues is practically impossible.
On my machine, iterating over all ints takes 12 seconds.
Iterating over all longs would take 4294967298 longer.
That is 51539607576 sec = 858993460 minutes = 14316558 hours = 596523 days = 1633 years. //rounding appllied

Now, iterating over all integers using BigInteger took 191 seconds on my machine which was 16 times slower than using primitive type.
So, extrapolating, iterating over all longs with this approach would take something like 26128 years
Yikes!

PS. Why use BigInteger.valueOf(1) when you have BigInteger.ONE?
 
Henry Wong
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:
Now, iterating over all integers using BigInteger took 191 seconds on my machine which was 16 times slower than using primitive type.
So, extrapolating, iterating over all longs with this approach would take something like 26128 years
Yikes!




Moore's law may be applicable here.

It may be faster to write the Java program but not run it. Instead, wait about 20 years, then port the program to the prevalent language equivalent (for that time period). Compile and run on a modern day machine (assuming that compiling is even needed in this language). And you can be done in about a couple of more years.

Of course, by then, there will also likely be data types bigger than longs -- and we have to go through this hypothetical exercise again ...

Henry
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:PS. Why use BigInteger.valueOf(1) when you have BigInteger.ONE?


Because I didn't see BigInteger.ONE. I did, however, think it would be a good idea if it existed
 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
((((((What syntax error?
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:((((((What syntax error?

I think Pawel must be referring to your method, foo(i). He doesn't have a foo(i), but test(i) instead
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I had compile error in mind when I wrote it!
 
Tim Cooke
Marshal
Posts: 4671
305
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:((((((What syntax error?


It's probably the "while" keyword inside the parenthesis that Pawel is talking about.

Wait, hold on, that's a lot of parenthesis you've got there for no good reason. You know this already don't you.....
 
Campbell Ritchie
Marshal
Posts: 65457
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What parentheses?

Syntax errors give compiler errors, so you can look on the two as near‑synonyms.I deleted a few lines from this output:-

java IterationDemo
-2147483648
-1879048193
-1879048192
. . .
-1
0
. . .
1879048191
1879048192
2147483647

Edit: add smilie
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!