|Navigate Language Fundamentals topic: )|
java.lang.Math class allows for the use of many common mathematical functions that can be used while creating programs.
Since it is in the
java.lang package, the
Math class need not be imported. However, in programs extensively utilizing these functions, a static import can be used.
There are two constants in the
Math class that are fairly accurate approximations of irrational mathematical numbers.
Math.E constant, or the base of natural logarithms, represents the value of e, the base of the natural logarithms, about 2.718282.
|Code section 3.20:
Math.PI constant represents the value of pi, the ratio of a circle's circumference to its diameter, about 3.14159265.
|Code section 3.21:
There are several methods in the
Math class that deal with exponential functions.
method, a special case of
pow, returns e to the power of the parameter. In addition,
returns (ex - 1). Both of these methods are more accurate and convenient in these special cases.
Java has no general logarithm function; when needed this can be simulated using the change-of-base theorem.
Trigonometric and hyperbolic methods
The trigonometric methods of the
Math class allow users to easily deal with trigonometric functions in programs. All accept only
doubles. Please note that all values using these methods are initially passed and returned in radians, not degrees. However, conversions are possible.
The three main trigonometric methods are
Math.tan(x), which are used to find the sine, cosine, and tangent, respectively, of any given number. So, for example, a call to
Math.sin(Math.PI/2) would return a value of about 1. Although methods for finding the cosecant, secant, and cotangent are not available, these values can be found by taking the reciprocal of the sine, cosine, and tangent, respectively. For example, the cosecant of pi/2 could be found using
Inverse trigonometric functions
Java provides inverse counterparts to the trigonometric functions:
In addition, hyperbolic functions are available:
To convert between degree and radian measures of angles, two methods are available,
Math.toDegrees(x). While using
Math.toRadians(x), a degrees value must be passed in, and that value in radians (the degree value multiplied by pi/180) will be returned. The
Math.toDegrees(x) method takes in a value in radians and the value in degrees (the radian value multiplied by 180/pi) is returned.
The absolute value method of the
Math class is compatible with the
double types. The data returned is the absolute value of parameter (how far away it is from zero) in the same date type. For example:
|Code section 3.22:
In this example,
result will contain a value of 3.
Maximum and minimum values
These methods are very simple comparing functions. Instead of using
else statements, one can use the
Math.max(x1, x2) and
Math.min(x1, x2) methods. The
Math.max(x1, x2) simply returns the greater of the two values, while the
Math.min(x1, x2) returns the lesser of the two. Acceptable types for these methods include
Functions dealing with floating-point representation
Java 1.5 and 1.6 introduced several non-mathematical functions specific to the computer floating-point representation of numbers.
Math.copySign returns the value of the first argument with the sign of the second argument. It can be used to determine the sign of a zero value.
Math.getExponent returns (as an
int) the exponent used to scale the floating-point argument in computer representation.