Java Floating-Point Arithmetic
An IEEE 754-1985 subset:
|Underflows to zero:
if decrementing to zero from the positive side|
if incrementing to zero from the negative side
|Overflows to infinity:||
if incrementing upwards to infinity|
if decrementing downwards to minus-infinity
Note: ∞ is used to represent the infinity sign since the character sets available to most browsers don't have an infinity sign and it's too much trouble to put in a special gif image every time.
You can initialise a variable to infinity for example:
float x = float.NEGATIVE_INFINITY;
initialises the IEEE 32-bit floating-point variable x to minus-infinity.
A Java floating-point variable can also hold a value called NaN which stands for 'not a number'. It results when the outcome of a floating-point arithmetic operation is what mathematicians call 'indeterminate'. The behaviour of Java floating-point arithmetic in operations involving signed zeros and infinities is illustrated by the following identities:
+∞ + +∞ = ∞ +∞ - +∞ = NaN
-∞ + -∞ = -∞ -∞ - -∞ = NaN
+∞ + -∞ = NaN +∞ - -∞ = ∞
x / ±0 = ±∞ x % 0 = NaN
x / ±∞ = ±0 x % ±∞ = x
0 / 0 = NaN 0 % 0 = NaN
±∞ / x = ±∞ ±∞ % x = NaN
±∞ / ±∞ = NaN ±∞ % ±∞ = NaN
Java floating-point arithmetic does not throw exceptions like invalid operation, division by zero, overflow, underflow or inexact. Nor does it signal the occurrence of a NaN result. It just carries on by rounding results of operations towards zero or the nearest representable value, preferring values with the least-significant bit set to zero.
Also, you cannot use things like <= or >= in Java floating point. Eg you must use if(!(x < y)) instead of if(x >= y).
© 1998 Robert John Morton