Java Reference

In-Depth Information

Table4-1
revealsawidevarietyofusefulmath-orientedmethods.Forexample,each

abs()
method returns its argument's
absolute value
(number without regard for sign).

abs(double)
and
abs(float)
are useful for comparing double precision

floating-pointandfloating-pointvaluessafely.Forexample,
0.3 == 0.1+0.1+0.1

evaluates to false because 0.1 has no exact representation. However, you can compare

these expressions with
abs()
and a tolerance value, which indicates an acceptable

range of error. For example,
Math.abs(0.3-(0.1+0.1+0.1)) < 0.1
returns

truebecausetheabsolutedifferencebetween
0.3
and
0.1+0.1+0.1
islessthana0.1

tolerance value.

demonstrated
Math
's
random()
,
sin()
,
cos()
, and
toRadians()
methods.