3.5 ±0, ± , and NaN Program 3 C3 tabulates the results of the floating-point additive and multiplicative operations involving positive or negative 0, positive or negative infinity, Not-a-Number, and a "regular" floating-point values such as -1.0 and 1.0. See Listing 3-3a. Listing 3-3a Zero, infinity, and Not-a-Number.package numbercruncher.program3_3; import numbercruncher.mathutils.AlignRight; /** * PROGRAM 3-3: Zero, Infinity, and Not-a-Number * * Investigate the results of floating-point arithmetic * involving zero, infinity, and NaN. */ public class ZeroInfinityNaN { public static void main(String args[]) { AlignRight ar = new AlignRight(); float operands[] = { -0f, +0f, -1f, 1f, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, Float.NaN, }; ar.print("x", 10); ar.print("y", 10); ar.print("", 2); ar.print("x+y", 10); ar.print("x-y", 10); ar.print("x*y", 10); ar.print("x/y", 10); ar.print("x%y", 10); ar.underline(); for (int i = 0; i < operands.length; ++i) { for (int j = 0; j < operands.length; ++j) { float x = operands[i]; float y = operands[j]; ar.print(x, 10); ar.print(y, 10); ar.print("", 2); ar.print(x+y, 10); ar.print(x-y, 10); ar.print(x*y, 10); ar.print(x/y, 10); ar.print(x%y, 11); ar.println(); } } } } Output: x y x+y x-y x*y x/y x%y ------------------------------------------------------------------------ -0.0 -0.0 -0.0 0.0 0.0 NaN NaN -0.0 0.0 0.0 -0.0 -0.0 NaN NaN -0.0 -1.0 -1.0 1.0 0.0 0.0 -0.0 -0.0 1.0 1.0 -1.0 -0.0 -0.0 -0.0 -0.0 -Infinity -Infinity Infinity NaN 0.0 -0.0 -0.0 Infinity Infinity -Infinity NaN -0.0 -0.0 -0.0 NaN NaN NaN NaN NaN NaN 0.0 -0.0 0.0 0.0 -0.0 NaN NaN 0.0 0.0 0.0 0.0 0.0 NaN NaN 0.0 -1.0 -1.0 1.0 -0.0 -0.0 0.0 0.0 1.0 1.0 -1.0 0.0 0.0 0.0 0.0 -Infinity -Infinity Infinity NaN -0.0 0.0 0.0 Infinity Infinity -Infinity NaN 0.0 0.0 0.0 NaN NaN NaN NaN NaN NaN -1.0 -0.0 -1.0 -1.0 0.0 Infinity NaN -1.0 0.0 -1.0 -1.0 -0.0 -Infinity NaN -1.0 -1.0 -2.0 0.0 1.0 1.0 -0.0 -1.0 1.0 0.0 -2.0 -1.0 -1.0 -0.0 -1.0 -Infinity -Infinity Infinity Infinity 0.0 -1.0 -1.0 Infinity Infinity -Infinity -Infinity -0.0 -1.0 -1.0 NaN NaN NaN NaN NaN NaN 1.0 -0.0 1.0 1.0 -0.0 -Infinity NaN 1.0 0.0 1.0 1.0 0.0 Infinity NaN 1.0 -1.0 0.0 2.0 -1.0 -1.0 0.0 1.0 1.0 2.0 0.0 1.0 1.0 0.0 1.0 -Infinity -Infinity Infinity -Infinity -0.0 1.0 1.0 Infinity Infinity -Infinity Infinity 0.0 1.0 1.0 NaN NaN NaN NaN NaN NaN -Infinity -0.0 -Infinity -Infinity NaN Infinity NaN -Infinity 0.0 -Infinity -Infinity NaN -Infinity NaN -Infinity -1.0 -Infinity -Infinity Infinity Infinity NaN -Infinity 1.0 -Infinity -Infinity -Infinity -Infinity NaN -Infinity -Infinity -Infinity NaN Infinity NaN NaN -Infinity Infinity NaN -Infinity -Infinity NaN NaN -Infinity NaN NaN NaN NaN NaN NaN Infinity -0.0 Infinity Infinity NaN -Infinity NaN Infinity 0.0 Infinity Infinity NaN Infinity NaN Infinity -1.0 Infinity Infinity -Infinity -Infinity NaN Infinity 1.0 Infinity Infinity Infinity Infinity NaN Infinity -Infinity NaN Infinity -Infinity NaN NaN Infinity Infinity Infinity NaN Infinity NaN NaN Infinity NaN NaN NaN NaN NaN NaN NaN -0.0 NaN NaN NaN NaN NaN NaN 0.0 NaN NaN NaN NaN NaN NaN -1.0 NaN NaN NaN NaN NaN NaN 1.0 NaN NaN NaN NaN NaN NaN -Infinity NaN NaN NaN NaN NaN NaN Infinity NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN Program 3 C3 uses a useful class AlignRight in package numbercruncher. mathutils for printing text and numbers right-aligned in columns . The class is shown in Listing 3-3b. Listing 3-3b Utility class AlignRight for printing text and numbers right-aligned in columns.package numbercruncher.mathutils; /** * Print text and numbers right-aligned in columns. */ public class AlignRight { /** line size */ private int lineSize; /** * Constructor. */ public AlignRight() {} /** * Print text right-aligned in the column. * @param text the text to print * @param width the column width */ public void print(String text, int width) { int padding = width - text.length(); while (- padding >= 0) System.out.print(" "); System.out.print(text); lineSize += width; } /** * Print an integer value right-aligned in the column. * @param value the value to print * @param width the column width */ public void print(int value, int width) { print(Integer.toString(value), width); } /** * Print a float value right-aligned in the column. * @param value the value to print */ public void print(float value, int width) { print(Float.toString(value), width); } /** * Print a double value right-aligned in the column. * @param value the value to print * @param width the column width */ public void print(double value, int width) { print(Double.toString(value), width); } /** * Print a line. */ public void println() { System.out.println(); lineSize = 0; } /** * Print an underline. */ public void underline() { System.out.println(); for (int i = 0; i < lineSize; ++i) System.out.print("-"); System.out.println(); lineSize = 0; } } Some floating-point comparisons are a bit tricky. -0.0 is always equal to +0.0, and so the value of the relational expression - 0.0 < + 0.0 is false. - Infinity is not equal to + Infinity. Finally, NaN is never equal to another number, not even to another NaN. |
Top |