Java Math Tutorial

Java Math library has many mathematical functions to work with numbers.

Following are the list of all Math Functions.

Math.abs(x)

abs(x) returns the absolute value of x. Datatype of x could be double, float, int or long and the return type would be same as that of argument.

int x = -10;
int a = Math.abs(x); //10

More about Java Math abs() function.

ADVERTISEMENT

Math.acos(x)

acos(x) returns the arc-cosine of x, in radians, in the range of 0.0 to pi. The return type of acos() method is double.

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

More about Java Math acos() function.

Math.asin(x)

asin(x) returns the arc-sine of x, in radians, in the range of 0.0 to pi. The return type of asin() method is double.

double x = 0.5;
double a = Math.asin(x); //0.5235987755982989

More about Java Math asin() function.

Math.atan(x)

atan(x) returns the arc-tangent of x as a numeric value between –pi/2 and pi/2 radians. The return type of atan() method is double.

double x = 1.73;
double a = Math.atan(x); //1.0466843936522807

More about Java Math atan() function.

Math.atan2(y,x)

Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). double

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

Math.cbrt(x)

cbrt() returns the cube root of x. x could be type int, float, long or double. cbrt() method returns value of type double.

int x = 27;
double a = Math.cbrt(x); //3.0

More about Java Math cbrt() function.

Math.ceil(x)

Returns the value of x rounded up to its nearest integer double

double x = 2.145D;
double a = Math.ceil(x); //3

More about Java Math ceil() function.

Math.copySign(x, y)

Returns the first floating point x with the sign of the second floating point y double

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

Math.cos(angle)

cos() method returns the cosine of angle (angle is in radians). The datatype of return value is double.

double x = 0.5; //radians
double a = Math.cos(x); //0.8775825618903728

More about Java Math cos() function.

Math.cosh(x)

cosh() method computes and returns the hyperbolic cosine of argument. cosh() method returns the result as value of datatype double.

double x = 10;
double a = Math.cosh(x); //11013.232920103324

More about Java Math cosh() function.

Math.exp(x)

exp() method returns the value of e to the power of x.

double x = 4.5;
double a = Math.exp(x); //90.01713130052181

More about Java Math exp() function.

Math.expm1(x)

Returns ex -1 double

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

More about Java Math expm1() function.

Math.floor(x)

Returns the value of x rounded down to its nearest integer double

double x = 5.9;
double a = Math.floor(x); //5.0

More about Java Math floot() function.

Math.getExponent(x)

Returns the unbiased exponent used in x int

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

More about Java Math getExponent() function.

Math.hypot(x, y)

hypot() method returns sqrt(x2 +y2) without intermediate overflow or underflow. This is the formula for computing length of hypotenuse when base and height are given.

double base = 3;
double height = 4;
double hypotenuse = Math.hypot(base, height); //5.0

More about Java Math hypot() function.

Math.IEEEremainder(x, y)

Computes the remainder operation on x and y as prescribed by the IEEE 754 standard double

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

More about Java Math IEEEremainder() function.

Math.log(x)

log() method returns the natural logarithm (base e) of x.

double x = 10;
double a = Math.log(x); //2.302585092994046

More about Java Math log() function.

Math.log10(x)

Returns the base 10 logarithm of x double

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

More about Java Math log10() function.

Math.log1p(x)

Returns the natural logarithm (base E) of the sum of x and 1 double

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

More about Java Math log1p() function.

Math.max(x, y)

max() method accepts two numbers as arguments and returns the largest of the two numbers.

int a = 10;
int b = 5;
int result = Math.max(a, b); //10

More about Java Math max() function.

Math.min(x, y)

max() method accepts two numbers as arguments and returns the smallest of the two numbers.

int a = 10;
int b = 5;
int result = Math.min(a, b); //5

More about Java Math min() function.

Math.nextAfter(x, y)

Returns the floating point number adjacent to x in the direction of y double|float

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

More about Java Math nextAfter() function.

Math.nextUp(x)

Returns the floating point value adjacent to x in the direction of positive infinity double|float

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

More about Java Math nextUp() function.

Math.pow(x, y)

pow() method returns the value of x to the power of y.

double a = 5.2;
double b = 2;
double result = Math.pow(a, b); //27.040000000000003

More about Java Math pow() function.

Math.random()

random() method returns a random number between 0 and 1.

double n = Math.random();

More about Java Math random() function.

Math.round(x)

round() method returns the value of argument rounded to its nearest integer.

double a = 5.236;
long rounded = Math.round(a); //5

More about Java Math round() function.

Math.rint(a)

rint() method returns the double value that is closest to x and equal to a mathematical integer double

double a = 21.35;
double result = Math.rint(a); //21.0

More about Java Math rint() function.

Math.signum(x)

signum() method returns the signum function applied on the argument.

double a = 1.5;
double result = Math.signum(a); //1.0

More about Java Math signum() function.

Math.sin(angle)

sin() method computes and returns the sine of angle (angle argument is in radians). The datatype of return value is double.

double angle = 0.5; //radians
double result = Math.sin(angle); //0.479425538604203

More about Java Math sin() function.

Math.sinh(x)

sinh() method computes and returns the hyperbolic sine of argument. sinh() method returns the result as value of datatype double.

double x = 10;
double a = Math.sinh(x); //11013.232874703393

More about Java Math sinh() function.

Math.sqrt(x)

sqrt() method returns the square root of x. The datatype of return value is double.

double x = 4;
double a = Math.sqrt(x); //2.0

More about Java Math sqrt() function.

Math.tan(angle)

tan() method computes and returns the tangent of an angle. The datatype of angle is double and is considered to be in radians. tan() returns value of datatype double.

double angle = 0.5; //radians
double result = Math.tan(x); //0.5463024898437905

More about Java Math tan() function.

Math.tanh(x)

tanh() method computes and returns the hyperbolic tangent of argument. tanh() method returns the result as value of datatype double.

double x = 0.5;
double a = Math.tanh(x); //0.46211715726000974

More about Java Math tanh() function.

Math.toDegrees(x)

toDegrees() method converts an angle measured in radians to an approximate equivalent angle measured in degrees. The datatype of return value is double.

double x = 1;
double a = Math.toDegrees(x); //57.29577951308232

More about Java Math toDegrees() function.

Math.toRadians(x)

toRadians() method converts an angle measured in degrees to an approximate equivalent angle measured in radians. The datatype of return value is double.

double degrees = 45;
double radians = Math.toRadians(degrees); //0.7853981633974483

More about Java Math toRadians() function.

Math.ulp(x)

Returns the size of the unit of least precision (ulp) of x

double x = 0.5;
double a = Math.acos(x); //1.0471975511965979

More about Java Math ulp() function.