Examples for usage of all methods of 'java.lang.Math' with console output of example code.
Please click on method from below list to go to code example for usage of that method. Click [↓ Imports] to get import statements used in examples. To read javadoc of methods, click on [⿺ Javadoc] for that method.
double normalRemainder1 = -100 % 5;
double ieeeRemainder1 = Math.IEEEremainder(-100, 5);
System.out.println("-100 & 5 -> Normal = " + normalRemainder1 + " IEEE = " + ieeeRemainder1);
double ieeeRemainder2 = Math.IEEEremainder(100, 0);
System.out.println("100 & 0 -> IEEE = " + ieeeRemainder2);
// Normal remainder causes exception.
double normalRemainder2 = 100 % 0;
Output: -100 & 5 -> Normal = 0.0 IEEE = -0.0 100 & 0 -> IEEE = NaN java.lang.ArithmeticException: / by zero at com.itsallbinary.javadocexamples.examples.java_lang.MathExamples.test_IEEEremainder(MathExamples.java:20)
Tag: Example for IEEEremainder method of class java.lang.Math., Math IEEEremainder function example with arguments double arg0, double arg1, How to use IEEEremainder method of Math?, Usage of Math.IEEEremainder, Math.IEEEremainder() examples
System.out.println("123.456d abs = " + Math.abs(123.456d));
System.out.println("-123.456d abs = " + Math.abs(-123.456d));
System.out.println("-0d abs = " + Math.abs(-0d));
System.out.println("0d abs = " + Math.abs(0d));
System.out.println("NEGATIVE_INFINITY abs = " + Math.abs(Double.NEGATIVE_INFINITY));
System.out.println("POSITIVE_INFINITY abs = " + Math.abs(Double.POSITIVE_INFINITY));
Output: 123.456d abs = 123.456 -123.456d abs = 123.456 -0d abs = 0.0 0d abs = 0.0 NEGATIVE_INFINITY abs = Infinity POSITIVE_INFINITY abs = Infinity
Tag: Example for abs method of class java.lang.Math., Math abs function example with arguments double arg0, How to use abs method of Math?, Usage of Math.abs, Math.abs() examples
System.out.println("123 abs = " + Math.abs(123));
System.out.println("-123 abs = " + Math.abs(-123));
System.out.println("-0 abs = " + Math.abs(-0));
System.out.println("0 abs = " + Math.abs(0));
// In case of MIN_VALUE, same negative value will be returned.
System.out.println("MIN_VALUE abs = " + Math.abs(Integer.MIN_VALUE));
Output: 123 abs = 123 -123 abs = 123 -0 abs = 0 0 abs = 0 MIN_VALUE abs = -2147483648
Tag: Example for abs method of class java.lang.Math., Math abs function example with arguments int arg0, How to use abs method of Math?, Usage of Math.abs, Math.abs() examples
System.out.println("123.456l abs = " + Math.abs(123l));
System.out.println("-123.456l abs = " + Math.abs(-123l));
System.out.println("-0l abs = " + Math.abs(-0l));
System.out.println("0l abs = " + Math.abs(0l));
// In case of MIN_VALUE, same negative value will be returned.
System.out.println("MIN_VALUE abs = " + Math.abs(Long.MIN_VALUE));
Output: 123.456l abs = 123 -123.456l abs = 123 -0l abs = 0 0l abs = 0 MIN_VALUE abs = -9223372036854775808
Tag: Example for abs method of class java.lang.Math., Math abs function example with arguments long arg0, How to use abs method of Math?, Usage of Math.abs, Math.abs() examples
System.out.println("123.456f abs = " + Math.abs(123.456f));
System.out.println("-123.456f abs = " + Math.abs(-123.456f));
System.out.println("-0f abs = " + Math.abs(-0f));
System.out.println("0f abs = " + Math.abs(0f));
System.out.println("NEGATIVE_INFINITY abs = " + Math.abs(Float.NEGATIVE_INFINITY));
System.out.println("POSITIVE_INFINITY abs = " + Math.abs(Float.POSITIVE_INFINITY));
Output: 123.456f abs = 123.456 -123.456f abs = 123.456 -0f abs = 0.0 0f abs = 0.0 NEGATIVE_INFINITY abs = Infinity POSITIVE_INFINITY abs = Infinity
Tag: Example for abs method of class java.lang.Math., Math abs function example with arguments float arg0, How to use abs method of Math?, Usage of Math.abs, Math.abs() examples
long a = 10;
long b = 123456;
long c = -1000;
long d = Math.addExact(a, b);
System.out.println("Addition = " + d);
long e = Math.addExact(b, c);
System.out.println("Addition = " + e);
// This addition will overflow since we adding with max. Should cause exception.
Math.addExact(b, Long.MAX_VALUE);
Output: Addition = 123466 Addition = 122456 java.lang.ArithmeticException: long overflow at java.base/java.lang.Math.addExact(Math.java:845) at com.itsallbinary.javadocexamples.examples.java_lang.MathExamples.test_addExact_long_long(MathExamples.java:86)
Tag: Example for addExact method of class java.lang.Math., Math addExact function example with arguments long arg0, long arg1, How to use addExact method of Math?, Usage of Math.addExact, Math.addExact() examples
int a = 10;
int b = 123456;
int c = -1000;
int d = Math.addExact(a, b);
System.out.println("Addition = " + d);
int e = Math.addExact(b, c);
System.out.println("Addition = " + e);
// This addition will overflow since we adding with max. Should cause exception.
Math.addExact(b, Integer.MAX_VALUE);
Output: Addition = 123466 Addition = 122456 java.lang.ArithmeticException: integer overflow at java.base/java.lang.Math.addExact(Math.java:825) at com.itsallbinary.javadocexamples.examples.java_lang.MathExamples.test_addExact_int_int(MathExamples.java:104)
Tag: Example for addExact method of class java.lang.Math., Math addExact function example with arguments int arg0, int arg1, How to use addExact method of Math?, Usage of Math.addExact, Math.addExact() examples
Tag: Simple working examples of methods / functions of class java.lang.Math along with their console output, java.lang.Math tutorial., Guide to java.lang.Math & its methods., Understanding java.lang.Math with examples.