Java : Math with Examples
Math (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Math methods.
Summary
System.out.println(Math.sin(0.0)); // 0.0
System.out.println(Math.cos(0.0)); // 1.0
System.out.println(Math.absExact(123)); // 123
System.out.println(Math.absExact(-456)); // 456
System.out.println(Math.ceil(1.4)); // 2.0
System.out.println(Math.floor(1.6)); // 1.0
There is a StrictMath class which is similar to the Math class. The Math class permmits better-performing implementations where strict reproducibility is not required.
Fields
static final double E
// 2.718281828459045
System.out.println(Math.E);
static final double PI
// 3.141592653589793
System.out.println(Math.PI);
Methods
static double abs (double a)
System.out.println(Math.abs(0.0)); // 0.0
System.out.println(Math.abs(1.23)); // 1.23
System.out.println(Math.abs(-4.56)); // 4.56
System.out.println(Math.abs(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.abs(Double.NEGATIVE_INFINITY)); // Infinity
System.out.println(Math.abs(Double.NaN)); // NaN
static float abs (float a)
This method is equivalent except a type to abs(double a).
static int abs (int a)
Please see also : absExact
System.out.println(Math.abs(0)); // 0
System.out.println(Math.abs(123)); // 123
System.out.println(Math.abs(-456)); // 456
System.out.println(Math.abs(Integer.MAX_VALUE)); // 2147483647
// The method may returns a negative value.
System.out.println(Math.abs(Integer.MIN_VALUE)); // -2147483648
static long abs (long a)
This method is equivalent except a type to abs(int a).
static int absExact (int a)
System.out.println(Math.absExact(0)); // 0
System.out.println(Math.absExact(123)); // 123
System.out.println(Math.absExact(-456)); // 456
System.out.println(Math.absExact(Integer.MAX_VALUE)); // 2147483647
try {
final var value = Math.absExact(Integer.MIN_VALUE);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//ArithmeticException!
static long absExact (long a)
This method is equivalent except a type to absExact(int a).
static double acos (double a)
System.out.println(Math.acos(-1.0)); // 3.141592653589793 ( ≒ Math.PI )
System.out.println(Math.acos(0.0)); // 1.5707963267948966 ( ≒ Math.PI / 2 )
System.out.println(Math.acos(1.0)); // 0.0
System.out.println(Math.acos(Double.NaN)); // NaN
static int addExact (int x, int y)
System.out.println(Math.addExact(1, 2)); // 3
System.out.println(Math.addExact(1000, 3000)); // 4000
System.out.println(Math.addExact(1000, -10000)); // -9000
try {
System.out.println("MAX_VALUE : " + Integer.MAX_VALUE);
final var value = Math.addExact(Integer.MAX_VALUE, 1);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//MAX_VALUE : 2147483647
//ArithmeticException!
final int value = Integer.MAX_VALUE + 1;
System.out.println(value); // -2147483648
static long addExact (long x, long y)
This method is equivalent except a type to addExact(int x, int y).
static double asin (double a)
System.out.println(Math.asin(-1.0)); // -1.5707963267948966 ( ≒ -Math.PI / 2 )
System.out.println(Math.asin(0.0)); // 0.0
System.out.println(Math.asin(1.0)); // 1.5707963267948966 ( ≒ Math.PI / 2 )
System.out.println(Math.asin(Double.NaN)); // NaN
static double atan (double a)
System.out.println(Math.atan(Double.NEGATIVE_INFINITY)); // -1.5707963267948966 ( ≒ -Math.PI / 2 )
System.out.println(Math.atan(-1.0)); // -0.7853981633974483 ( ≒ -Math.PI / 4 )
System.out.println(Math.atan(0.0)); // 0.0
System.out.println(Math.atan(1.0)); // 0.7853981633974483 ( ≒ Math.PI / 4 )
System.out.println(Math.atan(Double.POSITIVE_INFINITY)); // 1.5707963267948966 ( ≒ Math.PI / 2 )
System.out.println(Math.atan(Double.NaN)); // NaN
static double atan2 (double y, double x)
System.out.println(Math.atan2(0.0, -1.0)); // 3.141592653589793 ( ≒ Math.PI )
System.out.println(Math.atan2(1.0, 0.0)); // 1.5707963267948966 ( ≒ Math.PI / 2 )
System.out.println(Math.atan2(0.0, 1.0)); // 0.0
System.out.println(Math.atan2(-1.0, 0.0)); // -1.5707963267948966 ( ≒ -Math.PI / 2 )
static double cbrt (double a)
System.out.println(Math.cbrt(0.0)); // 0.0
System.out.println(Math.cbrt(1.0)); // 1.0
System.out.println(Math.cbrt(8.0)); // 2.0
System.out.println(Math.cbrt(-27.0)); // -3.0
System.out.println(Math.cbrt(-0.0)); // -0.0
System.out.println(Math.cbrt(Double.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Math.cbrt(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.cbrt(Double.NaN)); // NaN
static double ceil (double a)
System.out.println(Math.ceil(0.0)); // 0.0
System.out.println(Math.ceil(1.2)); // 2.0
System.out.println(Math.ceil(1.8)); // 2.0
System.out.println(Math.ceil(-3.12)); // -3.0
System.out.println(Math.ceil(-3.89)); // -3.0
System.out.println(Math.ceil(-0.0)); // -0.0
System.out.println(Math.ceil(Double.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Math.ceil(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.ceil(Double.NaN)); // NaN
static int ceilDiv (int x, int y)
Please see also : ceilDivExact(int x, int y)
System.out.println(Math.ceilDiv(4, 3)); // 2
System.out.println(4 / 3); // 1
System.out.println(Math.ceilDiv(-4, 3)); // -1
System.out.println(-4 / 3); // -1
System.out.println(Math.ceilDiv(Integer.MIN_VALUE, -1) == Integer.MIN_VALUE); // true
static long ceilDiv (long x, int y)
This method is equivalent except a type to ceilDiv(int x, int y).
static long ceilDiv (long x, long y)
This method is equivalent except a type to ceilDiv(int x, int y).
static int ceilDivExact (int x, int y)
Please see also : ceilDiv(int x, int y)
System.out.println(Math.ceilDivExact(4, 3)); // 2
System.out.println(4 / 3); // 1
System.out.println(Math.ceilDivExact(-4, 3)); // -1
System.out.println(-4 / 3); // -1
try {
final var value = Math.ceilDivExact(Integer.MIN_VALUE, -1);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//ArithmeticException!
static long ceilDivExact (long x, long y)
This method is equivalent except a type to ceilDivExact(int x, int y).
static int ceilMod (int x, int y)
System.out.println(Math.ceilMod(4, 3)); // -2
System.out.println(4 % 3); // 1
System.out.println(Math.ceilMod(-4, 3)); // -1
System.out.println(-4 % 3); // -1
System.out.println(Math.ceilMod(4, -3)); // 1
System.out.println(4 % -3); // 1
System.out.println(Math.ceilMod(-4, -3)); // 2
System.out.println(-4 % -3); // -1
static int ceilMod (long x, int y)
This method is equivalent except a type to ceilMod(int x, int y).
static long ceilMod (long x, long y)
This method is equivalent except a type to ceilMod(int x, int y).
static double copySign (double magnitude, double sign)
System.out.println(Math.copySign(1.23, 4.56)); // 1.23
System.out.println(Math.copySign(1.23, -4.56)); // -1.23
System.out.println(Math.copySign(-7.89, 1.23)); // 7.89
System.out.println(Math.copySign(-7.89, -1.23)); // -7.89
System.out.println(Math.copySign(0.0, 0.0)); // 0.0
System.out.println(Math.copySign(0.0, -0.0)); // -0.0
System.out.println(Math.copySign(1.0, Double.NEGATIVE_INFINITY)); // -1.0
System.out.println(Math.copySign(1.0, Double.POSITIVE_INFINITY)); // 1.0
System.out.println(Math.copySign(1.0, Double.NaN)); // 1.0
static float copySign (float magnitude, float sign)
This method is equivalent except a type to copySign(double magnitude, double sign).
static double cos (double a)
System.out.println(Math.cos(-Math.PI)); // -1.0
System.out.println(Math.cos(-Math.PI * 0.75)); // -0.7071067811865475
System.out.println(Math.cos(-Math.PI * 0.5)); // 6.123233995736766E-17 (≒ 0.0)
System.out.println(Math.cos(-Math.PI * 0.25)); // 0.7071067811865476
System.out.println(Math.cos(0.0)); // 1.0
System.out.println(Math.cos(Math.PI * 0.25)); // 0.7071067811865476
System.out.println(Math.cos(Math.PI * 0.5)); // 6.123233995736766E-17 (≒ 0.0)
System.out.println(Math.cos(Math.PI * 0.75)); // -0.7071067811865475
System.out.println(Math.cos(Math.PI)); // -1.0
System.out.println(Math.cos(Double.NEGATIVE_INFINITY)); // NaN
System.out.println(Math.cos(Double.POSITIVE_INFINITY)); // NaN
System.out.println(Math.cos(Double.NaN)); // NaN
static double cosh (double x)
System.out.println(Math.cosh(-1.0)); // 1.543080634815244
System.out.println(Math.cosh(-0.5)); // 1.1276259652063807
System.out.println(Math.cosh(0.0)); // 1.0
System.out.println(Math.cosh(0.5)); // 1.543080634815244
System.out.println(Math.cosh(1.0)); // -1.0
System.out.println(Math.cosh(Double.NEGATIVE_INFINITY)); // Infinity
System.out.println(Math.cosh(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.cosh(Double.NaN)); // NaN
static int decrementExact (int a)
System.out.println(Math.decrementExact(Integer.MAX_VALUE)); // 2147483646
System.out.println(Math.decrementExact(100)); // 99
System.out.println(Math.decrementExact(1)); // 0
System.out.println(Math.decrementExact(0)); // -1
System.out.println(Math.decrementExact(-100)); // -101
try {
System.out.println("MIN_VALUE : " + Integer.MIN_VALUE);
final int value = Math.decrementExact(Integer.MIN_VALUE);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//MIN_VALUE : -2147483648
//ArithmeticException!
final int value = Integer.MIN_VALUE - 1;
System.out.println(value); // 2147483647
static long decrementExact (long a)
This method is equivalent except a type to decrementExact(int a).
static int divideExact (int x, int y)
System.out.println(Math.divideExact(100, 2)); // 50
System.out.println(Math.divideExact(10, 3)); // 3
System.out.println(Math.divideExact(-500, 10)); // -50
System.out.println(Math.divideExact(-1000, -500)); // 2
try {
System.out.println("MIN_VALUE : " + Integer.MIN_VALUE);
final int value = Math.divideExact(Integer.MIN_VALUE, -1);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//MIN_VALUE : -2147483648
//ArithmeticException!
static long divideExact (long x, long y)
This method is equivalent except a type to divideExact(int x, int y).
static double exp (double a)
System.out.println(Math.exp(-2.0)); // 0.1353352832366127
System.out.println(Math.exp(-1.0)); // 0.36787944117144233
System.out.println(Math.exp(0.0)); // 1.0
System.out.println(Math.exp(1.0)); // 2.718281828459045
System.out.println(Math.exp(2.0)); // 7.38905609893065
System.out.println(Math.exp(Double.NEGATIVE_INFINITY)); // 0.0
System.out.println(Math.exp(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.exp(Double.NaN)); // NaN
static double expm1 (double x)
System.out.println(Math.expm1(-2.0)); // -0.8646647167633873
System.out.println(Math.expm1(-1.0)); // -0.6321205588285577
System.out.println(Math.expm1(0.0)); // 0.0
System.out.println(Math.expm1(1.0)); // 1.718281828459045
System.out.println(Math.expm1(2.0)); // 6.38905609893065
System.out.println(Math.expm1(Double.NEGATIVE_INFINITY)); // -1.0
System.out.println(Math.expm1(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.expm1(Double.NaN)); // NaN
static double floor (double a)
System.out.println(Math.floor(1.2)); // 1.0
System.out.println(Math.floor(1.8)); // 1.0
System.out.println(Math.floor(2.3)); // 2.0
System.out.println(Math.floor(2.9)); // 2.0
System.out.println(Math.floor(-3.12)); // -4.0
System.out.println(Math.floor(-3.89)); // -4.0
System.out.println(Math.floor(0.0)); // 0.0
System.out.println(Math.floor(-0.0)); // -0.0
System.out.println(Math.floor(Double.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Math.floor(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.floor(Double.NaN)); // NaN
static int floorDiv (int x, int y)
Please see also : floorDivExact(int x, int y)
System.out.println(Math.floorDiv(10, 1)); // 10
System.out.println(Math.floorDiv(10, 2)); // 5
System.out.println(Math.floorDiv(10, 3)); // 3
System.out.println(Math.floorDiv(10, 4)); // 2
System.out.println(Math.floorDiv(10, 5)); // 2
System.out.println(Math.floorDiv(10, 6)); // 1
System.out.println(Math.floorDiv(10, 10)); // 1
System.out.println(Math.floorDiv(10, 11)); // 0
System.out.println(Math.floorDiv(10, -1)); // -10
System.out.println(Math.floorDiv(10, -2)); // -5
System.out.println(Math.floorDiv(10, -3)); // -4
System.out.println(Math.floorDiv(10, -4)); // -3
System.out.println(Math.floorDiv(10, -5)); // -2
System.out.println(Math.floorDiv(10, -6)); // -2
System.out.println(Math.floorDiv(10, -10)); // -1
System.out.println(Math.floorDiv(10, -11)); // -1
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Math.floorDiv(Integer.MIN_VALUE, -1)); // -2147483648
static long floorDiv (long x, int y)
This method is equivalent except a type to floorDiv(int x, int y).
static long floorDiv (long x, long y)
This method is equivalent except a type to floorDiv(int x, int y).
static int floorDivExact (int x, int y)
Please see also : floorDiv(int x, int y)
System.out.println(Math.floorDivExact(10, 1)); // 10
System.out.println(Math.floorDivExact(10, 2)); // 5
System.out.println(Math.floorDivExact(10, 3)); // 3
System.out.println(Math.floorDivExact(10, -1)); // -10
System.out.println(Math.floorDivExact(10, -2)); // -5
System.out.println(Math.floorDivExact(10, -3)); // -4
try {
System.out.println("MIN_VALUE : " + Integer.MIN_VALUE);
final var value = Math.floorDivExact(Integer.MIN_VALUE, -1);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//MIN_VALUE : -2147483648
//ArithmeticException!
static long floorDivExact (long x, long y)
This method is equivalent except a type to floorDivExact(int x, int y).
static int floorMod (int x, int y)
System.out.println(Math.floorMod(10, 1)); // 0
System.out.println(Math.floorMod(10, 2)); // 0
System.out.println(Math.floorMod(10, 3)); // 1
System.out.println(Math.floorMod(10, 4)); // 2
System.out.println(Math.floorMod(10, 5)); // 0
System.out.println(Math.floorMod(10, 6)); // 4
System.out.println(Math.floorMod(10, 7)); // 3
System.out.println(Math.floorMod(10, 10)); // 0
System.out.println(Math.floorMod(10, 11)); // 10
System.out.println(Math.floorMod(10, -1)); // 0
System.out.println(Math.floorMod(10, -2)); // 0
System.out.println(Math.floorMod(10, -3)); // -2
System.out.println(Math.floorMod(10, -4)); // -2
System.out.println(Math.floorMod(10, -5)); // 0
System.out.println(Math.floorMod(10, -6)); // -2
System.out.println(Math.floorMod(10, -7)); // -4
System.out.println(Math.floorMod(10, -10)); // 0
System.out.println(Math.floorMod(10, -11)); // -1
System.out.println(Math.floorMod(-10, 1)); // 0
System.out.println(Math.floorMod(-10, 2)); // 0
System.out.println(Math.floorMod(-10, 3)); // 2
System.out.println(Math.floorMod(-10, 4)); // 2
System.out.println(Math.floorMod(-10, 5)); // 0
System.out.println(Math.floorMod(-10, 6)); // 2
System.out.println(Math.floorMod(-10, 7)); // 4
System.out.println(Math.floorMod(-10, 10)); // 0
System.out.println(Math.floorMod(-10, 11)); // 1
static int floorMod (long x, int y)
This method is equivalent except a type to floorMod(int x, int y).
static long floorMod (long x, long y)
This method is equivalent except a type to floorMod(int x, int y).
static double fma (double a, double b, double c)
final double a = 0.6;
final double b = 3.0;
final double c = 0.9;
System.out.println(Math.fma(a, b, c)); // 2.7
System.out.println(a * b + c); // 2.6999999999999997
System.out.println(Math.fma(Double.NaN, 1.0, 2.0)); // NaN
System.out.println(Math.fma(Double.POSITIVE_INFINITY, 0.0, 2.0)); // NaN
System.out.println(Math.fma(Double.POSITIVE_INFINITY, 1.0, Double.NEGATIVE_INFINITY)); // NaN
static float fma (float a, float b, float c)
This method is equivalent except a type to fma(double a, double b, double c).
static int getExponent (double d)
final double value1 = 0x1.0p1;
System.out.println(value1); // 2.0
System.out.println(Math.getExponent(value1)); // 1
final double value2 = 0x1.0p2;
System.out.println(value2); // 4.0
System.out.println(Math.getExponent(value2)); // 2
final double value3 = 0x1.0p3;
System.out.println(value3); // 8.0
System.out.println(Math.getExponent(value3)); // 3
System.out.println(Double.MAX_EXPONENT); // 1023
System.out.println(Math.getExponent(Double.NaN)); // 1024
System.out.println(Math.getExponent(Double.POSITIVE_INFINITY)); // 1024
System.out.println(Math.getExponent(Double.NEGATIVE_INFINITY)); // 1024
System.out.println(Double.MIN_EXPONENT); // -1022
System.out.println(Math.getExponent(0.0)); // -1023
static int getExponent (float f)
This method is equivalent except a type to getExponent(double d).
static double hypot (double x, double y)
System.out.println(Math.hypot(1.0, 0.0)); // 1.0
System.out.println(Math.hypot(3.0, 4.0)); // 5.0
final double x = Double.MAX_VALUE;
final double y = Double.MIN_VALUE;
System.out.println(Math.hypot(x, y)); // 1.7976931348623157E308
System.out.println(Math.sqrt((x * x) + (y * y))); // Infinity
System.out.println(Math.hypot(Double.NEGATIVE_INFINITY, 1.0)); // Infinity
System.out.println(Math.hypot(Double.POSITIVE_INFINITY, 1.0)); // Infinity
System.out.println(Math.hypot(Double.NaN, 1.0)); // NaN
static double IEEEremainder (double f1, double f2)
System.out.println(Math.IEEEremainder(10.0, 1.0)); // 0.0
System.out.println(Math.IEEEremainder(10.0, 2.0)); // 0.0
System.out.println(Math.IEEEremainder(10.0, 3.0)); // 1.0
System.out.println(Math.IEEEremainder(10.0, 4.0)); // 2.0
System.out.println(Math.IEEEremainder(10.0, 5.0)); // 0.0
System.out.println(Math.IEEEremainder(10.0, 6.0)); // -2.0
System.out.println(Math.IEEEremainder(10.0, 7.0)); // 3.0
System.out.println(Math.IEEEremainder(10.0, 8.0)); // 2.0
System.out.println(Math.IEEEremainder(10.0, 9.0)); // 1.0
System.out.println(Math.IEEEremainder(10.0, 10.0)); // 0.0
System.out.println(Math.IEEEremainder(10.0, 11.0)); // -1.0
System.out.println(10.0 % 1.0); // 0.0
System.out.println(10.0 % 2.0); // 0.0
System.out.println(10.0 % 3.0); // 1.0
System.out.println(10.0 % 4.0); // 2.0
System.out.println(10.0 % 5.0); // 0.0
System.out.println(10.0 % 6.0); // 4.0
System.out.println(10.0 % 7.0); // 3.0
System.out.println(10.0 % 8.0); // 2.0
System.out.println(10.0 % 9.0); // 1.0
System.out.println(10.0 % 10.0); // 0.0
System.out.println(10.0 % 11.0); // 10.0
System.out.println(Math.IEEEremainder(10.0, 0.0)); // NaN
System.out.println(Math.IEEEremainder(Double.POSITIVE_INFINITY, 1.0)); // NaN
System.out.println(Math.IEEEremainder(Double.NEGATIVE_INFINITY, 1.0)); // NaN
System.out.println(Math.IEEEremainder(Double.NaN, 1.0)); // NaN
System.out.println(Math.IEEEremainder(2.0, Double.POSITIVE_INFINITY)); // 2.0
System.out.println(Math.IEEEremainder(2.0, Double.NEGATIVE_INFINITY)); // 2.0
static int incrementExact (int a)
System.out.println(Math.incrementExact(0)); // 1
System.out.println(Math.incrementExact(1)); // 2
System.out.println(Math.incrementExact(1000)); // 1001
System.out.println(Math.incrementExact(-1000)); // -999
try {
System.out.println("MAX_VALUE : " + Integer.MAX_VALUE);
final var value = Math.incrementExact(Integer.MAX_VALUE);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//MAX_VALUE : 2147483647
//ArithmeticException!
final int value = Integer.MAX_VALUE + 1;
System.out.println(value); // -2147483648
static long incrementExact (long a)
This method is equivalent except a type to incrementExact(int a).
static double log (double a)
System.out.println(Math.log(0.1)); // -2.3025850929940455
System.out.println(Math.log(0.2)); // -1.6094379124341003
System.out.println(Math.log(0.3)); // -1.2039728043259361
System.out.println(Math.log(0.4)); // -0.916290731874155
System.out.println(Math.log(0.5)); // -0.6931471805599453
System.out.println(Math.log(1.0)); // 0.0
System.out.println(Math.log(2.0)); // 0.6931471805599453
System.out.println(Math.log(3.0)); // 1.0986122886681098
System.out.println(Math.log(4.0)); // 1.3862943611198906
System.out.println(Math.log(5.0)); // 1.6094379124341003
System.out.println(Math.log(-1.0)); // NaN
System.out.println(Math.log(Double.NEGATIVE_INFINITY)); // NaN
System.out.println(Math.log(Double.NaN)); // NaN
System.out.println(Math.log(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.log(0.0)); // -Infinity
static double log10 (double a)
System.out.println(Math.log10(0.1)); // -1.0
System.out.println(Math.log10(0.2)); // -0.6989700043360187
System.out.println(Math.log10(0.3)); // -0.5228787452803376
System.out.println(Math.log10(0.4)); // -0.3979400086720376
System.out.println(Math.log10(0.5)); // -0.3010299956639812
System.out.println(Math.log10(1.0)); // 0.0
System.out.println(Math.log10(2.0)); // 0.3010299956639812
System.out.println(Math.log10(3.0)); // 0.47712125471966244
System.out.println(Math.log10(4.0)); // 0.6020599913279624
System.out.println(Math.log10(5.0)); // 0.6989700043360189
System.out.println(Math.log10(10.0)); // 1.0
System.out.println(Math.log10(100.0)); // 2.0
System.out.println(Math.log10(1000.0)); // 3.0
System.out.println(Math.log10(-1.0)); // NaN
System.out.println(Math.log10(Double.NEGATIVE_INFINITY)); // NaN
System.out.println(Math.log10(Double.NaN)); // NaN
System.out.println(Math.log10(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.log10(0.0)); // -Infinity
static double log1p (double x)
System.out.println(Math.log1p(-0.9)); // -2.302585092994046
System.out.println(Math.log1p(-0.8)); // -1.6094379124341005
System.out.println(Math.log1p(-0.7)); // -1.203972804325936
System.out.println(Math.log1p(-0.6)); // -0.916290731874155
System.out.println(Math.log1p(-0.5)); // -0.6931471805599453
System.out.println(Math.log1p(0.0)); // 0.0
System.out.println(Math.log1p(1.0)); // 0.6931471805599453
System.out.println(Math.log1p(2.0)); // 1.0986122886681096
System.out.println(Math.log1p(3.0)); // 1.3862943611198906
System.out.println(Math.log1p(4.0)); // 1.6094379124341003
System.out.println(Math.log1p(-2.0)); // NaN
System.out.println(Math.log1p(Double.NEGATIVE_INFINITY)); // NaN
System.out.println(Math.log1p(Double.NaN)); // NaN
System.out.println(Math.log1p(Double.POSITIVE_INFINITY)); // Infinity
static double max (double a, double b)
System.out.println(Math.max(1.0, 2.0)); // 2.0
System.out.println(Math.max(2.0, 1.0)); // 2.0
System.out.println(Math.max(5.0, 5.0)); // 5.0
System.out.println(Math.max(+0.0, -0.0)); // 0.0
System.out.println(Math.max(Double.MAX_VALUE, Double.MIN_VALUE)); // 1.7976931348623157E308
System.out.println(Math.max(999.0, Double.NEGATIVE_INFINITY)); // 999.0
System.out.println(Math.max(999.0, Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.max(999.0, Double.NaN)); // NaN
static float max (float a, float b)
This method is equivalent except a type to max(double a, double b).
static int max (int a, int b)
System.out.println(Math.max(1, 2)); // 2
System.out.println(Math.max(2, 1)); // 2
System.out.println(Math.max(5, 5)); // 5
System.out.println(Math.max(+0, -0)); // 0
System.out.println(Math.max(Integer.MAX_VALUE, Integer.MIN_VALUE)); // 2147483647
static long max (long a, long b)
This method is equivalent except a type to max(int a, int b).
static double min (double a, double b)
System.out.println(Math.min(1.0, 2.0)); // 1.0
System.out.println(Math.min(2.0, 1.0)); // 1.0
System.out.println(Math.min(5.0, 5.0)); // 5.0
System.out.println(Math.min(+0.0, -0.0)); // -0.0
System.out.println(Math.min(Double.MAX_VALUE, Double.MIN_VALUE)); // 4.9E-324
System.out.println(Math.min(999.0, Double.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Math.min(999.0, Double.POSITIVE_INFINITY)); // 999.0
System.out.println(Math.min(999.0, Double.NaN)); // NaN
static float min (float a, float b)
This method is equivalent except a type to min(double a, double b).
static int min (int a, int b)
System.out.println(Math.min(1, 2)); // 1
System.out.println(Math.min(2, 1)); // 1
System.out.println(Math.min(5, 5)); // 5
System.out.println(Math.min(+0, -0)); // 0
System.out.println(Math.min(Integer.MAX_VALUE, Integer.MIN_VALUE)); // -2147483648
static long min (long a, long b)
This method is equivalent except a type to min(int a, int b).
static int multiplyExact (int x, int y)
System.out.println(Math.multiplyExact(0, 100)); // 0
System.out.println(Math.multiplyExact(1, 300)); // 300
System.out.println(Math.multiplyExact(5000, 100)); // 500000
System.out.println(Math.multiplyExact(5000, -100)); // -500000
try {
final var value = Math.multiplyExact(1000000, 1000000);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//ArithmeticException!
final int value = 1000000 * 1000000;
System.out.println(value); // -727379968
static long multiplyExact (long x, int y)
This method is equivalent except a type to multiplyExact(int x, int y).
static long multiplyExact (long x, long y)
This method is equivalent except a type to multiplyExact(int x, int y).
static long multiplyFull (int x, int y)
System.out.println(Math.multiplyFull(0, 100)); // 0
System.out.println(Math.multiplyFull(1, 300)); // 300
System.out.println(Math.multiplyFull(5000, 100)); // 500000
System.out.println(Math.multiplyFull(5000, -100)); // -500000
System.out.println(Math.multiplyFull(1000000, 1000000)); // 1000000000000
System.out.println(Math.multiplyFull(Integer.MAX_VALUE, Integer.MAX_VALUE)); // 4611686014132420609
System.out.println(Math.multiplyFull(Integer.MIN_VALUE, Integer.MIN_VALUE)); // 4611686018427387904
static long multiplyHigh (long x, long y)
Please see also : unsignedMultiplyHigh(long x, long y)
final var x = 0x1234567890abcdefL;
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x0L)); // 0x0
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x1L)); // 0x0
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x10L)); // 0x1
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x100L)); // 0x12
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x1000L)); // 0x123
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x10000L)); // 0x1234
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x100000L)); // 0x12345
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x1000000L)); // 0x123456
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x10000000L)); // 0x1234567
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x100000000L)); // 0x12345678
static int negateExact (int a)
System.out.println(Math.negateExact(Integer.MAX_VALUE)); // -2147483647
System.out.println(Math.negateExact(100)); // -100
System.out.println(Math.negateExact(1)); // -1
System.out.println(Math.negateExact(0)); // 0
System.out.println(Math.negateExact(-100)); // 100
try {
final int value = Math.negateExact(Integer.MIN_VALUE);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException! : " + e.getMessage());
}
// Result
// ↓
//ArithmeticException! : integer overflow
static long negateExact (long a)
This method is equivalent except a type to negateExact(int a).
static double nextAfter (double start, double direction)
System.out.println(Math.nextAfter(1.0, 2.0)); // 1.0000000000000002
System.out.println(Math.nextAfter(1.0000000000000001, 2.0)); // 1.0000000000000002
System.out.println(Math.nextAfter(1.0000000000000002, 2.0)); // 1.0000000000000004
System.out.println(Math.nextAfter(1.0000000000000003, 2.0)); // 1.0000000000000004
System.out.println(Math.nextAfter(1.0, -2.0)); // 0.9999999999999999
System.out.println(Math.nextAfter(0.9999999999999999, -2.0)); // 0.9999999999999998
System.out.println(Math.nextAfter(0.9999999999999998, -2.0)); // 0.9999999999999997
System.out.println(Math.nextAfter(1.0, 1.0)); // 1.0
System.out.println(Math.nextAfter(1.0, Double.NaN)); // NaN
System.out.println(Math.nextAfter(Double.MIN_VALUE, -1.0)); // 0.0
// 1.7976931348623157E308 ( = Double.MAX_VALUE)
System.out.println(Math.nextAfter(Double.POSITIVE_INFINITY, -1.0));
static float nextAfter (float start, double direction)
This method is equivalent except a type to nextAfter(double start, double direction).
static double nextDown (double d)
System.out.println(Math.nextDown(1.0)); // 0.9999999999999999
System.out.println(Math.nextDown(0.9999999999999999)); // 0.9999999999999998
System.out.println(Math.nextDown(0.9999999999999998)); // 0.9999999999999997
System.out.println(Math.nextDown(Double.NaN)); // NaN
System.out.println(Math.nextDown(Double.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Math.nextDown(0.0)); // -4.9E-324 ( = -Double.MIN_VALUE )
static float nextDown (float f)
This method is equivalent except a type to nextDown(double d).
static double nextUp (double d)
System.out.println(Math.nextUp(1.0)); // 1.0000000000000002
System.out.println(Math.nextUp(1.0000000000000001)); // 1.0000000000000002
System.out.println(Math.nextUp(1.0000000000000002)); // 1.0000000000000004
System.out.println(Math.nextUp(1.0000000000000003)); // 1.0000000000000004
System.out.println(Math.nextUp(Double.NaN)); // NaN
System.out.println(Math.nextUp(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.nextUp(0.0)); // 4.9E-324 ( = Double.MIN_VALUE )
static float nextUp (float f)
This method is equivalent except a type to nextUp(double d).
static double pow (double a, double b)
System.out.println(Math.pow(2.0, 0.0)); // 1.0
System.out.println(Math.pow(2.0, 1.0)); // 2.0
System.out.println(Math.pow(2.0, 2.0)); // 4.0
System.out.println(Math.pow(2.0, 3.0)); // 8.0
System.out.println(Math.pow(2.0, 4.0)); // 16.0
System.out.println(Math.pow(2.0, -1.0)); // 0.5
System.out.println(Math.pow(2.0, -2.0)); // 0.25
System.out.println(Math.pow(2.0, -3.0)); // 0.125
System.out.println(Math.pow(2.0, -4.0)); // 0.0625
System.out.println(Math.pow(2.0, Double.NaN)); // NaN
System.out.println(Math.pow(Double.NaN, 2.0)); // NaN
System.out.println(Math.pow(2.0, Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.pow(0.5, Double.NEGATIVE_INFINITY)); // Infinity
System.out.println(Math.pow(2.0, Double.NEGATIVE_INFINITY)); // 0.0
System.out.println(Math.pow(0.5, Double.POSITIVE_INFINITY)); // 0.0
System.out.println(Math.pow(0.0, 2.0)); // 0.0
System.out.println(Math.pow(Double.POSITIVE_INFINITY, -2.0)); // 0.0
System.out.println(Math.pow(0.0, -2.0)); // Infinity
System.out.println(Math.pow(Double.POSITIVE_INFINITY, 2.0)); // Infinity
static double random ()
Please see also : Random , RandomGenerator
System.out.println(Math.random()); // 0.2637632679380906
System.out.println(Math.random()); // 0.43028303277317403
System.out.println(Math.random()); // 0.5014048102036631
System.out.println(Math.random()); // 0.8983305345177616
System.out.println(Math.random()); // 0.05255565620878788
static double rint (double a)
System.out.println(Math.rint(0.0)); // 0.0
System.out.println(Math.rint(0.1)); // 0.0
System.out.println(Math.rint(0.2)); // 0.0
System.out.println(Math.rint(0.4)); // 0.0
System.out.println(Math.rint(0.5)); // 0.0
System.out.println(Math.rint(0.51)); // 1.0
System.out.println(Math.rint(0.6)); // 1.0
System.out.println(Math.rint(0.9)); // 1.0
System.out.println(Math.rint(1.1)); // 1.0
System.out.println(Math.rint(1.2)); // 1.0
System.out.println(Math.rint(1.4)); // 1.0
System.out.println(Math.rint(1.5)); // 2.0
System.out.println(Math.rint(1.6)); // 2.0
System.out.println(Math.rint(-99.4)); // -99.0
System.out.println(Math.rint(-99.5)); // -100.0
System.out.println(Math.rint(-99.6)); // -100.0
System.out.println(Math.rint(Double.NaN)); // NaN
System.out.println(Math.rint(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.rint(Double.NEGATIVE_INFINITY)); // -Infinity
static long round (double a)
System.out.println(Math.round(0.0)); // 0
System.out.println(Math.round(0.1)); // 0
System.out.println(Math.round(0.2)); // 0
System.out.println(Math.round(0.4)); // 0
System.out.println(Math.round(0.5)); // 1
System.out.println(Math.round(0.6)); // 1
System.out.println(Math.round(0.9)); // 1
System.out.println(Math.round(1.1)); // 1
System.out.println(Math.round(1.2)); // 1
System.out.println(Math.round(1.4)); // 1
System.out.println(Math.round(1.5)); // 2
System.out.println(Math.round(1.6)); // 2
System.out.println(Math.round(-99.4)); // -99
System.out.println(Math.round(-99.5)); // -99
System.out.println(Math.round(-99.6)); // -100
System.out.println(Math.round(Double.NaN)); // NaN
// 9223372036854775807 ( = Long.MAX_VALUE )
System.out.println(Math.round(Double.POSITIVE_INFINITY));
// -9223372036854775808 ( = Long.MIN_VALUE )
System.out.println(Math.round(Double.NEGATIVE_INFINITY));
static int round (float a)
This method is equivalent except a type to round(double a).
static double scalb (double d, int scaleFactor)
System.out.println(Math.scalb(2.0, 0)); // 2.0
System.out.println(Math.scalb(2.0, 1)); // 4.0
System.out.println(Math.scalb(2.0, 2)); // 8.0
System.out.println(Math.scalb(2.0, 3)); // 16.0
System.out.println(Math.scalb(2.0, 4)); // 32.0
System.out.println(Math.scalb(2.0, -1)); // 1.0
System.out.println(Math.scalb(2.0, -2)); // 0.5
System.out.println(Math.scalb(2.0, -3)); // 0.25
System.out.println(Math.scalb(2.0, -4)); // 0.125
System.out.println(Math.scalb(Double.NaN, 1)); // NaN
System.out.println(Math.scalb(Double.POSITIVE_INFINITY, 1)); // Infinity
System.out.println(Math.scalb(Double.NEGATIVE_INFINITY, 1)); // -Infinity
System.out.println(Math.scalb(0.0, 100)); // 0.0
static float scalb (float f, int scaleFactor)
This method is equivalent except a type to scalb(double d, int scaleFactor).
static double signum (double d)
System.out.println(Math.signum(2.0)); // 1.0
System.out.println(Math.signum(-2.0)); // -1.0
System.out.println(Math.signum(100.0)); // 1.0
System.out.println(Math.signum(-100.0)); // -1.0
System.out.println(Math.signum(0.0)); // 0.0
System.out.println(Math.signum(-0.0)); // -0.0
System.out.println(Math.signum(Double.NaN)); // NaN
System.out.println(Math.signum(Double.POSITIVE_INFINITY)); // 1.0
System.out.println(Math.signum(Double.NEGATIVE_INFINITY)); // -1.0
static float signum (float f)
This method is equivalent except a type to signum(double d).
static double sin (double a)
System.out.println(Math.sin(-Math.PI)); // -1.2246467991473532E-16 (≒ 0.0)
System.out.println(Math.sin(-Math.PI * 0.75)); // -0.7071067811865476
System.out.println(Math.sin(-Math.PI * 0.5)); // -1.0
System.out.println(Math.sin(-Math.PI * 0.25)); // -0.7071067811865475
System.out.println(Math.sin(0.0)); // 0.0
System.out.println(Math.sin(Math.PI * 0.25)); // 0.7071067811865475
System.out.println(Math.sin(Math.PI * 0.5)); // 1.0
System.out.println(Math.sin(Math.PI * 0.75)); // 0.7071067811865476
System.out.println(Math.sin(Math.PI)); // 1.2246467991473532E-16 (≒ 0.0)
System.out.println(Math.sin(Double.NEGATIVE_INFINITY)); // NaN
System.out.println(Math.sin(Double.POSITIVE_INFINITY)); // NaN
System.out.println(Math.sin(Double.NaN)); // NaN
static double sinh (double x)
System.out.println(Math.sinh(-1.0)); // -1.1752011936438014
System.out.println(Math.sinh(-0.5)); // -0.5210953054937474
System.out.println(Math.sinh(0.0)); // 0.0
System.out.println(Math.sinh(0.5)); // 0.5210953054937474
System.out.println(Math.sinh(1.0)); // 1.1752011936438014
System.out.println(Math.sinh(Double.NEGATIVE_INFINITY)); // -Infinity
System.out.println(Math.sinh(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.sinh(Double.NaN)); // NaN
static double sqrt (double a)
System.out.println(Math.sqrt(0.0)); // 0.0
System.out.println(Math.sqrt(1.0)); // 1.0
System.out.println(Math.sqrt(2.0)); // 1.4142135623730951
System.out.println(Math.sqrt(4.0)); // 2.0
System.out.println(Math.sqrt(9.0)); // 3.0
System.out.println(Math.sqrt(16.0)); // 4.0
System.out.println(Math.sqrt(25.0)); // 5.0
System.out.println(Math.sqrt(-1.0)); // NaN
System.out.println(Math.sqrt(Double.NaN)); // NaN
System.out.println(Math.sqrt(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.sqrt(Double.NEGATIVE_INFINITY)); // NaN
static int subtractExact (int x, int y)
System.out.println(Math.subtractExact(10, 1)); // 9
System.out.println(Math.subtractExact(10, 2)); // 8
System.out.println(Math.subtractExact(10, 3)); // 7
System.out.println(Math.subtractExact(10, -1)); // 11
System.out.println(Math.subtractExact(10, -2)); // 12
System.out.println(Math.subtractExact(10, -3)); // 13
System.out.println(Math.subtractExact(1000, 1)); // 999
System.out.println(Math.subtractExact(-1000, 1)); // -1001
try {
System.out.println("MIN_VALUE : " + Integer.MIN_VALUE);
final var value = Math.subtractExact(Integer.MIN_VALUE, 1);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//MIN_VALUE : -2147483648
//ArithmeticException!
final int value = Integer.MIN_VALUE - 1;
System.out.println(value); // 2147483647
static long subtractExact (long x, long y)
This method is equivalent except a type to subtractExact(int x, int y).
static double tan (double a)
System.out.println(Math.tan(-Math.PI * 0.5)); // -1.633123935319537E16 (≒ -Infinity)
System.out.println(Math.tan(-Math.PI * 0.25)); // -0.9999999999999999 (≒ -1.0)
System.out.println(Math.tan(0.0)); // 0.0
System.out.println(Math.tan(Math.PI * 0.25)); // 0.9999999999999999 (≒ 1.0)
System.out.println(Math.tan(Math.PI * 0.5)); // 1.633123935319537E16 (≒ Infinity)
System.out.println(Math.tan(Double.NEGATIVE_INFINITY)); // NaN
System.out.println(Math.tan(Double.POSITIVE_INFINITY)); // NaN
System.out.println(Math.tan(Double.NaN)); // NaN
static double tanh (double x)
System.out.println(Math.tanh(-1.0)); // -0.7615941559557649
System.out.println(Math.tanh(-0.5)); // -0.46211715726000974
System.out.println(Math.tanh(0.0)); // 0.0
System.out.println(Math.tanh(0.5)); // 0.46211715726000974
System.out.println(Math.tanh(1.0)); // 0.7615941559557649
System.out.println(Math.tanh(Double.NEGATIVE_INFINITY)); // -1.0
System.out.println(Math.tanh(Double.POSITIVE_INFINITY)); // 1.0
System.out.println(Math.tanh(Double.NaN)); // NaN
static double toDegrees (double angrad)
System.out.println(Math.toDegrees(0.0)); // 0.0
System.out.println(Math.toDegrees(Math.PI * 0.5)); // 90.0
System.out.println(Math.toDegrees(Math.PI)); // 180.0
System.out.println(Math.toDegrees(Math.PI * 1.5)); // 270.0
System.out.println(Math.toDegrees(Math.PI * 2.0)); // 360.0
static int toIntExact (long value)
System.out.println(Math.toIntExact(0L)); // 0
System.out.println(Math.toIntExact(100L)); // 100
System.out.println(Math.toIntExact(-100L)); // -100
System.out.println(Math.toIntExact(Integer.MAX_VALUE)); // 2147483647
System.out.println(Math.toIntExact(Integer.MIN_VALUE)); // -2147483648
try {
final var value = Math.toIntExact(Integer.MAX_VALUE + 1L);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException!");
}
// Result
// ↓
//ArithmeticException!
static double toRadians (double angdeg)
System.out.println(Math.toRadians(0.0)); // 0.0
System.out.println(Math.toRadians(90.0)); // 1.5707963267948966 ( ≒ Math.PI * 0.5 )
System.out.println(Math.toRadians(180.0)); // 3.141592653589793 ( ≒ Math.PI * 1.0 )
System.out.println(Math.toRadians(270.0)); // 4.71238898038469 ( ≒ Math.PI * 1.5 )
System.out.println(Math.toRadians(360.0)); // 6.283185307179586 ( ≒ Math.PI * 2.0 )
static double ulp (double d)
System.out.println(Math.ulp(1.0)); // 2.220446049250313E-16
System.out.println(Math.ulp(2.0)); // 4.440892098500626E-16
System.out.println(Math.ulp(3.0)); // 4.440892098500626E-16
System.out.println(Math.ulp(4.0)); // 8.881784197001252E-16
System.out.println(Math.ulp(5.0)); // 8.881784197001252E-16
System.out.println(Math.ulp(1000.0)); // 1.1368683772161603E-13
System.out.println(Math.ulp(2000.0)); // 2.2737367544323206E-13
System.out.println(Math.ulp(3000.0)); // 4.547473508864641E-13
System.out.println(Math.ulp(Double.NaN)); // NaN
System.out.println(Math.ulp(Double.POSITIVE_INFINITY)); // Infinity
System.out.println(Math.ulp(Double.NEGATIVE_INFINITY)); // Infinity
System.out.println(Math.ulp(0.0)); // 4.9E-324 ( = Double.MIN_VALUE )
System.out.printf("%a%n", Math.ulp(Double.MAX_VALUE)); // 0x1.0p971
static float ulp (float f)
This method is equivalent except a type to ulp(double d).
static long unsignedMultiplyHigh (long x, long y)
Please see also : multiplyHigh(long x, long y)
final var x = Long.MAX_VALUE;
System.out.printf("0x%x%n", x); // 0x7fffffffffffffff
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x10L)); // 0x7
System.out.printf("0x%x%n", Math.unsignedMultiplyHigh(x, 0x10L)); // 0x7
final var x = Long.MIN_VALUE;
System.out.printf("0x%x%n", x); // 0x8000000000000000
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x10L)); // 0xfffffffffffffff8
System.out.printf("0x%x%n", Math.unsignedMultiplyHigh(x, 0x10L)); // 0x8
System.out.printf("0x%x%n", Math.multiplyHigh(x, 0x100L)); // 0xffffffffffffff80
System.out.printf("0x%x%n", Math.unsignedMultiplyHigh(x, 0x100L)); // 0x80