Mathematical functions
| Navigate Language Fundamentals topic: |
The 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.
Math constants
There are two constants in the Math class that are fairly accurate approximations of irrational mathematical numbers.
Math.E
The 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.E
|
Math.PI
The 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: Math.PI
|
Math methods
Exponential methods
There are several methods in the Math class that deal with exponential functions.
Exponentiation
The power method, , returns the first parameter to the power of the second parameter. For example, a call to double Math.pow(double, double)Math.pow(2, 10) will return a value of 1024.
The method, a special case of double Math.exp(double)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.double Math.expm1(double)
Java also provides special cases of the pow function for square roots and cube roots of doubles, and double Math.sqrt(double).double Math.cbrt(double)
Logarithms
Java has no general logarithm function; when needed this can be simulated using the change-of-base theorem.
returns the natural logarithm of the parameter (not the common logarithm, as its name suggests!).double Math.log(double)
returns the common (base-10) logarithm of the parameter.double Math.log10(double)
returns ln(parameter+1). It is recommended for small values.double Math.log1p(double)
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.
Trigonometric functions
The three main trigonometric methods are Math.sin(x), Math.cos(x), and 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 1/Math.sin(Math.PI/2).
Inverse trigonometric functions
Java provides inverse counterparts to the trigonometric functions: Math.asin(x), and Math.acos(x), Math.atan(x).
Hyperbolic functions
In addition, hyperbolic functions are available: Math.sinh(x), Math.cosh(x), and Math.tanh(x).
Radian/degree conversion
To convert between degree and radian measures of angles, two methods are available, Math.toRadians(x) and 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.
Absolute value: Math.abs
The absolute value method of the Math class is compatible with the int, long, float, and 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: Math.abs
|
In this example, result will contain a value of 3.
Maximum and minimum values
These methods are very simple comparing functions. Instead of using if...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 int, long, float, and double.
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.ulp(double) and Math.ulp(float) return an ulp, the smallest value which, when added to the argument, would be recognized as larger than the argument.
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.