Math class


Here is a Java program that demonstrates the use of various important methods from the Math class.

public class MathExample {
public static void main(String[] args) {

// Absolute value
int absoluteValue = Math.abs(-10);
System.out.println("Absolute Value of -10: " + absoluteValue);

// Maximum and Minimum
int max = Math.max(10, 20);
int min = Math.min(10, 20);
System.out.println("Maximum of 10 and 20: " + max);
System.out.println("Minimum of 10 and 20: " + min);

// Power
double power = Math.pow(2, 3); // 2^3
System.out.println("2 raised to the power of 3: " + power);

// Square Root
double squareRoot = Math.sqrt(16); //√16
System.out.println("Square Root of 16: " + squareRoot);

// Random number between 0.0 (inclusive) and 1.0 (exclusive)
double randomValue = Math.random();
System.out.println("Random value between 0.0 and 1.0: " + randomValue);

// Generate a random integer between 0 (inclusive) and 10 (exclusive)
int randomInt = (int) (Math.random() * 10);
System.out.println("Random integer between 0 and 10: " + randomInt);

// Ceiling, Floor, Rounding, and Rint
double number = 5.67;
double ceilingValue = Math.ceil(number);
double floorValue = Math.floor(number);
long roundedValue = Math.round(number);
double rintValue = Math.rint(number);
System.out.println("Ceiling of 5.67: " + ceilingValue);
System.out.println("Floor of 5.67: " + floorValue);
System.out.println("Rounded value of 5.67: " + roundedValue);
System.out.println("Rint value of 5.67: " + rintValue);

}
}

Absolute Value

  • Explanation: The absolute value of a number is its distance from zero, without considering whether it's positive or negative.
  • Code: Math.abs(-10) returns 10 because the absolute value of -10 is 10.

Maximum and Minimum

  • Explanation: The maximum function returns the larger of two numbers, while the minimum function returns the smaller.
  • Code:
    • Math.max(10, 20) returns 20 because 20 is larger.
    • Math.min(10, 20) returns 10 because 10 is smaller.

Power

  • Explanation: This function raises one number to the power of another.
  • Code: Math.pow(2, 3) calculates 2 to the power of 3 (2^3), which is 8.

Square Root

  • Explanation: The square root of a number is a value that, when multiplied by itself, gives the original number.
  • Code: Math.sqrt(16) returns 4 because 4 * 4 = 16.

Random Value (0.0 to 1.0)

  • Explanation: Generates a random decimal number between 0.0 (inclusive) and 1.0 (exclusive).
  • Code: Math.random() might return something like 0.54321.

Random Integer (0 to 10)

  • Explanation: Generates a random integer between 0 and 9.
  • Code: (int) (Math.random() * 10) multiplies the random decimal by 10 and converts it to an integer.

Ceiling, Floor, Rounding, and Rint

Explanation:
  • Math.ceil(5.67) finds the smallest integer greater than or equal to 5.67, which is 6.0.
  • Math.floor(5.67) finds the largest integer less than or equal to 5.67, which is 5.0.
  • Math.round(5.67) rounds 5.67 to the nearest long value, which is 6.
  • Math.rint(5.67) returns the nearest even double value to 5.67, which is 6.0.

All Math Methods

A list of all Math methods can be found in the table below:

MethodDescriptionReturn Type
abs(x)Returns the absolute value of xdouble|float|int|long
acos(x)Returns the arccosine of x, in radiansdouble
addExact(x, y)Returns the sum of x and yint|long
asin(x)Returns the arcsine of x, in radiansdouble
atan(x)Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radiansdouble
atan2(y,x)Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).double
cbrt(x)Returns the cube root of xdouble
ceil(x)Returns the value of x rounded up to its nearest integerdouble
copySign(x, y)Returns the first floating point x with the sign of the second floating point ydouble|float
cos(x)Returns the cosine of x (x is in radians)double
cosh(x)Returns the hyperbolic cosine of a double valuedouble
decrementExact(x)Returns x-1int|long
exp(x)Returns the value of Exdouble
expm1(x)Returns ex -1double
floor(x)Returns the value of x rounded down to its nearest integerdouble
floorDiv(x, y)Returns the division between x and y rounded downint|long
floorMod(x, y)Returns the remainder of a division between x and y where the result of the division was rounded downint|long
getExponent(x)Returns the unbiased exponent used in xint
hypot(x, y)Returns sqrt(x2 +y2) without intermediate overflow or underflowdouble
IEEEremainder(x, y)Computes the remainder operation on x and y as prescribed by the IEEE 754 standarddouble
incrementExact(x)Returns x+1int|double
log(x)Returns the natural logarithm (base E) of xdouble
log10(x)Returns the base 10 logarithm of xdouble
log1p(x)Returns the natural logarithm (base E) of the sum of x and 1double
max(x, y)Returns the number with the highest valuedouble|float|int|long
min(x, y)Returns the number with the lowest valuedouble|float|int|long
multiplyExact(x, y)Returns the result of x multiplied with yint|long
negateExact(x)Returns the negation of xint|long
nextAfter(x, y)Returns the floating point number adjacent to x in the direction of ydouble|float
nextDown(x)Returns the floating point value adjacent to x in the negative directiondouble|float
nextUp(x)Returns the floating point value adjacent to x in the direction of positive infinitydouble|float
pow(x, y)Returns the value of x to the power of ydouble
random()Returns a random number between 0 and 1double
rint(x)Returns the double value that is closest to x and equal to a mathematical integerdouble
round(x)Returns the value of x rounded to its nearest integerlong|int
scalb(x, y)Returns x multiplied by 2 to the power of ydouble|float
signum(x)Returns the sign of xdouble|float
sin(x)Returns the sine of x (x is in radians)double
sinh(x)Returns the hyperbolic sine of a double valuedouble
sqrt(x)Returns the square root of xdouble
subtractExact(x, y)Returns the result of x minus yint|long
tan(x)Returns the tangent of an angledouble
tanh(x)Returns the hyperbolic tangent of a double valuedouble
toDegrees(x)Converts an angle measured in radians to an approx. equivalent angle measured in degreesdouble
toIntExact(x)Converts a long value to an intint
toRadians(x)Converts an angle measured in degrees to an approx. angle measured in radiansdouble
ulp(x)Returns the size of the unit of least precision (ulp) of xdouble|float

Note: All Math methods are static.







Comments