Java Math Class

Math Class in Java

In Java, the Math class is a part of the java.lang package and provides a wide range of mathematical methods and constants for performing common mathematical operations. It is a static class, which means you don’t need to create an instance of it; you can access its methods and properties directly.

Key Features of the Math Class:

Static Methods: The Math class comprises static methods for executing mathematical operations like trigonometric functions, exponentiation, logarithms, and more.
Mathematical Constants: It provides essential mathematical constants such as π (pi) and the base of the natural logarithm.

Common Methods of the Math Class:

Rounding and Absolute Value: Utilize methods like Math.round(), Math.floor(), and Math.abs() for rounding numbers and obtaining absolute values.

1. Math.round():
Purpose: Rounds a floating-point number to the nearest integer.
Usage: It rounds 0.5 and above upwards, and below 0.5 downwards.

Example:

long roundedValue = Math.round(4.7); // Returns 5
long roundedValue2 = Math.round(4.3); // Returns 4

2. Math.floor():
Purpose: Rounds a floating-point number down to the nearest integer, regardless of the decimal value.
Usage: Always rounds downward, even if the decimal is above 0.5.

Example:

double floorValue = Math.floor(4.7); // Returns 4.0
double floorValue2 = Math.floor(4.3); // Returns 4.0

3. Math.abs():
Purpose: Returns the absolute (positive) value of a number, removing any negative sign.
Usage: Works with both integers and floating-point numbers.

Example:

int absValue = Math.abs(-5); // Returns 5
double absValue2 = Math.abs(-4.7); // Returns 4.7

  • Square Root: Calculate square roots using Math.sqrt().
  • Min and Max Values: Find the minimum and maximum of two values using Math.min() and Math.max().
  • Truncate Equivalent: In Java, you can use the Math.floor() method to remove the decimal part of a number. This method rounds the number down to the nearest whole number, even if the number is positive or negative. It always rounds toward negative infinity, meaning it rounds down.

Example

 

  1. For 10.3:
    • Math.floor(10.3) will return 10.0, because it removes the decimal part and rounds down.
  2. For 10.7:
    • Math.floor(10.7) will also return 10.0, because it rounds down to the nearest whole number.

Exponentiation: The Math.pow() method in Java is used to calculate a specified number raised to the power of another specified number.

Example:
1. Math.pow(2, 3): This means 2 raised to the power of 3, which is 23=2×2×2=82^3 = 2 \times 2 \times 2 = 823=2×2×2=8.
2. Math.pow(5, 2): This means 5 raised to the power of 2, which is 52=5×5=255^2 = 5 \times 5 = 2552=5×5=25.

Examples of Common Methods:

// Math.floor(): Rounds a number down to the nearest integer.
double weight = 10.75;
double floorWeight = Math.floor(weight);
System.out.println(floorWeight); // floorWeight is 10.0

// Math.max(): Returns the larger of two numbers.
double greaternumber = Math.max(10.5, 20.7);
System.out.println(greaternumber); // greaternumber is 20.7

// Math.min(): Returns the smaller of two numbers.
double smallernumber = Math.min(30.9, 15.3);
System.out.println(smallernumber); // smallernumber is 15.3

// Math.sqrt(): Calculates the square root of a number.
double squareRoot = Math.sqrt(64.0);
System.out.println(squareRoot); // squareRoot is 8.0

// Math.abs(): Returns the absolute (non-negative) value of a number.
double absWeight = Math.abs(-5.2);
System.out.println(absWeight); // absWeight is 5.2 the negative -5.2 is changes to positive value 5.2

// Math.round(): Rounds a number to the nearest integer.
long height = Math.round(7.6); if 7.4 show rule
System.out.println(height); // rounded number is 8

// Math.pow(): Raises a number to a specified power.
double result = Math.pow(2.0, 3.0); // 2^3 = 8
System.out.println(result); // result is 8.0

// Math.floor(): Removes the decimal part of a number (rounds towards negative infinity).
double truncatedHeight = Math.floor(8.9);
System.out.println(truncatedHeight); // truncatedHeight is 8.0

Tasks:
Write a program to round the following number to the nearest integer using Math.round.
Write a program to round the following number down to the nearest integer using Math.floor.
Write a program to get the absolute value of the following number using Math.abs.
Write a program to calculate the square root of the following number using Math.sqrt.
Write a program to find the maximum of the following two numbers using Math.max.
Write a program to find the minimum of the following two numbers using Math.min.
Write a program to remove the decimal part of the following number using Math.floor or Math.truncate.
Write a program to calculate 3 raised to the power of 4 using Math.pow.
Write a program that performs the following operations:
Rounds the number 5.8 to the nearest integer.
Finds the absolute value of -10.
Calculates the square root of 144.
Finds the maximum between 10 and 20.
Finds the minimum between 5 and 15.
Removes the decimal part of 9876.54321.
Raises 2 to the power of 5.

Course Video

YouTube Reference :