<h2>Java Floating-Point Arithmetic</h2> <p>An IEEE 754-1985 subset: <p><table> <tr><td valign=top>Underflows to zero: &nbsp;&nbsp; <td width=30>+0<br>&minus;0 <td valign=top> if decrementing to zero from the positive side<br> if incrementing to zero from the negative side <tr><td valign = top>Overflows to infinity:<td> +&infin;<br>&minus;&infin; <td valign=top> if incrementing upwards to infinity<br> if decrementing downwards to minus-infinity </table> <p align=justify> You can initialise a variable to infinity for example: <pre>float x = float.NEGATIVE_INFINITY;</pre> initialises the IEEE 32-bit floating-point variable <tt>x</tt> to minus-infinity. <p align=justify> A Java floating-point variable can also hold a value called <tt>NaN</tt> 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: <p align=center><table width=60% border> <tr><td>+&infin; + +&infin; &equiv; &infin;<td>+&infin; &minus; +&infin; &equiv; NaN <tr><td>&minus;&infin; + &minus;&infin; &equiv; &minus;&infin;<td>&minus;&infin; &minus; &minus;&infin; &equiv; NaN <tr><td>+&infin; + &minus;&infin; &equiv; NaN<td>+&infin; &minus; &minus;&infin; &equiv; &infin; <tr><td>x &divide; &plusmn;0 &equiv; &plusmn;&infin;<td>x % 0 &equiv; NaN <tr><td>x &divide; &plusmn;&infin; &equiv; &plusmn;0<td>x % &plusmn;&infin; &equiv; x <tr><td>0 &divide; 0 &equiv; NaN<td>0 % 0 &equiv; NaN <tr><td>&plusmn;&infin; &divide; x &equiv; &plusmn;&infin;<td>&plusmn;&infin; % x &equiv; NaN <tr><td>&plusmn;&infin; &divide; &plusmn;&infin; &equiv; NaN<td>&plusmn;&infin; % &plusmn;&infin; &equiv; NaN </table> <p align=justify> Java floating-point arithmetic does not throw exceptions like invalid operations, division by zero, overflow, underflow or inexact. Nor does it signal the occurrence of a <tt>NaN</tt> 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. <p align=justify> Also, you cannot use things like <tt>&lt;=</tt> or <tt>&gt;=</tt> in Java floating point. Eg you must use <tt>if(!(x &lt; y))</tt> instead of <tt>if(x &gt;= y)</tt>. <hr> &copy; 1998 Robert John Morton