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[edit]
There are two constants in the Math
class that are fairly accurate approximations of irrational mathematical numbers.
Math.E[edit]
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[edit]
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[edit]
Exponential methods[edit]
There are several methods in the Math
class that deal with exponential functions.
Exponentiation[edit]
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 double
s,
and double
Math.sqrt(double
)
.double
Math.cbrt(double
)
Logarithms[edit]
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[edit]
The trigonometric methods of the Math
class allow users to easily deal with trigonometric functions in programs. All accept only double
s. Please note that all values using these methods are initially passed and returned in radians, not degrees. However, conversions are possible.
Trigonometric functions[edit]
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[edit]
Java provides inverse counterparts to the trigonometric functions: Math.asin(x)
, and Math.acos(x)
, Math.atan(x)
.
Hyperbolic functions[edit]
In addition, hyperbolic functions are available: Math.sinh(x)
, Math.cosh(x)
, and Math.tanh(x)
.
Radian/degree conversion[edit]
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
[edit]
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 data type. For example:
![]() |
Code section 3.22: Math.abs
|
In this example, result
will contain a value of 3.
Maximum and minimum values[edit]
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[edit]
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.
Rounding number example[edit]
Sometimes, we are not only interested in mathematically correct rounded numbers, but we want that a fixed number of significant digits are always displayed, regardless of the number used. Here is an example program that returns always the correct string. You are invited to modify it such that it does the same and is simpler!
The constant class contains repeating constants that should exist only once in the code so that to avoid inadvertent changes. (If the one constant is changed inadvertently, it is most likely to be seen, as it is used at several locations.)
![]() |
Code listing 3.20: StringUtils.java
|
The MathsUtils class is like an addition to the java.lang.Math
class and contains the rounding calculations.
![]() |
Code listing 3.21: MathsUtils.java
|
The code is tested with the following JUnit test:
![]() |
Code listing 3.22: MathsUtilsTest.java
|
The output of the JUnit test follows:
![]() |
Output for code listing 3.22
MathsUtils.round(5, '.', 0.0) ==> 0.00000 = 0.00000 MathsUtils.round(5, '.', -1.4012984643248202E-45) ==> -1.4012E-45 = -1.4012E-45 MathsUtils.round(5, '.', 1.4012984643248202E-45) ==> 1.4013E-45 = 1.4013E-45 MathsUtils.round(5, '.', -1.999999757E-5) ==> -1.9999E-5 = -1.9999E-5 MathsUtils.round(5, '.', 1.999999757E-5) ==> 2.0000E-5 = 2.0000E-5 MathsUtils.round(5, '.', -1.999999757E-4) ==> -1.9999E-4 = -1.9999E-4 MathsUtils.round(5, '.', 1.999999757E-4) ==> 2.0000E-4 = 2.0000E-4 MathsUtils.round(5, '.', -0.001999999757) ==> -0.0019999 = -0.0019999 MathsUtils.round(5, '.', 0.001999999757) ==> 0.0020000 = 0.0020000 MathsUtils.round(5, '.', -6.40589E-4) ==> -6.4058E-4 = -6.4058E-4 MathsUtils.round(5, '.', 6.40589E-4) ==> 6.4059E-4 = 6.4059E-4 MathsUtils.round(5, '.', -0.3396899998188019) ==> -0.33968 = -0.33968 MathsUtils.round(5, '.', 0.3396899998188019) ==> 0.33969 = 0.33969 MathsUtils.round(5, '.', -0.34) ==> -0.33999 = -0.33999 MathsUtils.round(5, '.', 0.34) ==> 0.34000 = 0.34000 MathsUtils.round(5, '.', -7.07) ==> -7.0699 = -7.0699 MathsUtils.round(5, '.', 7.07) ==> 7.0700 = 7.0700 MathsUtils.round(5, '.', -118.188) ==> -118.18 = -118.18 MathsUtils.round(5, '.', 118.188) ==> 118.19 = 118.19 MathsUtils.round(5, '.', -118.2) ==> -118.19 = -118.19 MathsUtils.round(5, '.', 118.2) ==> 118.20 = 118.20 MathsUtils.round(5, '.', -123.405009) ==> -123.40 = -123.40 MathsUtils.round(5, '.', 123.405009) ==> 123.41 = 123.41 MathsUtils.round(5, '.', -30.76994323730469) ==> -30.769 = -30.769 MathsUtils.round(5, '.', 30.76994323730469) ==> 30.770 = 30.770 MathsUtils.round(5, '.', -130.7699432373047) ==> -130.76 = -130.76 MathsUtils.round(5, '.', 130.7699432373047) ==> 130.77 = 130.77 MathsUtils.round(5, '.', -540.0) ==> -539.99 = -539.99 MathsUtils.round(5, '.', 540.0) ==> 540.00 = 540.00 MathsUtils.round(5, '.', -12345.0) ==> -12344 = -12344 MathsUtils.round(5, '.', 12345.0) ==> 12345 = 12345 MathsUtils.round(5, '.', -123456.0) ==> -123450 = -123450 MathsUtils.round(5, '.', 123456.0) ==> 123460 = 123460 MathsUtils.round(5, '.', -540911.0) ==> -540900 = -540900 MathsUtils.round(5, '.', 540911.0) ==> 540910 = 540910 MathsUtils.round(5, '.', -9.223372036854776E56) ==> -9.2233E56 = -9.2233E56 MathsUtils.round(5, '.', 9.223372036854776E56) ==> 9.2234E56 = 9.2234E56 |
If you are interested in a comparison with C#, take a look at the rounding number example there. If you are interested in a comparison with C++, you can compare this code here with the same example over there.
Notice that in the expression starting with if ((D == 0)
, I have to use OR instead of the ||
because of a bug in the source template.