|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 data 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.
Rounding number example
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: Common.java
The Maths class is like an addition to the
java.lang.Math class and contains the rounding calculations.
|Code listing 3.21: Maths.java
The code is tested with the following JUnit test:
|Code listing 3.22: MathsTest.java
The output of the JUnit test follows:
|Output for code listing 3.22
Maths.round(5, '.', 0.0) ==> 0.00000 = 0.00000 Maths.round(5, '.', -1.4012984643248202E-45) ==> -1.4012E-45 = -1.4012E-45 Maths.round(5, '.', 1.4012984643248202E-45) ==> 1.4013E-45 = 1.4013E-45 Maths.round(5, '.', -1.999999757E-5) ==> -1.9999E-5 = -1.9999E-5 Maths.round(5, '.', 1.999999757E-5) ==> 2.0000E-5 = 2.0000E-5 Maths.round(5, '.', -1.999999757E-4) ==> -1.9999E-4 = -1.9999E-4 Maths.round(5, '.', 1.999999757E-4) ==> 2.0000E-4 = 2.0000E-4 Maths.round(5, '.', -0.001999999757) ==> -0.0019999 = -0.0019999 Maths.round(5, '.', 0.001999999757) ==> 0.0020000 = 0.0020000 Maths.round(5, '.', -6.40589E-4) ==> -6.4058E-4 = -6.4058E-4 Maths.round(5, '.', 6.40589E-4) ==> 6.4059E-4 = 6.4059E-4 Maths.round(5, '.', -0.3396899998188019) ==> -0.33968 = -0.33968 Maths.round(5, '.', 0.3396899998188019) ==> 0.33969 = 0.33969 Maths.round(5, '.', -0.34) ==> -0.33999 = -0.33999 Maths.round(5, '.', 0.34) ==> 0.34000 = 0.34000 Maths.round(5, '.', -7.07) ==> -7.0699 = -7.0699 Maths.round(5, '.', 7.07) ==> 7.0700 = 7.0700 Maths.round(5, '.', -118.188) ==> -118.18 = -118.18 Maths.round(5, '.', 118.188) ==> 118.19 = 118.19 Maths.round(5, '.', -118.2) ==> -118.19 = -118.19 Maths.round(5, '.', 118.2) ==> 118.20 = 118.20 Maths.round(5, '.', -123.405009) ==> -123.40 = -123.40 Maths.round(5, '.', 123.405009) ==> 123.41 = 123.41 Maths.round(5, '.', -30.76994323730469) ==> -30.769 = -30.769 Maths.round(5, '.', 30.76994323730469) ==> 30.770 = 30.770 Maths.round(5, '.', -130.7699432373047) ==> -130.76 = -130.76 Maths.round(5, '.', 130.7699432373047) ==> 130.77 = 130.77 Maths.round(5, '.', -540.0) ==> -539.99 = -539.99 Maths.round(5, '.', 540.0) ==> 540.00 = 540.00 Maths.round(5, '.', -12345.0) ==> -12344 = -12344 Maths.round(5, '.', 12345.0) ==> 12345 = 12345 Maths.round(5, '.', -123456.0) ==> -123450 = -123450 Maths.round(5, '.', 123456.0) ==> 123460 = 123460 Maths.round(5, '.', -540911.0) ==> -540900 = -540900 Maths.round(5, '.', 540911.0) ==> 540910 = 540910 Maths.round(5, '.', -9.223372036854776E56) ==> -9.2233E56 = -9.2233E56 Maths.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.