Java Reference
In-Depth Information
System.out.println(d1.equals(d2));
System.out.println(+0.0 == -0.0);
}
}
(
java FloatDoubleDemo
).Thefollowingoutputprovesthat
Float
's
equals()
method properly handles NaN and
Double
's
equals()
method properly handles
+/-0.0:
NaN
NaN
true
false
0.0
-0.0
false
true
Tip
If you want to test a
float
or
double
value for equality with +infinity
or -infinity (but not both), do not use
isInfinite()
. Instead, compare the value
with
NEGATIVE_INFINITY
or
POSITIVE_INFINITY
via
==
.Forexample,
f ==
Float.NEGATIVE_INFINITY
.
Youwillfind
parseFloat()
and
parseDouble()
usefulinmanycontexts.For
double
s.
Listing 4-6.
Parsing command-line arguments into double precision floating-point values
class Calc
{
public static void main(String[] args)
{
if (args.length != 3)
{
System.err.println("usage: java Calc value1 op
value2");