This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

integer overflow

 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Considering that Java does not automatically throw an exception (or otherwise object) to integer overflow (it just "wraps around"), what is the best/preferred/whatever style to handle this condition when it is unknown that the inputs and subsequent calculations will for certain stay within bounds (of Integer.MAX_VALUE and Integer.MIN_VALUE in the case of type int)? Should values first be stored as type long and the appropriate comparisons made before continuing? Then, in the case of using data of type long, is there a better way of handling the situation than first storing the data as BigInteger objects and making the appropriate comparisons?
For the purposes of reuse, my thought was to create my own class with static methods that checked the input data and subsequent calculations that the appropriate bounds were not exceeded; but if they were, then trow some custom exception. Or is this function already in one of the standard Java classes?
Thanks for the advice.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JLS, §4.2.2 Integer Operations:

The built-in integer operators do not indicate overflow or underflow in any way.

From that, I'm left with only trickery to determine when overflow or underflow occured. If your using an int, you can try an approach like this:

This will vaguely work. However, if the overflow were so great that the ensuing wrapping brought the resultant value within the valid range of an int again, the overflow or underflow wouldn't be caught. Of course, this doesn't slove anything for catching overflow or underflow on a long...
Sorry, this is the best way I know.
Corey
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I searched all over the web for articles on this subject, and it would seem that no magic trick is available to deal with a possible integer overflow.
Oh well. It looks as if we'll just have to do it the hard way. Nuts to that.
Good Luck.
[ April 05, 2002: Message edited by: Dirk Schreckmann ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I came up with that might be useful for dealing with longs:

Naturally you can add additional methods for subtraction and division, as well as any other operations you want to use. And it may well be possible to improve on the implementations I've provided - I just tossed them off quickly. But I think the basic idea is useful. If you do decide to build something like this, please let us know what you come up with - it may well be useful for others.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh... here you go.
 
krishna kumar.s
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can try this
public class Testlong {
public static int multiply(int a, int b) throws Exception

{


return Math.multiplyExact(a,b);

}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

int r=multiply(-32768,65536) ;

}

}
 
Campbell Ritchie
Sheriff
Pie
Posts: 47284
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math#multiplyExact? That is a new method which I wasn't aware of. It should have been there all along. There is a corresponding addExact method too.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic