Thursday, 2 June 2022

Java math abs: Get the absolute value of a number

‘java.lang.Math’ class provides ‘abs’ method to get the absolute value of a number. ‘abs’ method is available in below overloaded forms.

 

public static double abs(double a)

public static float abs(float a)

public static long abs(long a)

public static int abs(int a)

 

‘abs’ method return the absolute value of a number. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.


 

AbsMethodDemo1.java

package com.sample.app;

import static java.lang.Math.abs;

public class AbsMethodDemo1 {

	public static void main(String[] args) {
		int a = 10;
		int b = -10;

		long c = 10l;
		long d = -10l;

		float e = 10.01f;
		float f = -10.01f;

		double g = 10.01;
		double h = -10.01;

		System.out.println("abs(" + a + ") : " + abs(a) + ", abs(" + b + ") : " + abs(b));
		System.out.println("abs(" + c + ") : " + abs(c) + ", abs(" + d + ") : " + abs(d));
		System.out.println("abs(" + e + ") : " + abs(e) + ", abs(" + f + ") : " + abs(f));
		System.out.println("abs(" + g + ") : " + abs(g) + ", abs(" + h + ") : " + abs(h));
	}

}

 

Output

abs(10) : 10, abs(-10) : 10
abs(10) : 10, abs(-10) : 10
abs(10.01) : 10.01, abs(-10.01) : 10.01
abs(10.01) : 10.01, abs(-10.01) : 10.01

 

Special cases for int and long

a. if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

 

b. If the argument is equal to the value of Long.MIN_VALUE, the most negative representable long value, the result is that same value, which is negative.

 

AbsMethodDemo2.java

package com.sample.app;

import static java.lang.Math.abs;

public class AbsMethodDemo2 {
	public static void main(String[] args) {
		int a = Integer.MIN_VALUE;
		long b = Long.MIN_VALUE;

		System.out.println("abs(" + a + ") : " + abs(a));
		System.out.println("abs(" + b + ") : " + abs(b));
	}

}

Output

abs(-2147483648) : -2147483648
abs(-9223372036854775808) : -9223372036854775808

Special cases for float and double

a. If the argument is positive zero or negative zero, the result is positive zero.

b. If the argument is infinite, the result is positive infinity.

c. If the argument is NaN, the result is NaN.

 

AbsMethodDemo3.java

package com.sample.app;

import static java.lang.Math.abs;

public class AbsMethodDemo3 {
	public static void main(String[] args) {
		float f1 = 0f;
		float f2 = -0f;
		float f3 = Float.POSITIVE_INFINITY;
		float f4 = Float.NEGATIVE_INFINITY;
		float f5 = Float.NaN;

		double d1 = 0;
		double d2 = -0;
		double d3 = Double.POSITIVE_INFINITY;
		double d4 = Double.NEGATIVE_INFINITY;
		double d5 = Double.NaN;

		System.out.println("abs(" + f1 + ") : " + abs(f1));
		System.out.println("abs(" + f2 + ") : " + abs(f2));
		System.out.println("abs(" + f3 + ") : " + abs(f3));
		System.out.println("abs(" + f4 + ") : " + abs(f4));
		System.out.println("abs(" + f5 + ") : " + abs(f5) +"\n");

		System.out.println("abs(" + d1 + ") : " + abs(d1));
		System.out.println("abs(" + d2 + ") : " + abs(d2));
		System.out.println("abs(" + d3 + ") : " + abs(d3));
		System.out.println("abs(" + d4 + ") : " + abs(d4));
		System.out.println("abs(" + d5 + ") : " + abs(d5));

	}

}

Output

abs(0.0) : 0.0
abs(-0.0) : 0.0
abs(Infinity) : Infinity
abs(-Infinity) : Infinity
abs(NaN) : NaN

abs(0.0) : 0.0
abs(0.0) : 0.0
abs(Infinity) : Infinity
abs(-Infinity) : Infinity
abs(NaN) : NaN

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment