The Math Class – Selected API Classes

8.6 The Math Class

The final class java.lang.Math defines a set of static methods to support common mathematical functions, including functions for rounding numbers, finding the maximum and minimum of two numbers, calculating logarithms and exponentiation, performing exact arithmetic, generating pseudorandom numbers, and much more. The Math class is a utility class and cannot be instantiated.

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

Math.E
Math.PI

Miscellaneous Rounding Functions

Click here to view code image

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.

Click here to view code image

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.

Click here to view code image

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:

Click here to view code image

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

double d1 = Math.min(Math.PI, Math.E);   // 2.718281828459045
long   m1 = Math.max(1984L, 2022L);      // 2022L
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 in order to assign it to an int variable.

Click here to view code image

static double ceil(double d)

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument d, and is equal to a mathematical integer.

Click here to view code image

static double floor(double d)

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument d, and is equal to a mathematical integer. Note that Math.ceil(d) is exactly the value of -Math.floor(-d).

Click here to view code image

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 8.3, p. 480.

Click here to view code image

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

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

Table 8.3 Applying Rounding Functions

Leave a Reply

Your email address will not be published. Required fields are marked *