Java Reference

In-Depth Information

• Because
StrictMath
cannot utilize platform-specific features such as an

extended-precision math coprocessor, an implementation of
StrictMath

might be less efficient than an implementation of
Math
.

Forthemostpart,
Math
'smethodscalltheir
StrictMath
counterparts.Twoexcep-

tions are
toDegrees()
and
toRadians()
. Although these methods have identic-

alcodebodiesinbothclasses,
StrictMath
'simplementationsincludereservedword

strictfp
in the method headers:

public static strictfp double toDegrees(double angrad)

public static strictfp double toRadians(double angdeg)

Wikipedia's“strictfp”entry(
http://en.wikipedia.org/wiki/Strictfp
)

mentionsthat
strictfp
restrictsfloating-pointcalculationstoensureportability.This

reservedwordaccomplishesportabilityinthecontextofintermediatefloating-pointrep-

resentationsandoverflows/underflows(generatingavaluetoolargeorsmalltofitarep-

resentation).

Note
The previously cited “strictfp” article states that
Math
contains
public

static strictfp double abs(double);
andother
strictfp
methods.If

youcheckoutthisclass'ssourcecodeunderJava7,youwillnotfind
strictfp
any-

where in the source code. However, many
Math
methods (such as
sin()
) call their

StrictMath
counterparts,whichareimplementedinaplatform-specificlibrary,and

the library's method implementations are strict.

Without
strictfp
,anintermediatecalculationisnotlimitedtotheIEEE75432-bit

and64-bitfloating-pointrepresentationsthatJavasupports.Instead,thecalculationcan

takeadvantageofalargerrepresentation(perhaps128bits)onaplatformthatsupports

this representation.

Anintermediatecalculationthatoverflows/underflowswhenitsvalueisrepresented

in32/64bitsmightnotoverflow/underflowwhenitsvalueisrepresentedinmorebits.

Becauseofthisdiscrepancy,portabilityiscompromised.
strictfp
levelstheplaying

field by requiring all platforms to use 32/64 bits for intermediate calculations.

When applied to a method,
strictfp
ensures that all floating-point calculations

performed in that method are in strict compliance. However,
strictfp
can be used

inaclassheaderdeclaration(asin
public strictfp class FourierTrans-

form
) to ensure that all floating-point calculations performed in that class are strict.

Note
Math
and
StrictMath
are declared
final
so that they cannot be exten-

ded.Also,theydeclare private empty noargument constructors sothat theycannot be