1

I understand the overflows in java, but what is called underflows? and how can java handle it ?

3
  • 1
    Are you referring to integer underflow, or floating point underflow? Commented Apr 25, 2011 at 20:37
  • Duplicate stackoverflow.com/questions/2154712/… ? Commented Apr 25, 2011 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. Commented Apr 26, 2011 at 8:26

2 Answers 2

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.)

Sign up to request clarification or add additional context in comments.

8 Comments

To the best of my knowledge that's wrong (compare my answer). Could you cite sources for your claim?
how about the first result in Google for "integer underflow": 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.
i'd say subtracting one from MIN_VALuE is just overflow in the negative direction.
@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.
@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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.