Java Reference
In-Depth Information
Solution
Use the trig functions in
java.lang.Math
. Like
java.lang.Math.random()
, all the meth-
ods of the
Math
class are static, so no
Math
instance is necessary. This makes sense because
none of these computations maintains any state. Note that the arguments for trigonometric
functions are in radians, not in degrees. Here is a program that computes one trigonometric
value and displays the values of
e
and
PI
that are available in the math library:
// Trig.java
System
.
out
.
println
(
"The cosine of 1.1418 is "
+
Math
.
cos
(
1.1418
));
System
.
out
.
println
(
"Java's PI is "
+
Math
.
PI
);
System
.
out
.
println
(
"Java's e is "
+
Math
.
E
);
The
java.lang.StrictMath
class is intended to perform the same operations as
java.lang.Math
but with greater cross-platform repeatability.
Taking Logarithms
Problem
You need to take the logarithm of a number.
Solution
For logarithms to base
e
, use
java.lang.Math
's
log()
function:
// Logarithm.java
double
double
someValue
;
// compute someValue...
double
double
log_e
=
Math
.
log
(
someValue
);
For logarithms to other bases, use the identity that: