Some Useful Maths Functions In JavaScript

14th January 2008 - 4 minutes read time

All of the maths functions in JavaScript are kept in a handy object called Math, which contains a number of different functions.

To get the absolute value of a number use the abs() function.

Math.abs(3.14159265) // returns 3.14159265

Rounding a number is done by either the round() function to round to the nearest integer, the ceil() function to round up to the nearest integer and the floor() function to round down to the nearest integer.

Math.ceil(3.14159265)    // returns 4
Math.floor(3.14159265)   // returns 3
Math.round(3.14159265) // returns 3

To find the exponent of a number use the exp() function.

Math.exp(3.14159265) // returns 23.140692549708973

The log() method returns the natural logarithm (base E) of a number.

Math.log(3.14159265) // returns 1.1447298847067335

Raising a number by a value is done with the pow() function, the two parameters are the number and the power.

Math.pow(3.14159265) // returns 4.114463202429791

Finding the square root of a number is done by using the sqrt() function.

Math.sqrt(3.14159265) // returns 1.7724538498928541

A pseudo random number between 0 and 1 can be found by using the random() function.

Math.random() // returns a number between 0 and 1

A group of trigonometric functions also exist.

Math.tan(1.5707963267948966) // returns 16331778728383844
Math.cos(1.5707963267948966) // returns 6.123031769111886e-17
Math.sin(1.5707963267948966) // returns 1
Math.atan(1.5707963267948966) // returns 1.0038848218538872
Math.atan2(1.5707963267948966,1.23567) // arc tangent of a/b - returns 0.9042475963309642
Math.acos(0.5) // returns 1.0471975511965976
Math.asin(0.5) // returns 0.5235987755982989

All of the trigonometric functions work with radians and not degrees so you will need to convert degrees to radians if that is what you are working in. To convert radians to degrees multiply the value by PI divided by 180. To convert degrees to radians multiply the value by 180 divided by PI.

// degrees to radians 
var rad = (90*(Math.PI/180)); // returns 1.5707963267948966
// radians to degrees
var deg = (1.5707963267948966*(180/Math.PI)); // returns 90

To compare numbers you can use the min() and max() functions. The min() function will return the smallest number from a set of values and max() will returns the largest number from a set of values.

Math.max(1,2,3,4,5,6,7,8,9) // returns 9
Math.min(1,2,3,4,5,6,7,8,9) // returns 1

Additionally, there are a few constant values available in the Math object, these are as follows.

  • Math.E - Euler's constant (also known as e) = 2.718281828459045
  • Math.LN2 - Natural log of the value 2 = 0.6931471805599453
  • Math.LN10 - Natural log of the value 10 = 2.302585092994046
  • Math.LOG2E - The base 2 log of e = 2.302585092994046
  • Math.LOG10E - The base 10 log of e = 0.4342944819032518
  • Math.PI - Pi = 3.141592653589793
  • Math.SQRT1_2 - The square root of one half = 0.7071067811865476
  • Math.SQRT2 - The square root of 2 = 1.4142135623730951

 

There are also some constants kept in the Number object. These are mainly used to validate integers and are as follows.

  • Number.MAX_VALUE - The largest value of an integer that can be used in JavaScript. This value is 1.7976931348623157e+308
  • Number.MIN_VALUE - The smallest value of an integer that can be used in JavaScript. This value is 5e-324.
  • Number.NaN - Used to indicate that a value is not a number.
  • Number.NEGATIVE_INFINITY - The value returned if a negative overflow occurs. Any numeric value divided by this is 0.
  • Number.POSITIVE_INFINITY - The value returned if a positive value overflow occurs. Any numeric value divided by this is 0.

 

All of these constants can be used as validation. For example, trying to run a function with an integer that is above the maximum limit can cause errors, so a simple checksum can be put in place.

if (value1*value2 <= Number.MAX_VALUE) {
  function1(value1)
} else {
  function2(value1)
}

Comments

Permalink
Hi, I was just wondering if when you use the pi constant, if 3.141592653589793 is the entirety of the pi value it stores? I know that once you get ot more than 35 or so decimal places it doesn't really matter, as that is enough to calcualate to a very high accuracy the circumference or whatever of a circle. But is 15 really the maximum that is stored as a constant?

James (Mon, 01/14/2008 - 17:33)

Permalink
All major browsers seem to store the value down to 16 decimal places. For example.
PI 3.141592653589793 - firefox 2.0
PI 3.141592653589793 - opera 9.2
PI 3.141592653589793 - IE 7
If you want to go for increased precision then you can work out the value of PI for yourself before using it in a calculation. Here is a link to a JavaScript function that will work it out.http://www.trans4mind.com/personal_development/JavaScript/longnumPiMach…This method is quite processor intensive and should only be used if you really need that level of precision.
Permalink
can you tell me what the significance 1.5707963267948966 has to pi ? or does it ? why is that number significant ? i don't know algebra, but i got this number adding and subtracting ... thanks

trisha craig (Thu, 03/20/2014 - 06:01)

Add new comment

The content of this field is kept private and will not be shown publicly.