Java Math Tutorial

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

Following are the list of all Math Functions.

Mathabsx

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.

Mathacosx

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.

Mathasinx

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.

Mathatanx

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.

Mathatan2y,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

Mathcbrtx

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.

Mathceilx

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.

MathcopySignx, 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

Mathcosangle

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.

Mathcoshx

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.

Mathexpx

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.

Mathexpm1x

Returns ex -1 double

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

More about Java Math expm1() function.

Mathfloorx

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.

MathgetExponentx

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.

Mathhypotx, 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.

MathIEEEremainderx, 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.

Mathlogx

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.

Mathlog10x

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.

Mathlog1px

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.

Mathmaxx, 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.

Mathminx, 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.

MathnextAfterx, 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.

MathnextUpx

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.

Mathpowx, 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.

Mathrandom

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

double n = Math.random();

More about Java Math random() function.

Mathroundx

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.

Mathrinta

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.

Mathsignumx

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.

Mathsinangle

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.

Mathsinhx

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.

Mathsqrtx

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.

Mathtanangle

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.

Mathtanhx

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.

MathtoDegreesx

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.

MathtoRadiansx

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.

Mathulpx

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.