This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

integer overflow - > unpredictable ?

 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys : I assumed that an integer overflow would through an exception, but I found otherwise (here are the factorials, computed using both recursion and a while loop, notice the negative numbers after )?

0 : 1 1
1 : 1 1
2 : 2 2
3 : 6 6
4 : 24 24
5 : 120 120
6 : 720 720
7 : 5040 5040
8 : 40320 40320
9 : 362880 362880
10 : 3628800 3628800
11 : 39916800 39916800
12 : 479001600 479001600
13 : 1932053504 1932053504
14 : 1278945280 1278945280
15 : 2004310016 2004310016
16 : 2004189184 2004189184
17 : -288522240 -288522240

Im wondering : How does java deal with multiplication overflows ? I guess since its a primitive operation, there is no way to throw an exception....
But I have had NaN exceptions before, which I think are much cleaner.

Any idea regarding the JVM specifics on this ?
 
Rancher
Posts: 3502
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jay vas wrote:Im wondering : How does java deal with multiplication overflows ? I guess since its a primitive operation, there is no way to throw an exception....


Java could have thrown an exception here - they just chose not to. They throw an exception if you divide by zero in integer division, for example. I think it was a bad decision not to throw an exception in the case of overflow, but they were probably trying to mimic existing behavior of c programs that people were used to back then.

jay vas wrote:But I have had NaN exceptions before, which I think are much cleaner.


They probably weren't NaN exceptions, but NaN results. These are possible values in floating-point operations, but not integer operations.

jay vas wrote:Any idea regarding the JVM specifics on this ?


Basically integer multiplication in java behaves as if the multiplication was done correctly, except that only the lowest 32 bits of the answer are retained. (Or 64 bits if any longs are involved.) No error or warning is given. It's very poor engineering, in my opinion.
 
Saloon Keeper
Posts: 11881
253
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a daily base, making sure that my values don't overflow anywhere in my code gives me the biggest headache. Even if the language threw an exception in this case, it wouldn't change that you have to check everywhere whether there's a possibility for overflow. Still, using unchecked exceptions would have been much much better than silent overflow.

If your program theoretically uses infinite precision, you might want to consider using a BigInteger instead of regular ints or longs.
 
Sheriff
Posts: 21919
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They shouldn't have thrown exceptions automatically, they should have gone the C# way - let the programmer decide to allow or deny overflow. By default integer overflow causes an exception (or error, can't remember) in C#, but with the addition of a keyword to the declaration of the variable it will overflow instead.
 
Marshal
Posts: 68899
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another reason to permit overflow is to enable calculations of hash codes or similar, where one requires a result and doesn't care whether it overflows or not.

As Mike says, Java™ retains the rightmost 32 bits (or 64 for a long), so overflow is actually deterministic and predictable.
 
Campbell Ritchie
Marshal
Posts: 68899
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: the overflow in those factorials occurs at no 13.
 
Mike Simmons
Rancher
Posts: 3502
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:They shouldn't have thrown exceptions automatically, they should have gone the C# way - let the programmer decide to allow or deny overflow.


100% agree, there are valid reasons to want to allow overflow, and sometimes you need it. As long as the default is to throw an exception. Or in some languages the default is to use an infinite-precision datatype - that can work well too.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is a little late to reply, sorry

Catching Integer Overflows is nice to have but there is a large performance overhead. Each time a calculation is done a comparison has to made which is not affordable in real world applications. This is a typical tradeoff between performance and correctness for which each PL has to decide.
 
Campbell Ritchie
Marshal
Posts: 68899
275
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
You have a good point there, and it is never really too late to reply. To come back to Rob S's point: that post was before the introduction of this sort of method. Note the link says, “Since 1.8.”
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic