BlogsDope image BlogsDope

Functions available for use on JavaScript Math Object

June 14, 2020 JAVASCRIPT 1326

Math is a built-in JavaScript object that allows you to perform mathematical operations easily. Unlike other objects like String, it is not a constructor. You can directly call methods using Math object, for example, Math.sqrt(). In this tutorial, we are going to see what different methods are available for use and explain them with examples. So, let’s get started!

abs()


The abs() method returns the absolute value of a number i.e. it returns the non-negative value of a number. The syntax is Math.abs(x). Let’s see the example below.

x_neg = -30
x_pos = 30

res1 = Math.abs(x_neg)
console.log("The absolute value of " + x_neg + " is " + res1)
res2 = Math.abs(x_pos)
console.log("The absolute value of " + x_pos + " is " + res2)

Output

The absolut value of -30 is 30
The absolute value of 30 is 30

The absolute of a negative number is a positive number and the absolute value of a positive number is a positive number.

Some important points about the parameter x in the Math object.

  • If x is null, the method returns the same value as it would return for zero.
  • If x is undefined or any value that cannot be converted into a number, the method returns NaN.

round()


This method rounds off a given number to the nearest integer. The fractional part of a number greater than or equal to .5 is rounded up and less than .5 is rounded down to the nearest integer. The syntax is Math.round(x). Let’s see some examples.

console.log(Math.round(3.5))
console.log(Math.round(3.78))
console.log(Math.round(3.45))
console.log(Math.round(-3.45))
console.log(Math.round(-3.7))

Output

4
4
3
-3
-4

Note when rounding off, the absolute value of a number is considered. Therefore, -3.45 is rounded down to -3, and -3.7 is rounded up to -4.

ceil()


The ceil() method converts a floating-point number to the next nearest integer. The syntax is Math.ceil(x). Consider the following examples.

console.log(Math.ceil(3.4))
console.log(Math.ceil(3.7))

 Output

4
4

floor()


The floor() method converts a floating-point number to the previous nearest integer. The syntax is Math.floor(x). Consider the following examples.

console.log(Math.floor(3.4))
console.log(Math.floor(3.7))

Output

3
3

trunc()


This method returns the integer part of a number. It does not rounds up or down like math.floor(), math.ceil(), or math.round(). It just truncates the decimal part. The syntax is Math.trunc(x). Let’s see.

console.log(Math.trunc(2.45778))
console.log(Math.trunc(3))
console.log(Math.trunc(3.01))
console.log(Math.trunc("4.20"))

Output

2
3
3
4

exp()


The Math.exp(x) method returns the result of ex. Here, the base e is approximately equal to 2.1783, and the power x is given as a parameter. It is known as a natural exponential function. Let’s see.

console.log(Math.exp(1))
console.log(Math.exp(0))
console.log(Math.exp(-4))

Output

2.718281828459045
1
0.01831563888873418

log()


The Math.log(x) method computes the logarithmic function of x to the base e. The domain of the natural logarithmic function is all real numbers greater than 0 and the range consists of all real values. Let’s see.

console.log(Math.log(1))
console.log(Math.log(Math.E))
console.log(Math.log(4))
console.log(Math.log(-4))
console.log(Math.log(0))

Output

0
1
1.3862943611198906
NaN
-Infinity

When given a negative value it returns NaN and on zero and null it returns -Infinity. The natural logarithmic function and the natural exponential function are inverses of each other.

pow()


The Math.pow(x, y) method computes the value of base x raised to the power y. The examples are given below.

console.log(Math.pow(2, 3))
console.log(Math.pow(3, 0))
console.log(Math.pow(-3, 3))
console.log(Math.pow(3, -2))
console.log(Math.pow(1/3, 2))

Output

8
1
-27
0.1111111111111111
0.1111111111111111

The last two examples are the same written in two different forms.

sqrt()


The Math.sqrt(x) method calculates the square root of a number x.

console.log(Math.sqrt(16))
console.log(Math.sqrt(2))
console.log(Math.sqrt(-2))

Output

4
1.4142135623730951
NaN

It returns NaN on a negative value.

cbrt()


The Math.cbrt(x) method calculates the cubic root of a number x.

console.log(Math.cbrt(27))
console.log(Math.cbrt(125))
console.log(Math.cbrt(-8))

Output

3
5
-2

Let’s us now see some basic trigonometric functions like sin(), arcsin(), cos(), etc. Note that these methods in the Math object require or output the angle in radians. However, we normally use degrees. So, let’s create two functions that convert radians to degrees and degrees to radians. i.e.

function degtorad(deg)
{
    return deg*(Math.PI/180)
}

function radtodeg(rad)
{
    return rad/(Math.PI/180)
}

Now, let’s go through the functions provided.

sin()


The Math.sin(x) finds the sine of the angle x in radians. The domain is all real numbers and the range is [-1, 1]. Let’s see.

console.log(Math.sin(degtorad(180)))
console.log(Math.sin(degtorad(30)))
console.log(Math.sin(degtorad(90)))

Output

1.2246467991473532e-16
0.49999999999999994
1

If we wish to provide the angle in radians, no conversion is required.

cos()


The Math.cos(x) finds the cosine of the angle x in radians. The domain is all real numbers and the range is [-1, 1]. Let’s see.

console.log(Math.cos(degtorad(180)))
console.log(Math.cos(degtorad(30)))
console.log(Math.cos(degtorad(90)))

Output

-1
0.5000000000000001
6.123233995736766e-17

tan()


The Math.tan(x) finds the tangent of the angle x in radians. The domain is all real numbers except the values where cos(x) is 0 like π/2 or 3π/2 and the range is all real numbers. Let’s see

console.log(Math.tan(degtorad(30)))
console.log(Math.tan(degtorad(60)))
console.log(Math.tan(degtorad(0)))
console.log(Math.tan(degtorad(90)))

Output

0.5773502691896257
1.7320508075688767
0
16331239353195370

As you can observe above, Math.tan(90o) is not undefined. The reason is that Math.cos(90o) does not give an exact value of 0. It returns 6.12 × 10-17 (approximately), which is really small and almost equal to zero and it prevents the denominator to be equal to zero.

asin()


The Math.asin(x) method calculates the inverse of the sine function i.e. given a value, it finds the angle in radians. The domain is [-1, 1] and the range is [-π/2, π/2] or [-90o, 90o].

console.log(radtodeg(Math.asin(0.5)))
console.log(radtodeg(Math.asin(-0.5)))
console.log(radtodeg(Math.asin(-2)))

Output

30.000000000000004
-30.000000000000004
NaN

acos()


The Math.acos(x) method calculates the inverse of the cosine function i.e. given a value, it finds the angle in radians. The domain is [-1, 1] and the range is [0, π] or [0o, 180o].

console.log(radtodeg(Math.acos(0.5)))
console.log(radtodeg(Math.acos(-0.5)))
console.log(radtodeg(Math.acos(-1)))

Output

60.00000000000001
120.00000000000001
180

atan()


The Math.atan(x) method calculates the inverse of the tangent function i.e. given a value, it finds the angle in radians. The domain is all real numbers and the range is [-π/2, π/2] or [-90o, 90o].

console.log(radtodeg(Math.atan(0)))
console.log(radtodeg(Math.atan(1)))
console.log(radtodeg(Math.atan(-1)))

Output

0
45
-45

atan2()


The Math.atan2(y, x) takes two parameters, the first is a value on the vertical axis and the other is a value on the horizontal axis in the coordinate system. It finds the counterclockwise angle in radians between the point (x, y) and the x-axis. The range is (-π, π).

console.log(radtodeg(Math.atan2(2,0)))
console.log(radtodeg(Math.atan2(2,2)))
console.log(radtodeg(Math.atan2(0,-2)))

Output

90
45
180

If any of the parameters are missing, the method return NaN.

sinh()


The Math.sinh(x) calculates the hyperbolic sine of a number. The domain and the range consist of all real numbers.

console.log((Math.sinh(0)))
console.log((Math.sinh(1)))
console.log((Math.sinh(-2)))

Output

0
1.1752011936438014
-3.626860407847019

cosh()


The Math.cosh(x) calculates the hyperbolic cosine of a number. The domain consists of all real numbers and the range consists of all real values greater than or equal to 1.

console.log((Math.cosh(0)))
console.log((Math.cosh(1)))
console.log((Math.cosh(-2)))

Output

1
1.5430806348152437
3.7621956910836314

tanh()


The Math.tanh(x) calculates the hyperbolic tangent of a number. The domain consists of all real numbers and the range is (-1, 1).

console.log((Math.tanh(0)))
console.log((Math.tanh(1)))
console.log((Math.tanh(-2)))

Output

0
0.7615941559557649
-0.9640275800758169

asinh()


The Math.asinh(x) method calculates the inverse hyperbolic sine of a number. The domain and range consist of all real numbers.

console.log((Math.asinh(0)))
console.log((Math.asinh(1.175)))
console.log((Math.asinh(-3.63)))

Output

0
0.9998696091254418
-2.0008341750867618

acosh()


The Math.acosh(x) method calculates the inverse hyperbolic cosine of a number. The domain is [1, infinity) and the range is [0, infinity).

console.log((Math.acosh(1)))
console.log((Math.acosh(3.76)))
console.log((Math.acosh(-1)))

Output

0
1.9993944126410146
NaN

atanh()


The Math.atanh(x) method calculates the inverse hyperbolic tangent of a number. The domain is (-1, 1) and the range is all real numbers.

console.log((Math.atanh(0)))
console.log((Math.atanh(0.5)))
console.log((Math.atanh(-1)))
console.log((Math.atanh(-2)))

Output

0
0.5493061443340548
-Infinity
NaN

Note that on -1 and 1, it returns -Infinity and Infinity, respectively. It returns NaN on all the other values outside the domain.

max()


The Math.max(x1, x2, …., xN) method finds the maximum value from the numbers given as parameters. If no parameter is provided, it returns -Infinity. If one parameter is provided, it returns itself and if it contains any values that cannot be converted into a numerical value, it returns NaN. Let’s see the examples below.

console.log(Math.max())
console.log(Math.max(1))
console.log(Math.max(2, 5, 6, -2, 0))
console.log(Math.max(1, 2, "3"))
console.log(Math.max(1, 2, "a"))

Output

-Infinity
1
6
3
NaN

min()


The Math.min(x1, x2, …., xN) method finds the minimum value from the numbers given as parameters. If no parameter is provided, it returns Infinity. If one parameter is provided, it returns itself and if it contains any values that cannot be converted into a numerical value, it returns NaN. Let’s see the examples below.

console.log(Math.min())
console.log(Math.min(1))
console.log(Math.min(2, 5, 6, -2, 0))
console.log(Math.min(1, 2, "3"))
console.log(Math.min(1, 2, "a"))

Output

Infinity
1
-2
1
NaN

random()


The Math.random() method returns a pseudo-random floating-point number between 0(inclusive) and 1(exclusive). i.e.

console.log(Math.random())
console.log(Math.random())

Output

0.5646641980114446
0.7960284546622958

Let’s see how we can generate random numbers between any range i.e. from min(inclusive) to max (exclusive).

max = 150
min = 50
console.log(Math.random()*(max-min) + min)

Output

90.16577232839887

The above code generates random numbers between [50, 150).

To generate random integers between a range just use the Math.floor() method. i.e.

max = 150
min = 50
console.log(Math.floor(Math.random()*(max-min) + min))

Output

61

Note that the initial seed to the random number generator can’t set by the user.

That’s it for this topic. :)


Liked the post?
A computer science student having interest in web development. Well versed in Object Oriented Concepts, and its implementation in various projects. Strong grasp of various data structures and algorithms. Excellent problem solving skills.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).