# 10.4 The 'Math' Class

The final class Math defines a set of static methods to support common mathematical functions, including functions for rounding numbers, performing trigonometry, generating pseudo random numbers, finding maximum and minimum of two numbers, calculating logarithms and exponentiation. The Math class cannot be instantiated. Only the class name Math can be used to invoke the static methods.

The final class Math provides constants to represent the value of e, the base of the natural logarithms, and the value p (pi), the ratio of the circumference of a circle to its diameter:

```Math.E
Math.PI
```

#### Miscellaneous Rounding Functions

```static int    abs(int i)
static long   abs(long l)
static float  abs(float f)
static double abs(double d)
```

The overloaded method abs() returns the absolute value of the argument. For a non-negative argument, the argument is returned. For a negative argument, the negation of the argument is returned.

```static int    min(int a, int b)
static long   min(long a, long b)
static float  min(float a, float b)
static double min(double a, double b)
```

The overloaded method min() returns the smaller of the two values a and b for any numeric type.

```static int    max(int a, int b)
static long   max(long a, long b)
static float  max(float a, float b)
static double max(double a, double b)
```

The overloaded method max() returns the greater of the two values a and b for any numeric type.

The following code illustrates the use of these methods from the Math class:

```long   ll = Math.abs(2010L);             // 2010L
double dd = Math.abs(-Math.PI);          // 3.141592653589793

double d1 = Math.min(Math.PI, Math.E);   // 2.718281828459045
long   m1 = Math.max(1984L, 2010L);      // 2010L
int    i1 = (int) Math.max(3.0, 4);      // Cast required.
```

Note the cast required in the last example. The method with the signature max(double, double) is executed, with implicit conversion of the int argument to a double. Since this method returns a double, it must be explicitly cast to an int.

```static double ceil(double d)
```

The method ceil() returns the smallest double value that is greater than or equal to the argument d, and is equal to a mathematical integer.

```static double floor(double d)
```

The method floor() returns the largest double value that is less than or equal to the argument d, and is equal to a mathematical integer.

```static int  round(float f)
static long round(double d)
```

The overloaded method round() returns the integer closest to the argument. This is equivalent to adding 0.5 to the argument, taking the floor of the result, and casting it to the return type. This is not the same as rounding to a specific number of decimal places, as the name of the method might suggest.

If the fractional part of a positive argument is less than 0.5, then the result returned is the same as Math.floor(). If the fractional part of a positive argument is greater than or equal to 0.5, then the result returned is the same as Math.ceil().

If the fractional part of a negative argument is less than or equal to 0.5, then the result returned is the same as Math.ceil(). If the fractional part of a negative argument is greater than 0.5, then the result returned is the same as Math.floor().

It is important to note the result obtained on negative arguments, keeping in mind that a negative number whose absolute value is less than that of another negative number, is actually greater than the other number (e.g., ?3.2 is greater than ?4.7). Compare also the results returned by these methods, shown in Table 10.1.

```double upPI    = Math.ceil(Math.PI);        // 4.0
double downPI  = Math.floor(Math.PI);       // 3.0
long   roundPI = Math.round(Math.PI);       // 3L

double upNegPI    = Math.ceil(-Math.PI);    // -3.0
double downNegPI  = Math.floor(-Math.PI);   // -4.0
long   roundNegPI = Math.round(-Math.PI);   // -3L
```

Argument:

7.0

7.1

7.2

7.3

7.4

7.5

7.6

7.7

7.8

7.9

8.0

ceil:

7.0

8.0

8.0

8.0

8.0

8.0

8.0

8.0

8.0

8.0

8.0

floor:

7.0

7.0

7.0

7.0

7.0

7.0

7.0

7.0

7.0

7.0

8.0

round:

7

7

7

7

7

8

8

8

8

8

8

Argument:

-7.0

-7.1

-7.2

-7.3

-7.4

-7.5

-7.6

-7.7

-7.8

-7.9

-8.0

ceil:

-7.0

-7.0

-7.0

-7.0

-7.0

-7.0

-7.0

-7.0

-7.0

-7.0

-8.0

floor:

-7.0

-8.0

-8.0

-8.0

-8.0

-8.0

-8.0

-8.0

-8.0

-8.0

-8.0

round:

-7

-7

-7

-7

-7

-7

-8

-8

-8

-8

-8

#### Exponential Functions

```static double pow(double d1, double d2)
```

The method pow() returns the value of d1 raised to the power of d2 (i.e., d1d2).

```static double exp(double d)
```

The method exp() returns the exponential number e raised to the power of d (i.e., ed).

```static double log(double d)
```

The method log() returns the natural logarithm (base e) of d (i.e., loged).

```static double sqrt(double d)
```

The method sqrt() returns the square root of d (i.e., d0.5). For a NaN or a negative argument, the result is a NaN (see Section 3.5, p. 52).

Some examples of exponential functions:

```double r = Math.pow(2.0, 4.0);           // 16.0
double v = Math.exp(2.0);                // 7.38905609893065
double l = Math.log(Math.E);             // 0.9999999999999981
double c = Math.sqrt(3.0*3.0 + 4.0*4.0); // 5.0
```

#### Trigonometry Functions

```static double sin(double d)
```

The method sin() returns the trigonometric sine of an angle d specified in radians.

```static double cos(double d)
```

The method cos() returns the trigonometric cosine of an angle d specified in radians.

```static double tan(double d)
```

The method tan() returns the trigonometric tangent of an angle d specified in radians.

```static double toRadians(double degrees)
```

The method toRadians() converts an angle in degrees to its approximation in radians.

```static double toDegrees(double radians)
```

The method toRadians() converts an angle in radians to its approximation in degrees.

Some examples of trigonometry functions:

```double deg1  = Math.toDegrees(Math.PI/4.0);  // 45 degrees
double deg2  = Math.toDegrees(Math.PI/2.0);  // 90 degrees

double r1 = Math.sin(Math.PI/2.0);           // 1.0
double r2 = Math.sin(Math.PI*2);             // -2.4492935982947064E-16 (0.0)

double r3 = Math.cos(Math.PI);               // -1.0
double r4 = Math.cos(Math.toRadians(360.0)); // 1.0

double r5 = Math.tan(Math.toRadians(90.0));  // 1.633123935319537E16 (infinity)
double r6 = Math.tan(Math.toRadians(45.0));  // 0.9999999999999999   (1.0)
```

Expected mathematical values are shown in parentheses.

#### Pseudorandom Number Generator

```static double random()
```

The method random() returns a random number greater than or equal to 0.0 and less than 1.0, where the value is selected randomly from the range according to a uniform distribution.

An example of using the pseudorandom number generator is as follows:

```for (int i = 0; i < 10; i++)
System.out.println((int)(Math.random()*10));   // int values in range [0 .. 9].
```

The loop will generate a run of ten pseudorandom integers between 0 (inclusive) and 10 (exclusive).

 List of Figures
 List of Tables
 List of Examples
 Foreword
 Preface
 Chapter 1. Basics of Java Programming
 Chapter 2. Language Fundamentals
 Chapter 3. Operators and Assignments
 Chapter 4. Declarations and Access Control
 Chapter 5. Control Flow, Exception Handling, and Assertions
 Chapter 6. Object-oriented Programming
 Chapter 7. Nested Classes And Interfaces