Java Math abs method
DevSniper
133 Views
public static int abs(int x) public static double abs(double x) public static float abs(float x) public static long abs(long x)
- Java Math.abs(arg) return absolute value of argument.
- Java Math.abs return positive value of the argument. argument can be positive or negative
- if argument is Infinity Math.abs() will return Positive Infinity.
- If the argument is NaN Math.abs will return NaN.
- If the argument is equal to the Integer.MIN_VALUE or Long.MIN_VALUE the most negative representable int value or long value. Java Math.abs will return that same value which is negative.
Example 1
public class Program{ public static void main(String []args){ int x=-5; int y=10; System.out.println("Math.abs(x) : "+Math.abs(x)); System.out.println("Math.abs(y) : "+Math.abs(y)); System.out.println("Math.abs(Integer.MIN_VALUE) : "+Math.abs(Integer.MIN_VALUE)); } }
Math.abs(x) : 5 Math.abs(y) : 10 Math.abs(Integer.MIN_VALUE) : -2147483648
Example 2
public class Program{ public static void main(String []args){ double x=-5.67; double y=10.45; System.out.println("Math.abs(x) : "+Math.abs(x)); System.out.println("Math.abs(y) : "+Math.abs(y)); System.out.println("Math.abs(15.7/0) : "+Math.abs(15.7/0)); System.out.println("Math.abs(15/2) : "+Math.abs(15/2)); System.out.println("Math.abs(15.0/2) : "+Math.abs(15.0/2)); System.out.println("Math.abs(15/2.0) : "+Math.abs(15/2.0)); System.out.println("Math.abs(14.0/2) : "+Math.abs(14.0/2)); System.out.println("Math.abs(14/2.0) : "+Math.abs(14/2.0)); } }
Math.abs(x) : 5.67 Math.abs(y) : 10.45 Math.abs(15.7/0) : Infinity Math.abs(15/2) : 7 Math.abs(15.0/2) : 7.5 Math.abs(15/2.0) : 7.5 Math.abs(14.0/2) : 7.0 Math.abs(14/2.0) : 7.0
Example 3
public class Program{ public static void main(String []args){ float x=-5.67f; float y=10.45f; System.out.println("Math.abs(x) : "+Math.abs(x)); System.out.println("Math.abs(y) : "+Math.abs(y)); System.out.println("Math.abs(15.7f/0) : "+Math.abs(15.7f/0)); System.out.println("Math.abs(15/0f) : "+Math.abs(15/0f)); System.out.println("Math.abs(15/2) : "+Math.abs(15/2)); System.out.println("Math.abs(15f/2) : "+Math.abs(15f/2)); System.out.println("Math.abs(15/2f) : "+Math.abs(15/2f)); System.out.println("Math.abs(14f/2) : "+Math.abs(14f/2)); System.out.println("Math.abs(14/2f) : "+Math.abs(14/2f)); } }
Math.abs(x) : 5.67 Math.abs(y) : 10.45 Math.abs(15.7f/0) : Infinity Math.abs(15/0f) : Infinity Math.abs(15/2) : 7 Math.abs(15f/2) : 7.5 Math.abs(15/2f) : 7.5 Math.abs(14f/2) : 7.0 Math.abs(14/2f) : 7.0
Example 4
public class Program{ public static void main(String []args){ long x=-9223372036854775L; long y=9223372036854775L; System.out.println("Math.abs(x) : "+Math.abs(x)); System.out.println("Math.abs(y) : "+Math.abs(y)); System.out.println("Math.abs(Long.MIN_VALUE) : "+Math.abs(Long.MIN_VALUE)); System.out.println("Math.abs(Long.MAX_VALUE) : "+Math.abs(Long.MAX_VALUE)); } }
Math.abs(x) : 9223372036854775 Math.abs(y) : 9223372036854775 Math.abs(Long.MIN_VALUE) : -9223372036854775808 Math.abs(Long.MAX_VALUE) : 9223372036854775807