Math

Page section: posts
More on math

Math operators

The math operators are:

Symbol Action Example
+ add
- subtract
* multiply
/ divide
% remainder
** to the power of
++ adds 1 myVar++
-- subtracts 1 myVar--
+= Adds the current value to the value following myVar += 10
-= Subtracts the current value from the value following myVar -= 10
*= Multiplies the current value by the value following myVar *= 10
/= Divides the current value by the value following myVar *= 10

BIDMAS

Math calculations are performed in a set order: B I D M A S:

  1. brackets
  2. indices (power of)
  3. division
  4. multiplication
  5. addtion
  6. subtraction

Math functions:

There are lots of math functions:

Math.PI() // give pi 3.141592653589793
Math.round() // rounds a decimal number to it's nearest integer value
Math.ceil()  // rounds a floating point number to it's highest integer value
Math.floor()  // rounds a floating point number to it's lowest integer value
Math.pow(x, y)  // multiplies x to the power of y.
Math.sqrt() // gets the square root of a number.
Math.abs() // gets the absolute value (ie positive). So -3 will be 3.
Math.sin() // the sine value of a number
Math.cos() // the cosine value of a number
Math.min(x, y, z) // finds the lowest number in a list
Math.max(x, y, z) // finds the highest number in a list
Math.random()  // generates a random number between 0 and 1

To round a decimal to its nearest, lowest whole number:

let age = 20.589568
age = Math.Floor(age);

// output 20

random