This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes integer overflow - > unpredictable ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "integer overflow - > unpredictable ? " Watch "integer overflow - > unpredictable ? " New topic
Author

integer overflow - > unpredictable ?

jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
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 ?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3573
    
  14

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37897
    
  22
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
Sheriff

Joined: Oct 13, 2005
Posts: 37897
    
  22
By the way: the overflow in those factorials occurs at no 13.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: integer overflow - > unpredictable ?
 
Similar Threads
variable declaration changes output
printing the combination of all the letters
Arrays - JLS
MQJE001: Completion Code 2, Reason 2085 ERROR
How to trace recursion questions.