0% found this document useful (0 votes)
30 views

Java Math Class - GeeksforGeeks

The document discusses the Java Math class which contains methods for common mathematical operations like trigonometric, exponential, logarithmic and other functions. It lists the various methods of the Math class along with a brief description of each method and what mathematical operation it performs.

Uploaded by

ishwar khalkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Java Math Class - GeeksforGeeks

The document discusses the Java Math class which contains methods for common mathematical operations like trigonometric, exponential, logarithmic and other functions. It lists the various methods of the Math class along with a brief description of each method and what mathematical operation it performs.

Uploaded by

ishwar khalkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

4/22/24, 8:20 PM Java Math Class - GeeksforGeeks

Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exceptio

Java Math Class


Last Updated : 11 Mar, 2024
Java.lang.Math Class methods help to perform numeric operations like
square, square root, cube, cube root, exponential and trigonometric
operations.

Declaration

public final class Math


extends Object

Methods of Math Class in Java


Math class consists of methods that can perform mathematical operations
and can make long calculations a bit easier. Let us check the method
provided in the Math class.

Method Description

sin Returns the trigonometric value of the sine of an angle.

cos Returns the trigonometric value of the cosine of an angle.

tan Returns the trigonometric value of the tangent of an angle.

asin Returns the trigonometric value of the arc sine of an angle.

Returns the trigonometric value of the arc cosine of an


acos
angle.

Returns the trigonometric value of the arc tangent of an


atan
angle.

toRadians Convert value in degrees to value in radians


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
toDegrees Convert value in radians to value in degrees
Policy

https://www.geeksforgeeks.org/java-math-class/ 1/12
4/22/24, 8:20 PM Java Math Class - GeeksforGeeks

Method Description

Returns Euler’s number e raised to the power of a double


exp
value

log Returns the natural logarithm (base e) of a double value

log10 Returns the base 10 logarithms of a double value

Returns the correct rounded positive square root of a


sqrt
double value

cbrt Returns the cube root of a double value

Computes the remainder operation on two arguments as


IEEEremainder
instructed by the IEEE 754 standard.

Returns the smallest double value that is greater than or


ceil
equal to the argument. It is a fixed mathematical integer

Returns the smallest double value that is less than or equal


floor
to the argument. It is a fixed mathematical integer.

Returns the double value that is closest in value to the


rint
argument and is equal to a mathematical integer

Returns the angle theta from the conversion of rectangular


atan2
coordinates (x, y) to polar coordinates (r, theta)

pow Returns(pow(a,b)) the value of ab.

Returns the closest int to the argument, with ties rounding


round
to positive infinity

Returns a double value with a positive sign, in the range


random
[0.0, 1.0].

Returns the sum of its arguments, throwing an exception if


We use cookiesaddExact
to ensure you have the best browsing experience on our website. By using
the result
our site, you acknowledge that you have read and understood overflows
our Cookie a value.
Policy & Privacy
Policy

https://www.geeksforgeeks.org/java-math-class/ 2/12
4/22/24, 8:20 PM Java Math Class - GeeksforGeeks

Method Description

Returns the difference of its arguments, throwing an


subtractExact
exception if the result overflows a value.

Returns the product of the arguments, throwing an


multiplyExact
exception if the result overflows a value.

Returns the argument value incremented by 1 and throws


incrementExact
an exception if the value overflows.

Returns the argument value decremented by 1 and throws


decrementExact
an exception if the value overflows.

Returns the negation of the argument and throws an


negateExact
exception if the value overflows.

Returns the value of the long argument and throws an


toIntExact
exception if the int overflows.

Returns the largest (closest to positive infinity) long value


floorDiv
that is less than or equal to the algebraic quotient

floorMod Returns the floor modulus of the arguments

abs Return the absolute value

max Returns the maximum out of the two arguments

min Returns the minimum out of the two arguments

ulp Returns the size of an ulp of the argument.

Returns value 0,1 and -1 depending upon if the argument is


signum
equal, greater, or less than zero respectively.

sinh Returns the hyperbolic sine of the value.

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge
cosh that you have read and understood
Returns our Cookie Policy
the hyperbolic & Privacy
cosine of the value.
Policy

https://www.geeksforgeeks.org/java-math-class/ 3/12
4/22/24, 8:20 PM Java Math Class - GeeksforGeeks

Method Description

tanh Returns the hyperbolic sine of the value.

hypot Returns the hyperbolic tangent of the value.

expm1 Returns ex-1

Returns the natural logarithm of the sum of the argument


log1p
and 1

Returns the first value argument with the sign of the second
copySign
value argument

Returns the unbiased exponent used in the representation


getExponent
of a value

Returns the floating-point number adjacent to the first


nextAfter
argument in the direction of the second argument.

Returns the floating-point value adjacent to d in the


nextUp
direction of positive infinity.

Returns the floating-point value adjacent to d in the


nextDown
direction of negative infinity.

Returns f × 2scaleFactor rounded as if performed by a


scalb single correctly rounded floating-point multiplied to a
member of the value set.

Example of the Java Math Class


Java

// Java Program to demonstrate the


// Use of Math Class
public class MathLibraryExample {
public static void main(String[] args)
We use cookies
{ to ensure you have the best browsing experience on our website. By using
our site, you acknowledge
int i =that
7;you have read and understood our Cookie Policy & Privacy
int j = -9; Policy

https://www.geeksforgeeks.org/java-math-class/ 4/12
4/22/24, 8:20 PM Java Math Class - GeeksforGeeks

double x = 72.3;
double y = 0.34;

System.out.println("i is " + i);


System.out.println("j is " + j);

// The absolute value of a number is equal to the


// number if the number is positive or zero and
// equal to the negative of the number if the number
// is negative.

System.out.println("|" + i + "| is " + Math.abs(i));


System.out.println("|" + y + "| is " + Math.abs(y));

// Truncating and Rounding functions


// You can round off a floating point number to the
// nearest integer with round()
System.out.println(x + " is approximately "
+ Math.round(x));
System.out.println(y + " is approximately "
+ Math.round(y));

// The "ceiling" of a number is the smallest integer


// greater than or equal to the number. Every
// integer is its own //ceiling.
System.out.println("The ceiling of " + x + " is "
+ Math.ceil(x));
System.out.println("The ceiling of " + y + " is "
+ Math.ceil(y));

// The "floor" of a number is the largest integer


// less than or equal to the number. Every integer
// is its own floor.
System.out.println("The floor of " + x + " is "
+ Math.floor(x));
System.out.println("The floor of " + y + " is "
+ Math.floor(y));

// Comparison operators

// min() returns the smaller of the two arguments


// you pass it
System.out.println("min(" + i + "," + j + ") is "
+ Math.min(i, j));
System.out.println("min(" + x + "," + y + ") is "
+ Math.min(x, y));

// There's a corresponding max() method


// that returns the larger of two numbers
We use cookies toSystem.out.println("max(" + i + "," + j + ") is "
ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read+ and
Math.max(i, j));
understood our Cookie Policy & Privacy
System.out.println("max(" + x + "," + y + ") is "
Policy
+ Math.max(x, y));
https://www.geeksforgeeks.org/java-math-class/ 5/12
4/22/24, 8:20 PM Java Math Class - GeeksforGeeks

// The Math library defines a couple of useful


// constants:
System.out.println("Pi is " + Math.PI);
System.out.println("e is " + Math.E);

// Trigonometric methods. All arguments are given in


// radians
// Convert a 45 degree angle to radians
double angle = 45.0 * 2.0 * Math.PI / 360.0;
System.out.println("cos(" + angle + ") is "
+ Math.cos(angle));
System.out.println("sin(" + angle + ") is "
+ Math.sin(angle));

// Inverse Trigonometric methods. All values are


// returned as radians

double value = 0.707;

System.out.println("acos(" + value + ") is "


+ Math.acos(value));
System.out.println("asin(" + value + ") is "
+ Math.asin(value));
System.out.println("atan(" + value + ") is "
+ Math.atan(value));

// Exponential and Logarithmic Methods

// exp(a) returns e (2.71828...) raised


// to the power of a.
System.out.println("exp(1.0) is " + Math.exp(1.0));
System.out.println("exp(10.0) is "
+ Math.exp(10.0));
System.out.println("exp(0.0) is " + Math.exp(0.0));

// log(a) returns the natural


// logarithm (base e) of a.
System.out.println("log(1.0) is " + Math.log(1.0));
System.out.println("log(10.0) is "
+ Math.log(10.0));
System.out.println("log(Math.E) is "
+ Math.log(Math.E));

// pow(x, y) returns the x raised


// to the yth power.
System.out.println("pow(2.0, 2.0) is "
+ Math.pow(2.0, 2.0));
System.out.println("pow(10.0, 3.5) is "
+ Math.pow(10.0, 3.5));
We use cookies to ensure you have the best browsing experience on our website. By using
System.out.println("pow(8, -1) is "
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
+ Math.pow(8, -1));
Policy

https://www.geeksforgeeks.org/java-math-class/ 6/12
4/22/24, 8:20 PM Java Math Class - GeeksforGeeks

// sqrt(x) returns the square root of x.


for (i = 0; i < 10; i++) {
System.out.println("The square root of " + i
+ " is " + Math.sqrt(i));
}

// Finally there's one Random method


// that returns a pseudo-random number
// between 0.0 and 1.0;

System.out.println("Here's one random number: "


+ Math.random());
}
}

Output

i is 7j is -9
|7| is 7
|0.34| is 0.34
We use cookies to ensure you have the best browsing experience on our website. By using
72.3 is approximately 72
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
0.34 is approximately 0Policy

https://www.geeksforgeeks.org/java-math-class/ 7/12
4/22/24, 8:20 PM Java Math Class - GeeksforGeeks

The ceiling of 72.3 is 73.0


The ceiling of 0.34 is 1.0
The floor of 72.3 is 72.0
The floor of 0.34 is 0.0min(7,-9) is -9
min(72.3,0.34) is 0.34
max(7,-9) is 7
max(72.3,0.34) is 72.3
Pi is 3.141592653589793
e is 2.718281828459045
cos(0.7853981633974483) is 0.7071067811865476
sin(0.7853981633974483) is 0.7071067811865475
acos(0.707) is 0.7855491633997437
asin(0.707) is 0.785247163395153
atan(0.707) is 0.6154085176292563
exp(1.0) is 2.718281828459045
exp(10.0) is 22026.465794806718
exp(0.0) is 1.0
log(1.0) is 0.0
log(10.0) is 2.302585092994046
log(Math.E) is 1.0
pow(2.0, 2.0) is 4.0

NaN’s argument: A constant holding a Not-a-Number (NaN) value of


type double. It is equivalent to the value returned by
Double.longBitsToDouble(0x7ff8000000000000L).

Feeling lost in the vast world of Backend Development? It's time for a
change! Join our Java Backend Development - Live Course and embark on an
exciting journey to master backend development efficiently and on schedule.
What We Offer:

Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks

We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
9 Policy Suggest improvement

https://www.geeksforgeeks.org/java-math-class/ 8/12

You might also like