<h2>Java Floating-Point Arithmetic</h2> An IEEE 754-1985 subset: <p> <table> <tr> <td valign = top>Underflows to zero: <td> <pre> +0 -0 </pre> <td><small> if decrementing to zero from the positive side<br> if incrementing to zero from the negative side </small> <tr><td valign = top>Overflows to infinity:<td> <pre> +&#8734; -&#8734; </pre> <td><small> if incrementing upwards to infinity<br> if decrementing downwards to minus-infinity </small></table> <p><small> Note: &#8734; 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. </small><p> 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> 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: <pre> +&#8734; + +&#8734; = &#8734; +&#8734; - +&#8734; = NaN -&#8734; + -&#8734; = -&#8734; -&#8734; - -&#8734; = NaN +&#8734; + -&#8734; = NaN +&#8734; - -&#8734; = &#8734; x / &plusmn;0 = &plusmn;&#8734; x % 0 = NaN x / &plusmn;&#8734; = &plusmn;0 x % &plusmn;&#8734; = x 0 / 0 = NaN 0 % 0 = NaN &plusmn;&#8734; / x = &plusmn;&#8734; &plusmn;&#8734; % x = NaN &plusmn;&#8734; / &plusmn;&#8734; = NaN &plusmn;&#8734; % &plusmn;&#8734; = NaN</pre> 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 <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> 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