Math and StrictMath Classes


Math and StrictMath Classes

Much of the built-in math capability of Java can be found in the Math and StrictMath classes from the java.lang package. These two classes define a parallel set of methods that encompass absolute value, power, square root, exponential, logarithmic , trigonometric, minimum, maximum, and rounding operations as well as random number generation. They also define the constants e and p .

As you might guess from the class name , the methods in the StrictMath class, introduced in Java 1.3, are stricter in that they are required to produce the same results as certain published algorithms. These algorithms are found in the Freely Distributable Math Library ( fdlibm ) package and are written in C. The Java math libraries are defined according to the 1995 version of fdlibm . When the fdlibm defines more than one version of a function, Java uses the IEEE 754 core function version

The Math class defines higher performance versions of the methods found in the StrictMath class. The Math class methods do not guarantee bit-for-bit comparison with the fdlibm algorithms, but many of the Math class methods are implemented so they simply call the corresponding StrictMath version. One gauge of accuracy for floating point methods is a measure called units of last place (ulps). A method that is accurate to 0.5 ulps will always return the floating point number that is nearest to the exact result. Because they are written for higher performance, some of the Math class methods are accurate to 1 or 2 ulps. The Math class methods are sufficiently accurate for most applications.

The methods and constants in the Math and StrictMath classes are all public and static . They can be accessed by typing the class name, a period, and the constant or method name. Because they reside in the java.lang package, the Math and StrictMath members are available to every Java program. In the next several sections, we will look in more detail at the constants and methods in the Math and StrictMath classes.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net