Mathematics in Java

6.9 Mathematics in Java

Most of what you need for math in Java is in the Math class. So, to access these methods, you prepend the name Math to the method. Most mathematics functions, such as sin, cos, exp, are written as Math.sin, Math.cos, Math.exp. The list below summarizes what is available.


e: Math.E
Returns a double representation of the base of the natural logarithm.
Pi: Math.PI
Returns a double representation of the ratio of the area of a circle to its diameter.
infinity: Double.POSITIVE_INFINITY
Returns a double representation of positive infinity.
-infinity: Double.NEGATIVE_INFINITY
Returns a double representation of negative infinity. "
not a number: Double.NaN
Returns a double representation of an IEEE NaN. The NaN is often generated during floating point calculations to signify an exceptional situation. For example, Math.sqrt(-1) will return a NaN, since Java does not know implicitly about complex numbers.


abs Math.abs(value)
Returns the absolute integer value of a.
arc cosine Math.acos(value)
Returns the arc cosine of a, in the range of 0.0 through Pi.
arc sine Math.asin(value)
Returns the arc sine of a, in the range of -Pi/2 through Pi/2.
arc tangent Math.atan(value)
Returns the arc tangent of a, in the range of -Pi/2 through Pi/2.
two argument arc tangent Math.atan2(x,y)
Returns the polar angle of the rectangular coordinates (x,y).
ceiling Math.ceil(value)
Returns the smallest whole number greater than or equal to a.
cosine Math.cos(value)
Returns the trigonometric cosine of an angle.
exp Math.exp(value)
Returns the exponential number e(2.718...) raised to the power of a.
floor Math.floor(value)
Returns the "floor" or largest whole number less than or equal to a.
logarithm Math.log(value)
Returns the natural logarithm (base e) of value. To get base 10 logarithm, use Math.log(value)/Math.log(10).
max Math.max(value_1,value_2)
Returns the greater of value_1 and value_2.
min Math.min(value_1,value_2)
Returns the lesser of value_1 and value_2.
power Math.pow(value_1,value_2)
Returns value_1 to the power of value_2.
random number Math.random()
Returns a pseudo-random number between 0.0 and 1.0.
round Math.round(value)
Rounds off a floating point value by first adding 0.5 to it and then returning the largest integer that is less than or equal to this new value.
sine Math.sin(value)
Returns the trigonometric sine of an angle.
sqare root Math.sqrt(value)
Returns the square root of a.
tangent Math.tan(value)
Returns the trigonometric tangent of an angle.
Next 6.10 do-while loop

David Maxwell, who is still writing this, would like to hear your comments and suggestions.