0% found this document useful (0 votes)
5 views8 pages

7) Mathematical Library Methods

The Math Library Class in Java provides a collection of static functions for manipulating numerical values, located in the java.lang package. Key functions include sqrt(), cbrt(), pow(), abs(), min(), max(), ceil(), floor(), round(), random(), rint(), and log(), each serving specific mathematical purposes with examples provided. The return types for these functions vary, primarily being double, int, or long depending on the input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

7) Mathematical Library Methods

The Math Library Class in Java provides a collection of static functions for manipulating numerical values, located in the java.lang package. Key functions include sqrt(), cbrt(), pow(), abs(), min(), max(), ceil(), floor(), round(), random(), rint(), and log(), each serving specific mathematical purposes with examples provided. The return types for these functions vary, primarily being double, int, or long depending on the input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Computer Science

1
Math Library Class

Math Library Class

Mr. SAMEER S NANEKAR


Computer Science
2
Math Library Class

Introduction
• Math Library class has collection of functions that manipulates the numerical
values

• It is a static class.

• It is present in java.lang package(default package)

• Syntax: Math.functionname(argument)

Mr. SAMEER S NANEKAR


Computer Science
3
Math Library Class

Function Name Description Example


Syntax
sqrt() Returns the square root of the Math.sqrt(16) // 4.0
Math.sqrt(value) given argument value Math.sqrt(-16) // Logical error
Return Type: double as square root of negative
number is not possible. Output
will be NAN (Not A Number)
cbrt() Returns the cube root of the Math.cbrt(27) // 3.0
Math.cbrt(value) given argument value Math.cbrt(-27) // -3.0
Return Type: double

Mr. SAMEER S NANEKAR


Computer Science
4
Math Library Class

Function Name Description Example


Syntax
pow() It is used to calculate a number Math.pow(2,3) // 8.0
Math.pow(base, power) raise to the power Math.pow(5, -2) // 0.04
Return Type: double Math.pow(32, 0.2) // 2.0
abs() Returns the absolute value of Math.abs(-7.0) // 7.0
Math.abs(value) the given argument value Math.abs(-7) // 7
Return Type: int, long, double Math.abs(8) // 8
depending on the argument

Mr. SAMEER S NANEKAR


Computer Science
5
Math Library Class

Function Name Description Example


Syntax
min() Returns the minimum of the two Math.min(2,3) // 2
Math.min(value 1, value 2) value Math.min(5, 2.0) // 2.0
Return Type: int or double Math.min(2, Math.min(5,1)) // 1
depending on the argument value
max() Returns the maximum of the two Math.max(2,3) // 3
Math.max(value 1, value 2) value Math.max(5, 2.0) // 5.0
Return Type: int or double Math.max(2, Math.max(5,1)) // 5
depending on the argument value

Mr. SAMEER S NANEKAR


Computer Science
6
Math Library Class

Function Name Description Example


Syntax
ceil() Return the nearest integer value in Math.ceil(2.3) // 3.0
Math.ceil(value) double datatype which greater than Math.ceil(2.6) // 3.0
or equals to the given argument Math.ceil(-2.1) // -2.0
Return Type: double
floor() Return the nearest integer value in Math.floor(2.3) // 2.0
Math.floor(value) double datatype which smaller than Math.floor(2.6) // 2.0
or equals to the given argument Math.floor(-2.1) // -3.0
Return Type: double

Mr. SAMEER S NANEKAR


Computer Science
7
Math Library Class

Function Name Description Example


Syntax
round() Return the value in Math.round(2.3) // 2
Math.round(value) rounded form Math.round(2.6) // 3
Return Type: long (most Math.round(-2.1) // -2
of the time) or int Math.round(-2.8) // -3
random() Return any random Math.random() // any number between 0 & 1
Math.random() number between 0 & 1 To get numbers from 0 to 9
Return Type: double System.out.println((int)(Math.random( )*10));
To get numbers from 0 to 99
System.out.println((int)(Math.random( )*100));
To get numbers for a range: int m=60, n=45;
System.out.println((int)(Math.random( )*(m-n)+n));
Mr. SAMEER S NANEKAR
Computer Science
8
Math Library Class

Function Name Description Example


Syntax
rint() Return the integer value in double datatype after Math.rint(2.6) // 3.0
Math.rint(value) rounding the decimal part the given argument. Math.rint(2.2) // 2.0
In case 0.5 fractional value, it will round off Math.rint(2.5) // 2.0
to nearest even integer in double datatype Math.rint(3.5) // 4.0
Return Type: double
log() Return the natural logarithmic value of the given Math.log(5.5)
Math.log(value) argument //1.7047480922384253
Return Type: double

Mr. SAMEER S NANEKAR

You might also like