Math Class

The class Math contains methods for performing basic numeric operations such as elementary exponential, logarithm, square root, and trigonometric functions.

Constants

• Math.E (= 2.718) the base of natural logarithms
• Math.PI (= 3.14159) pi, the ratio of the circumference of a circle to its diameter

Exponent Methods

• Math.sqrt (double a): returns the square root of a
• Math.pow (double a, double b): returns a raised to the power b
• Math.exp (double a): returns e raised to the power a
• Math.log (double a): returns the natural logarithm of a

Rounding Methods

• Math.ceil (double a): rounded up to the nearest integer
• Math.floor (double a): rounded down to the nearest integer
• Math.round (double a): returns the closest long to a. The result is rounded to an integer by adding 0.5 and taking the floor of the result.
• Math.rint (double a): returns the double value that is closest to a and is equal to a mathematical integer. If two double values that are mathematical integers are equally close, the result is the integer value that is even

Service Methods

• Math.abs (type a): returns the absolute value of a
• Math.max (type a, type b): returns the maximum of a and b
• Math.min (type a, type b): returns the minimum of a and b
• Math.random(): returns a double that is greater than or equal to 0.0 and less than 1.0
The methods abs(), max(), and min() are overloaded. type could be any type - short, int, long, float, or double.

Trigonometric Methods

• Math.sin (double a): returns the sine of a expressed in radians
• Math.cos (double a): returns the cosine of a expressed in radians
• Math.tan (double a): returns the tangent of a expressed in radians
• Math.asin (double a): returns the arc sine of a
• Math.acos (double a): returns the arc cosine of a
• Math.atan (double a): returns the arc tangent of a

Recursion

A recursive method is a method that calls itself. An iterative method is a method that uses a loop to repeat an action. Anything that can be done iteratively can be done recursively, and vice versa. Iterative algorithms and methods are generally more efficient than recursive algorithms.

Recursion is based on two key problem solving concepts: divide and conquer and self-similarity. A recursive solution solves a problem by solving a smaller instance of the same problem. It solves this new problem by solving an even smaller instance of the same problem. Eventually, the new problem will be so small that its solution will either be obvious or known. This solution will lead to the solution of the original problem.

A recursive definition consists of two parts: a recursive part in which the nth value is defined in terms of the (n-1)th value, and a non recursive boundary case or base case which defines a limiting condition. An infinite repetition will result if a recursive definition is not properly bounded. In a recursive algorithm, each recursive call must make progress toward the bound, or base case. A recursion parameter is a parameter whose value is used to control the progress of the recursion towards its bound.

Procedure call and return in Java uses a last-in-first-out protocol. As each method call is made, a representation of the method call is place on the method call stack. When a method returns, its block is removed from the top of the stack.

Use an iterative algorithm instead of a recursive algorithm whenever efficiency and memory usage are important design factors. When all other factors are equal, choose the algorithm (recursive or iterative) that is easiest to understand, develop, and maintain.

Here is an example of a recursive method that calculates the factorial of n. The base case occurs when n is equal to 0. We know that 0! is equal to 1. Otherwise we use the relationship n! = n * ( n - 1 )!

```public static long fact ( int n )
{
if ( n == 0 )
return 1;
else
return n * fact ( n - 1 );
}
```