Java Programming/Mathematical functions

The  class allows the use of many common mathematical functions that can be used while creating programs.

Since it is in the  package, the   class does not need to be imported. However, in programs extensively utilizing these functions, a static import can be used.

Math constants
There are two constants in the  class that are fairly accurate approximations of irrational mathematical numbers.

Math.E
The  constant represents the value of Euler's number (e), the base of the natural logarithm.

Math.PI
The  constant represents the value of pi, the ratio of a circle's circumference to its diameter.

Exponential methods
There are several methods in the  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  will return a value of 1024 (210).

The  method, a special case of , 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 also provides special cases of the pow function for square roots and cube roots of doubles,  and.

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!).

returns the common (base-10) logarithm of the parameter.

returns ln(parameter+1). It is recommended for small values.

Trigonometric and hyperbolic methods
The trigonometric methods of the  class allow users to easily deal with trigonometric functions in programs. All accept only s. 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,  , and  , which are used to find the sine, cosine, and tangent, respectively, of any given number. So, for example, a call to  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:, and  ,.

Hyperbolic functions
In addition, hyperbolic functions are available:,  , and.

Radian/degree conversion
To convert between degree and radian measures of angles, two methods are available,  and. While using, a degrees value must be passed in, and that value in radians (the degree value multiplied by pi/180) will be returned. The  method takes in a value in radians and the value in degrees (the radian value multiplied by 180/pi) is returned.

Absolute value:
The absolute value method of the  class is compatible with the, , , and  types. The data returned is the absolute value of parameter (how far away it is from zero) in the same data type. For example:

In this example,  will contain a value of 3.

Maximum and minimum values
These methods are very simple comparing functions. Instead of using ... statements, one can use the  and   methods. The  simply returns the greater of the two values, while the   returns the lesser of the two. Acceptable types for these methods include, , , and.

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.

and  return an ulp, the smallest value which, when added to the argument, would be recognized as larger than the argument.

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.

returns (as an ) 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.)

The MathsUtils class is like an addition to the  class and contains the rounding calculations.

The code is tested with the following JUnit test:

The output of the JUnit test follows:

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, I have to use OR instead of the   because of a bug in the source template.