I understand the overflows in java, but what is called underflows? and how can java handle it ?
Asked
Active
Viewed 1,346 times
1
-
1Are you referring to integer underflow, or floating point underflow? – Justin Apr 25 '11 at 20:37
-
Duplicate http://stackoverflow.com/questions/2154712/common-underflow-and-overflow-exceptions ? – DJClayworth Apr 25 '11 at 20:38
-
The answer, as far as I know, is it doesn't. You'll get the exact same behavior as you would with a series of full adders. – Apr 26 '11 at 08:26
2 Answers
4
Underflow is the exact opposite of overflow.
int high = Integer.MAX_VALUE;
int overflow = high + 1;
int low = Integer.MIN_VALUE;
int underflow = low - 1;
And you handle it the same way: you make sure inputs are not going to put yourself in the range of over/underflow, and make the user aware of potential shortcomings. (Consider Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE
for instance.)

corsiKa
- 81,495
- 25
- 153
- 204
-
To the best of my knowledge that's wrong (compare my answer). Could you cite sources for your claim? – musiKk Apr 25 '11 at 20:53
-
how about the first result in Google for "integer underflow": http://cwe.mitre.org/data/definitions/191.html Note that floating point underflow and integer underflow are different notions, and the OP specifically asked for integer underflow. – corsiKa Apr 25 '11 at 20:55
-
i'd say subtracting one from MIN_VALuE is just overflow in the negative direction. – MeBigFatGuy Apr 25 '11 at 21:30
-
@MeBigFatGuy I've heard it called underflow from as long as I can remember. I have also heard it called "negative overflow." I cannot think of another definition of "integer underflow" though, and I think my handling of it is appropriate. – corsiKa Apr 25 '11 at 21:32
-
@glowcoder: Fair enough. Given the clueless nature of the question I ignored the _integer_ in the title and only concentrated on _underflow_ as indicated in the question. I still think underflow per se is not the opposite of overflow. The downvote to my answer puts me in a rather lonesome position. If you edit your answer in some way, I'm able to remove my downvote to your answer. Please excuse my knee-jerk reaction. – musiKk Apr 26 '11 at 06:56
-
@musiKk I don't see anything to excuse. You read my answer as something that did not fit the definition you were familiar with and considered it harmful. When someone has an answer that is detrimental to those who read it, I say downvote. I'm more than willing to work with critique on my posts because I feel it leads to everyone learning more (we all are flat out wrong sometimes!) I also feel your answer, while I didn't upvote because it ignored an important aspect of the question, I wouldn't consider it DV material. It's accurate and cited; most posts lack either one! – corsiKa Apr 26 '11 at 08:28
-
Underflow cannot occur with Integers, because the smallest number greater than 0 is 1. Underflow only applies to floating point operations, and is essentially a loss of precision in which a calculation result cannot be distinguished from 0, due to limitations of the representation of Floating types. In Java 8, you can use the Math.subtractExact() method to show that what you call Underflow results in an Overflow exception. – Jool Mar 08 '21 at 12:23
-
@Jool Sorry but your definition is simply incorrect. If you subtract two integers and the result would be lower than `MIN_VALUE` an underflow occurs. The `OverflowException` that is thrown is thrown because it would be a waste of everyone's time to throw a different exception when the way to handle it is the same: tell your user the inputs are invalid due to the valid range of integers. You may choose to constrain your definition to it, but many developers do not. The definition in this answer is as accurate now as it was 10 years ago when I wrote it. – corsiKa Mar 11 '21 at 03:26
0
Quoting Wikipedia:
The term arithmetic underflow (or "floating point underflow", or just "underflow") is a condition in a computer program that can occur when the true result of a floating point operation is smaller in magnitude (that is, closer to zero) than the smallest value representable as a normal floating point number in the target datatype.
Others seem to think underflow is the exact opposite of overflow but I never saw that definition.

musiKk
- 14,751
- 4
- 55
- 82