Java Reference
In-Depth Information
Table 3.1
Math Constants
Constant
Description
base used in natural logarithms (2.71828...)
E
ratio of circumference of a circle to its diameter (3.14159 ...)
PI
Notice that we passed a value of type int to Math.sqrt , but the header says that
it expects a value of type double . Remember that if Java is expecting a double and
gets an int , it converts the int into a corresponding double .
The Math class also defines two frequently used constants: e and
(see Table 3.1).
Following the Java convention, we use all uppercase letters for their names and refer
to them as Math.E and Math.PI .
Table 3.2 lists some of the most useful static methods from the Math class. You
can see a complete list of methods defined in the Math class by checking out the API
π
Table 3.2
Useful Static Methods in the Math Class
Method
Description
Example
absolute value
Math.abs(-308) returns 308
abs
ceiling (rounds upward)
Math.ceil(2.13) returns 3.0
ceil
cosine (radians)
Math.cos(Math.PI) returns -1.0
cos
exponent base e
Math.exp(1) returns
2.7182818284590455
exp
floor (rounds downward)
Math.floor(2.93) returns 2.0
floor
logarithm base e
Math.log(Math.E) returns 1.0
log
logarithm base 10
Math.log10(1000) returns 3.0
log10
maximum of two values
Math.max(45, 207) returns 207
max
minimum of two values
Math.min(3.8, 2.75) returns 2.75
min
power (general exponentiation)
Math.pow(3, 4) returns 81.0
pow
random value
Math.random() returns a random
double value k such that 0.0
random
k
1.0
round real number to nearest
Math.round(2.718) returns 3
round
integer
sine (radians)
Math.sin(0) returns 0.0
sin
square root
Math.sqrt(2) returns
1.4142135623730951
sqrt
converts from radians to degrees
toDegrees
Math.toDegrees(Math.PI)
returns 180.0
converts from degrees to radians
Math.toRadians(270.0) returns
4.71238898038469
toRadians
 
 
Search WWH ::




Custom Search