Java Reference
In-Depth Information
import java.lang.*;
class WtComp
{
public static boolean equals(double v1, // First argument
double v2, // Second argument
double e) // Epsilon
{
return Math.abs(v1 - v2) < e;
}
public static void main(String[] args)
{
double a = 9.33333333333000;
double b = 9.33333333333333;
final double EPSILON = 1.0E-10;
if(equals(a, b, EPSILON))
System.out.println(“values are equal”);
else
System.out.println(“values are not equal”);
}
}
On the Web
The program WtComp.java is found in the Chapter 24 folder at
www.crcpress.com .
Bit-by-Bit operations
In Chapter 21 ( Table 21-2 and Figure 21-1 ) you saw how floating-point num -
bers are encoded in the IEEE 754 double and single formats. Java's double
and float types correspond bit-by-bit to IEEE 754 double and single basic
and double basic formats. The classes java.lang.Double and java.lang.Float
contain methods that allow inspecting and manipulating the individual bits
and bit fields of a floating-point number. Java bit encoding for double and
float formats is shown in Figure 24-1 .
The following methods in java.lang.Double relate to bit conversions of
floating-point values.
public static long doubleToLongBits(double value)
Returns the bit layout of the specified floating-point value according to
IEEE 754. Table 24-1 s hows the masks that can be used to select the individ-
ual bits and bit fields.
Search WWH ::




Custom Search