Java Reference
In-Depth Information
Until J2SE 5.0, Java separated formatting from input and output operations.
Yo u first formatted numbers into a string and then printed the string or displayed
it graphically. In Section 5.11 we discuss formatting with the new tools in Java
5.0 that provide combined formatting/output capabilities similar to those of the
printf()
function in C.
2.12.2 Floating-point demo
As discussed in Section 2.11, you must deal with several aspects of floating-
point representations and operations when doing numerical computations. The
following code illustrates some of these floating-point issues:
// FP literals are double type by default.
// Append F or f to make float, or cast to float
float x = 5.1f;
float y = 0.0f;
float z = (float) 1.0;
float div
-
by
-
zero = x/y;
System.out.println ("Divide By Zero = x/y ="+
div
-
by
-
zero);
x
=
-1.0f;
div
-
by
-
zero
=
x/y;
System.out.println (
"
Divide negative value by zero
=
x/y
="
+ div
-
by
-
zero);
x
=
2.0e-45f;
y
=
1.0e-10f;
float positive
-
underflow
=
x*y;
System.out.println ("Positive underflow ="
+ positive
-
underflow);
x = -2.0e-45f;
y = 1.0e-10f;
float negative
-
underflow = x*y;
System.out.println ("Negative underflow ="
+ negative
-
underflow);
x = 1.0f;
y = negative
-
underflow;
float div
-
by
-
neg
-
zero = x/y;
System.out.println("Divide 1 by negative zero ="+
div
-
by
-
neg
-
zero + "
\
n");
x = 0.0f;
y = 0.0f;
float div
-
zero
-
by
-
zero
=
x/y;
System.out.println (
"
Divide zero by zero
="
+
div
-
zero
-
by
-
zero);
Search WWH ::
Custom Search