Java Reference
In-Depth Information
Strict and non-strict math
Java supports two modes of floating point mathematics. One mode,called
strict is designed to ensure portability of Java programs. The methods that
provide strict mathematics are located in the StrictMath class of java.math.
The main purpose of Java strict math is to ensure that calculations pro-
duce identical results on any Java virtual machine. Non-strict arithmetic,
on the other hand,can be implemented with more relaxed rules. That is,
the designer of a Java virtual machine is allowed to use faster implemen-
tations for non-strict math functions,at the expense of producing slightly
different results on different platforms.
The programmer can select strict or non-strict modes by means of the
strictfp modifier. The modifier can be applied to a class,an interface,or a
method. When a method is declared with the strictfp modifier,all the
code is executed according to strict constraints. When used at the class
or interface level,all code in the class or interface is evaluated according
to strict math rules.
Programmers note:
The StrictMath class was introduced in Java version 1.3. Previous ver-
sions of the JDK generate an error if StrictMath is referenced in code.
If your code requires exact bit-for-bit results on all Java virtual ma-
chines,then you should use the strictfp modifier. Note that implementing
non-strict math is a developer's option. Therefore,it is possible that in a
particular Java virtual machine strict and non-strict routines produce the
same results.
At the application level strict math and non-strict math can be selected
by referencing the corresponding parent classes: Math for non-strict code
and StrictMath otherwise. For example,the following program calculates
the square of the constant e using strict math.
// Java for Engineers
// Filename: StrictE
// Reference: Chapter 23
// Description:
//
Using strict math in applications.
//
Calculatese*e
// Requires:
//
Keyin class in current directory
import java.lang.*;
Search WWH ::




Custom Search