  Free Academy Experience

Preparing for tech interviews?
Learn from the best instructors in the field.

Join Now!  # Math methods in JS

In JavaScript, we have math methods that can be used without actually creating them. It allows us to perform mathematical tasks on numbers.

`Example: Math.LN2It returns the natural logarithm of 2.`

Some of the math properties(constants) are:

` Math.E        // returns Euler's number Math.PI       // returns PI Math.SQRT2    // returns the square root of 2 Math.SQRT1_2  // returns the square root of 1/2 Math.LN2      // returns the natural logarithm of 2 Math.LN10     // returns the natural logarithm of 10 Math.LOG2E    // returns base 2 logarithm of E Math.LOG10E   // returns base 10 logarithm of E `

Some of the Math methods are:

`abs(x)        // Returns the absolute value of xcbrt(x)       // Returns the cubic root of xceil(x)       // Returns x, rounded upwards to the nearest integerclz32(x)      // Returns the number of leading zeros in a 32-bit binary representation of xexp(x)        // Returns the value of Exexpm1(x)      // Returns the value of Ex minus 1floor(x)      // Returns x, rounded downwards to the nearest integerfround(x)     // Returns the nearest (32-bit single precision) float representation of a numberlog(x)        // Returns the natural logarithmof xlog10(x)      // Returns the base-10 logarithm of xlog1p(x)      // Returns the natural logarithm of 1 + xlog2(x)       // Returns the base-2 logarithm of xpow(x, y)     // Returns the value of x to the power of yrandom()      // Returns a random number between 0 and 1round(x)      // Rounds x to the nearest integersign(x)       // Returns the sign of a number (checks whether it is positive, negative or zero)sqrt(x)       // Returns the square root of xtrunc(x)      // Returns the integer part of a number (x)max(x, y, z, ..., n)  // Returns the number with the highest valuemin(x, y, z, ..., n)  // Returns the number with the lowest value`

Example: Suppose we want to find the max among 1,7 and 77, then Math.max(1,7,77) will give us the required results.

Some of the Trigonometric methods are:

`acos(x)       // Returns the arccosine of x, in radiansacosh(x)      // Returns the hyperbolic arccosine of xasin(x)       // Returns the arcsine of x, in radiansasinh(x)      // Returns the hyperbolic arcsine of xatan(x)       // Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radiansatan2(y, x)   // Returns the arctangent of the quotient of its argumentsatanh(x)      // Returns the hyperbolic arctangent of xcos(x)        // Returns the cosine of x (x is in radians)cosh(x)       // Returns the hyperbolic cosine of xsin(x)        // Returns the sine of x (x is in radians)sinh(x)       // Returns the hyperbolic sine of xtan(x)        // Returns the tangent of an angletanh(x)       // Returns the hyperbolic tangent of a number`

Try the following example in the editor below.

You are given an integer N, you have to print the nearest integral value for the base-2 logarithm of N.

Sample Input:

101

Sample Output:

7 Hints
• Hints are not available for this problem