I understand the overflows in java, but what is called underflows? and how can java handle it ?
-
1Are you referring to integer underflow, or floating point underflow?Justin– Justin2011-04-25 20:37:59 +00:00Commented Apr 25, 2011 at 20:37
-
Duplicate stackoverflow.com/questions/2154712/… ?DJClayworth– DJClayworth2011-04-25 20:38:49 +00:00Commented 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.user684934– user6849342011-04-26 08:26:53 +00:00Commented Apr 26, 2011 at 8:26
2 Answers
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.)
8 Comments
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.