What is the absolute value function in Mathematics?

In mathematics, the absolute value of a number is equal to the positive value of the number passed.The absolute value function ignores the sign and returns the value without it. For example, the absolute of +5 is 5. Whereas, the absolute of -5 is also 5. Java Math abs() method - 1

What is Math.abs() method() in Java?

The java.lang.Math class provides a static method Math.abs(parameter) to find the “absolute value” of the parameter.
So, if you pass any positive number let’s say Math.abs(5) it will return 5. For a negative 5, Math.abs(-5) the result would be the same, i-e; 5.

Method Header


public static dataType abs(dataType parameter)

Allowed DataTypes

The abs() method of Java is overloaded for various data types. The allowed types are as under.
int float double long

Example 1


public class DriverClass {
    public static void main(String args[]) {
   
        int number = +5;
        // Print the original number
        System.out.println("Original Number = " + number);
 
        // Printing the absolute value
        // Calling the Math.abs() method
        System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number));
        
        
        number = -5;
        // Print the original number
        System.out.println("Original Number = " + number);
 
        // Printing the absolute value
        // Calling the Math.abs() method
        System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number));
        
    }
}

Output

Original Number = 5 Absolute Number = Math.abs( 5 ) = 5 Original Number = -5 Absolute Number = Math.abs( -5 ) = 5

Explanation

In the code snippet above, we have taken two numbers. The first number is a positive integer i.e. +5. The second number is a negative integer i.e. -5. We pass both the numbers to the Math.abs(number) method. The method returns 5 for both the inputs ignoring their respective signs.

Example 2


public class DriverClass {
    public static void main(String args[]) {
   
        int number = -0;
        System.out.println("Original Number = " + number);
        System.out.println("Math.abs( " + number + " ) = " + Math.abs(number) + "\n");
        
        long number1 = -4499990;
        System.out.println("Original Number = " + number1);
        System.out.println("Math.abs( " + number1 + " ) = " + Math.abs(number1) + "\n");
        
        float number2 = -92.45f;
        System.out.println("Original Number = " + number2);
        System.out.println("Math.abs( " + number2 + " ) = " + Math.abs(number2) + "\n");
        
        double number3 = -63.7777777777;
        System.out.println("Original Number = " + number3);
        System.out.println("Math.abs( " + number3 + " ) = " + Math.abs(number3) + "\n");
    }
}

Output

Original Number = 0 Math.abs( 0 ) = 0 Original Number = -4499990 Math.abs( -4499990 ) = 4499990 Original Number = -92.45 Math.abs( -92.45 ) = 92.45 Original Number = -63.7777777777 Math.abs( -63.7777777777 ) = 63.7777777777

Explanation

In the code above, we have taken double, long and float values in addition to integer as inputs for the Math.abs() method. We have passed all the respectives values to the Math.abs() method one by one and displayed the results on the console.

Boundary Cases

Here are some exceptional cases that you need to take care of while using Math.abs() method.

For int and long data types

If the argument is positive zero or negative zero, the result is positive zero.
Math.abs(+0) = 0 Math.abs(-0) = 0
For Integer.MIN_VALUE or Long.MIN_VALUE the output of Math.abs() is still the smallest integer or long which is negative.
Math.abs(Integer.MIN_VALUE) = -2147483648 Math.abs(Long.MIN_VALUE) = -9223372036854775808

For float and double data types

If the argument is infinite, the result is positive infinity.
Math.abs(Double.NEGATIVE_INFINITY) = Infinity
If the argument is NaN, the result is NaN.
Math.abs(Double.NaN) = NaN

Importing the Math Class and Using the abs() Function

In Java, the Math class is part of the java.lang package, which is automatically imported. Therefore, you can use the Math.abs() function directly without any explicit import statement. Here's a step-by-step guide to using the function:

Step 1: Understand the Math.abs() Function

The abs() function computes the absolute value of a number. It is overloaded to accept different data types: int, long, float, and double.

Step 2: Write a Simple Example

public class MathAbsExample {
    public static void main(String[] args) {
        int intValue = -5;
        double doubleValue = -4.7;

        // Using Math.abs() to find absolute values
        int absInt = Math.abs(intValue);
        double absDouble = Math.abs(doubleValue);

        System.out.println("Absolute value of -5: " + absInt); // Output: 5
        System.out.println("Absolute value of -4.7: " + absDouble); // Output: 4.7
    }
}

Output:

  • Absolute value of -5: 5
  • Absolute value of -4.7: 4.7

Broader Utility of the abs() Function in Solving Programming Problems

The Math.abs() function is widely used in various real-world programming scenarios. Here are some examples:

Example 1: Calculating Distance Between Two Points

The absolute value is often used to calculate the distance between two points on a number line:

public class DistanceCalculation {
    public static void main(String[] args) {
        int point1 = 3;
        int point2 = 10;

        // Calculate the distance
        int distance = Math.abs(point2 - point1);
        System.out.println("Distance between points: " + distance); // Output: 7
    }
}

Example 2: Handling Negative Values in Financial Applications

In financial calculations, absolute values can be used to handle negative amounts, such as balances or profits:

public class FinancialExample {
    public static void main(String[] args) {
        double profit = -2500.50;

        // Calculate the absolute profit
        double absProfit = Math.abs(profit);
        System.out.println("Absolute profit: $" + absProfit); // Output: $2500.5
    }
}

Example 3: Normalizing Values in Data Analysis

The abs() function is useful for normalizing values in data analysis to ensure consistency in calculations.

Exploring Other Mathematical Functions in the Math Class

The Math class offers a wide range of functions beyond abs(). Exploring these functions can expand your problem-solving toolkit. Some notable examples include:

  • Math.pow(double a, double b): Calculates a raised to the power of b.
  • Math.sqrt(double a): Computes the square root of a.
  • Math.max(double a, double b): Returns the larger of two values.
  • Math.min(double a, double b): Returns the smaller of two values.
  • Math.random(): Generates a random number between 0.0 and 1.0.

Example: Using Multiple Math Functions

public class MathFunctionExample {
    public static void main(String[] args) {
        double base = 2;
        double exponent = 3;

        double power = Math.pow(base, exponent);
        double squareRoot = Math.sqrt(16);
        double max = Math.max(10, 20);
        double min = Math.min(10, 20);

        System.out.println("2^3: " + power); // Output: 8.0
        System.out.println("Square root of 16: " + squareRoot); // Output: 4.0
        System.out.println("Max of 10 and 20: " + max); // Output: 20.0
        System.out.println("Min of 10 and 20: " + min); // Output: 10.0
    }
}

Conclusion

By the end of this post, you must be familiar with the Java Math.abs() method. You can use it on different numerical data types. You can come across multiple day-to-day applications of this method. As always, we encourage you to learn by practicing. Till then, keep learning and keep growing!