14 - Math in Javascript

In the early days computers were mainly used to perform large and complex arithmetic operations. This was because computers could compute much faster than humans could and in fact accurately all the time. This has indeed carried over and though computers nowadays do much more than arithmetic operations, there are functions to perform arithmetic or more precisely mathematical operations.

Math Object

The Math object in Javascript is said to be static. One cannot create new instances of the object and all the properties and functions can be accessed using the dot operator and by passing the operators as arguments to the functions.

Constant proFunction

Constant properties of Math

In mathematics and science several constants can be used to perform calculations. These constants can be accessed like properties. Here are the common properties and their values printed out on a web page.

Constant

Purpose

Math.E

This is the Euler’s constant. Its value approximates to 2.178. This is also the base value for natural log

Math.PI

The constant which represents the ratio of the circumference of the circle to its diameter. The value approximates to 3.14159

Math.LOG2E

This constant represents the base 2 log of E. Value approximates to 1.433.

Math.LOG10E

This constant represents the base 10 log of E. Value approximates to 0.434.

Math.SQRT2

This constant represents the square root of 2. Value approximates to 1.414.

Math.SQRT1_2

This constant represents the square root of ½ or 0.5. Value approximates to 0.707

Math.LN2

This constant represents the natural log of 2. Value approximates to 0.693.

Math.LN10

This constant represents the natural log of 10. Value approximates to 2.303.

<!DOCTYPE html>
<html>
    <head>        
        <script type="text/javascript">
        </script>
    </head>     
    <body>
        Displaying the values of the Math object
        <hr>
        <script type = "text/javascript">
          document.write("The value of PI is " + Math.PI + ".<br/>");
          document.write("The value of the Euler's constant is " + Math.E + ".<br/>");
          document.write("The value of Log2E is " + Math.LOG2E + ".<br/>");
          document.write("The value of Log10E is " + Math.LOG10E + ".<br/>");
          document.write("The value of LN2 is " + Math.LN2 + ".<br/>");
          document.write("The value of LN10 is " + Math.LN10 + ".<br/>");
          document.write("The value of square root of 2 is " + Math.SQRT2 + ".<br/>");
          document.write("The value of square root of 1/2 or 0.5 is " + Math.SQRT1_2 + ".<br/>");
        </script>
    </body>
</html>


Trigonometric functions of Math

Function

Purpose

Math.sin(x)

This function returns the sine value of the number passed as argument in radians.

Math.sinh(x)

This function returns the hyperbolic sine value of the argument in radians.

Math.asin(x)

This function returns the arc sine value of the argument in radians.

Math.cos(x)

This function returns the cosine value of the number passed as argument in radians.

Math.cosh(x)

This function returns the hyperbolic

cosine value of the argument in radians.

Math.acos(x)

This function returns the arc cosine value of the argument in radians.

Math.tan(x)

This function returns the tangent value of the number passed as argument in radians.

Math.tanh(x)

This function returns the hyperbolic tangent value of the argument in radians.

Math.atan(x)

This function returns the arc tangent value of the argument in radians.

Math.atan2(y, x)

This function returns the arc tangent value of the quotient of the arguments in radians.

 <!DOCTYPE html>
<html>
    <head>        
        <script type="text/javascript">
        </script>
    </head>    
    <body>
        Displaying the functions and their outcomes of Math trigonometric functions
        <hr>
        <script type = "text/javascript">
          var x = 0;
          document.write("The sine value of x is " + Math.sin(x) + ".<br/>");
          document.write("The arc sine value of x is " + Math.asin(x) + ".<br/>");
          document.write("The hyperbolic sine value of x is " + Math.sinh(x) + ".<br/>");
          document.write("The cosine value of x is " + Math.cos(x) + ".<br/>");
          document.write("The arc cosine value of x is " + Math.acos(x) + ".<br/>");
          document.write("The hyperbolic cosine value of x is " + Math.cosh(x) + ".<br/>");
          document.write("The tangent value of x is " + Math.sin(x) + ".<br/>");
          document.write("The arc tangent value of x is " + Math.asin(x) + ".<br/>");
          document.write("The hyperbolic tangent value of x is " + Math.sinh(x) + ".<br/>");
          document.write("The hyperbolic tangent value of 90/x is " + Math.sinh(90, x) + ".<br/>");
        </script>
    </body>
</html>


Mathematical functions

Function

Purpose

Math.abs(x)

This function returns the absolute value of x

Math.ceil(x)

This function returns the integer closest to x but not less than it.

Math.floor(x)

This function returns the integer that is closest to x but not greater than it.

Math.exp(x)

This function returns the value of Euler’s constant power X. (Math.E Power x)

Math.max(x,y)

This function returns the greater value among x and y.

Math.min(x,y)

This function returns the lesser value among x and y.

Math.pow(x,y)

This function returns the value of x to the power of y.

Math.random()

This function returns a pseudorandom number

Math.round(x)

This function rounds to the closest integer.

Math.sqrt(x)

This function returns the square root of x.

Math.cbrt(x)

This function returns the cube root of x.

<!DOCTYPE html>
<html>
    <head>        
        <script type="text/javascript">
        </script>
    </head>    
    <body>
        Displaying the functions and their outcomes of Math functions
        <hr>
        <script type = "text/javascript">
          var x = 10.2;
          var y = 11;
          document.write("The value of x is " + x + ".<br/>");
          document.write("The absolute value of x is " + Math.abs(x) + ".<br/>");
          document.write("The ceil value of x is " + Math.ceil(x) + ".<br/>");
          document.write("The floor value of x is " + Math.floor(x) + ".<br/>");
          document.write("The exponential value of x is " + Math.exp(x) + ".<br/>");
          document.write("The maximum among x and y is "+ Math.max(x,y) + ".<br/>");
          document.write("The minimum among x and y is " + Math.min(x,y) + ".<br/>");
          document.write("The value of x to the power y is " + Math.pow(x,y) + ".<br/>");
          document.write("A random value generated is " + Math.random() + ".<br/>");
          document.write("The rounded value of x is " + Math.round(x) + ".<br/>");
          document.write("The square root of x is " + Math.sqrt(x) + ".<br/>");
          document.write("The cube root of x is " + Math.cbrt(x) + ".<br/>");
        </script>
    </body>
</html>


Like us on Facebook