Page 211 - ComputerScience_Class_11
P. 211
5. Math.pow()
n
This function finds the power of a number in the format m , where m is the base and n represents how many times
a number m is multiplied.
Example 1: double d = Math.pow(2,2); Output: 4.0
Example 2: double d = Math.pow(4,-3); Output: 0.015625
6. Math.log()
This function finds the logarithm of a value provided in the argument.
Example 1: double d = Math.log(4); Output: 1.3862943611198906
Example 2: double d = Math.log(-2); Output: NaN
7. Math.abs()
This function finds the absolute value of the argument, i.e., it always returns the positive value.
Example 1: double d = Math.abs(2.3); Output: 2.3
Example 2: int i = Math.abs(2); Output: 2
For negative value:
Example 3: double d = Math.abs(-8.6); Output: 8.6
Example 4: int i = Math.abs(-7); Output: 7
8. Math.round()
This function rounds a number passed as an argument to its nearest integer using normal math round rules.
If it is an integer value, then it will return the same value.
Example : int i = Math.round(5); Output: 5
If it is a real positive value, then the value after the decimal is considered according to the following:
If the decimal value is less than 0.5, then the output will be integer value only else will return the smallest integer
value greater than the number. If the decimal value is less than equal to 0.5, then the output will be integer value
only else will the largest integer value smaller than the number.
Example 1: int i = Math.round(6.35); Output: 6
Example 2: int i = Math.round(-26.87); Output: -27
9. Math.ceil()
This function rounds a floating-point number up to the next largest integer value. In other words, it finds the
smallest integer value which is greater than or equal to the argument passed to it.
It uses a double data type.
Example 1: double d = Math.ceil(4.1); Output: 5.0
Example 2: double d = Math.ceil(-58.9); Output: -58.0
10. Math.floor()
This function rounds a floating-point number down to its nearest integer value. In other words, it finds the largest
integer value which is less than or equal to the argument passed to it. It uses a double data type.
Example 1: double d = Math.floor(-3.3); Output: -4.0
Example 2: double d = Math.floor(25.8); Output: 25.0
11. Math.rint()
This function rounds the floating-point argument to its nearest integer in floating-point format.
For positive fractional numbers:
Statements and Scope 209

