Java Reference
In-Depth Information
π (3 . 141592 ... )
Math.PI
e (2 . 71828 ... )
Math.E
x (absolute value)
Math.abs(x)
(ceil function)
Math.round(x) ro u nd to nearest integer
Math.sqrt(x) x (square root function)
Math.log(x) ln x (log e x )
Math.log10(x) log 10 x
Math.pow(x,y) x y
x
Math.ceil(x)
(power function)
Math.cos(x)
cos x (cosine function)
Math.sin(x)
sin x (sine function)
tan x (tangent function)
Math.tan(x)
The program below demonstrates a more elaborate program for computing a
formula:
Program 1.5 Program demonstrating the use of mathematical functions
class MathFunction {
public static void main ( String [
]
args )
double x=Math . E ;
double fx=Math. log (x) ;
System . out . print ( "Is this precisely 1 or are there numerical
errors? " );
System . out . println ( fx ) ;
x=Math.PI/15.0;
fx=Math. sin (x)
Math . cos ( x ) ;
System . out . print ( "What about this trigonometric equality (
should be 1) ?" );
System . out . println ( fx ) ;
}
}
Math . s i n ( x )+Math . cos ( x )
The output of the program is:
Is this precisely 1 or are there numerical errors? 1.0
What about this trigonometric equality (should be 1) ?1.0000000000000002
Note that the constant Math.E is defined as the double value that is closer than
any other to e . Observe that in the second case, we should mathematically have
the identity sin 2 θ +cos 2 θ = 1, but due to numerical imprecisions, we end up
only with a very close number: 1 . 0000000000000002.
 
Search WWH ::




Custom Search