Pages

Thursday, December 2, 2021

Java Absolute Value - Math.abs()

1. Overview

In this tutorial, We'll learn how to get the absolute value for the given number in java
Java API has the handy Math utility class to work with arithmetic and mathematical operations.



Math.abs() method take the number and return the absolute value for the given value. Absolute value means the positive number for the given number. Here, a given number can be a positive value or negative value or zero.

Math.abs() is used to get the positive number for the given value.

Java Absolute Value - Math.abs()



2. Math.abs() Syntax


This method is an overloaded method and each takes a different type of data.

Syntax
public static int abs(int a)
public static long abs(long a)
public static float abs(float a)
public static double abs(double a)

abs() method works with primitive number types such as int, long, float and double. Does not work for the char, byte, strings and boolean.


3. Java Absolute Value - Math.abs()


For int and long

abs() method returns the absolute value of an int/long value.

If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

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



For float and double

abs() method returns the absolute value of a primitive float/double value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. 

Special cases

If the argument is positive zero or negative zero, the result is positive zero.
If the argument is infinite, the result is positive infinity.
If the argument is NaN, the result is NaN.
In other words, the result is the same as the value of the expression.



4. Java Absolute Value Example - Integer/Long


Let us create simple examples using Math.abs() function with integer and long values.

Example 1

This example is on primitive int type values. We have passed positive, negative and 0 to the abs function.
It has returned all positive values as a result.

package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample1 {

	public static void main(String[] args) {

		// for integer primitive values
		int i = -100;
		int absValueofI = Math.abs(i);

		System.out.println("abs value of " + i + " is " + absValueofI);

		i = 200;
		absValueofI = Math.abs(i);

		System.out.println("abs value of " + i + " is " + absValueofI);

		i = -0;
		absValueofI = Math.abs(i);

		System.out.println("abs value of " + i + " is " + absValueofI);

	}
}

Output
abs value of -100 is 100
abs value of 200 is 200
abs value of 0 is 0

Example 2

For primitive long values.
package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample2 {

	public static void main(String[] args) {

		// for long primitive values
		long l = -100;
		long absValueofL = Math.abs(l);

		System.out.println("abs value of " + l + " is " + absValueofL);

		l = 200;
		absValueofL = Math.abs(l);

		System.out.println("abs value of " + l + " is " + absValueofL);

		l = -0;
		absValueofL = Math.abs(l);

		System.out.println("abs value of " + l + " is " + absValueofL);

	}
}

Output
abs value of -100 is 100
abs value of 200 is 200
abs value of 0 is 0


Example 3

For Wrapper Integer values.
package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample3 {

	public static void main(String[] args) {

		// for Wrapper Integer values
		Integer integer = -100;
		Integer absValueofInteger = Math.abs(integer);

		System.out.println("Integer - abs value of " + integer + " is " + absValueofInteger);

		integer = 200;
		absValueofInteger = Math.abs(integer);

		System.out.println("Integer - abs value of " + integer + " is " + absValueofInteger);

		integer = -0;
		absValueofInteger = Math.abs(integer);

		System.out.println("Integer - abs value of " + integer + " is " + absValueofInteger);
	}
}

Output
Integer - abs value of -100 is 100
Integer - abs value of 200 is 200
Integer - abs value of 0 is 0

Example 4

For wrapper Long values
package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample4 {

	public static void main(String[] args) {

		// for Wrapper Long values
		Long longValue = -100l;
		Long absValueofLong = Math.abs(longValue);

		System.out.println("Long - abs value of " + longValue + " is " + absValueofLong);

		longValue = 200l;
		absValueofLong = Math.abs(longValue);

		System.out.println("Long - abs value of " + longValue + " is " + absValueofLong);

		longValue = -0l;
		absValueofLong = Math.abs(longValue);

		System.out.println("Long - abs value of " + longValue + " is " + absValueofLong);
	}
}

Output
Long - abs value of -100 is 100
Long - abs value of 200 is 200
Long - abs value of 0 is 0


5. Java Absolute Value Example - Double/Float


Math.abs() method takes the arguments of double and float type.

Example 5

For Double primitive and wrapper types
package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample5 {

	public static void main(String[] args) {

		// for primitive Double values

		System.out.println("double primitive");
		double doublePrimitive = -100;
		Double absValueofDouble = Math.abs(doublePrimitive);

		System.out.println("primitive double - abs value of " + doublePrimitive + " is " + absValueofDouble);

		doublePrimitive = 200l;
		absValueofDouble = Math.abs(doublePrimitive);

		System.out.println("primitive double - abs value of " + doublePrimitive + " is " + absValueofDouble);

		doublePrimitive = -0l;
		absValueofDouble = Math.abs(doublePrimitive);

		System.out.println("primitive double - abs value of " + doublePrimitive + " is " + absValueofDouble);

		// for Wrapper Double values
		System.out.println("\ndouble wrapper");
		Double DoubleValue = Double.valueOf(-100);
		Double absValueofWrapperDouble = Math.abs(DoubleValue);

		System.out.println("Double - abs value of " + DoubleValue + " is " + absValueofWrapperDouble);

		DoubleValue = Double.valueOf(100);
		absValueofWrapperDouble = Math.abs(DoubleValue);

		System.out.println("Double - abs value of " + DoubleValue + " is " + absValueofWrapperDouble);

		DoubleValue = Double.valueOf(-0l);
		absValueofWrapperDouble = Math.abs(DoubleValue);

		System.out.println("Double - abs value of " + DoubleValue + " is " + absValueofWrapperDouble);
	}
}

Output
double primitive
primitive double - abs value of -100.0 is 100.0
primitive double - abs value of 200.0 is 200.0
primitive double - abs value of 0.0 is 0.0

double wrapper
Double - abs value of -100.0 is 100.0
Double - abs value of 100.0 is 100.0
Double - abs value of 0.0 is 0.0

Example 6


For float primitive and wrapper values.
package com.javaprogramto.programs.numbers.math;

public class MathAbsoluteExample6 {

	public static void main(String[] args) {

		// for primitive Float values

		System.out.println("Float primitive");
		Float FloatPrimitive = -100f;
		Float absValueofFloat = Math.abs(FloatPrimitive);

		System.out.println("primitive Float - abs value of " + FloatPrimitive + " is " + absValueofFloat);

		FloatPrimitive = 200f;
		absValueofFloat = Math.abs(FloatPrimitive);

		System.out.println("primitive Float - abs value of " + FloatPrimitive + " is " + absValueofFloat);

		FloatPrimitive = -0f;
		absValueofFloat = Math.abs(FloatPrimitive);

		System.out.println("primitive Float - abs value of " + FloatPrimitive + " is " + absValueofFloat);

		// for Wrapper Float values
		System.out.println("\nFloat wrapper");
		Float FloatValue = Float.valueOf(-100);
		Float absValueofWrapperFloat = Math.abs(FloatValue);

		System.out.println("Float - abs value of " + FloatValue + " is " + absValueofWrapperFloat);

		FloatValue = Float.valueOf(100);
		absValueofWrapperFloat = Math.abs(FloatValue);

		System.out.println("Float - abs value of " + FloatValue + " is " + absValueofWrapperFloat);

		FloatValue = Float.valueOf(-0l);
		absValueofWrapperFloat = Math.abs(FloatValue);

		System.out.println("Float - abs value of " + FloatValue + " is " + absValueofWrapperFloat);
	}
}

Output
Float primitive
primitive Float - abs value of -100.0 is 100.0
primitive Float - abs value of 200.0 is 200.0
primitive Float - abs value of -0.0 is 0.0

Float wrapper
Float - abs value of -100.0 is 100.0
Float - abs value of 100.0 is 100.0
Float - abs value of 0.0 is 0.0


6. Conclusion


In this article, We've seen how to get the absolute value for the given number in java. Math.abs() method takes long, float, int and double types and returns its absolute values.



No comments:

Post a Comment

Please do not add any spam links in the comments section.