This lesson is a complete reference for the Math object, grouped by purpose, followed by the two areas that need extra explanation: trigonometry (which works in radians, not degrees) and logarithms.
| round(x) | Nearest integer | round(2.5) → 3 |
| floor(x) | Round down | floor(-2.5) → -3 |
| ceil(x) | Round up | ceil(-2.5) → -2 |
| trunc(x) | Drop decimals | trunc(-2.7) → -2 |
| abs(x) | Absolute value | abs(-3) → 3 |
| sign(x) | -1, 0, or 1 | sign(-8) → -1 |
| pow(x, y) | x to the power y (same as x ** y) | pow(2, 10) → 1024 |
| sqrt(x) / cbrt(x) | Square / cube root | cbrt(27) → 3 |
| hypot(a, b, ...) | Square root of the sum of squares | hypot(3, 4) → 5 |
| exp(x) | e to the power x | exp(1) → 2.718... |
| log(x) | Natural log (base e) | log(Math.E) → 1 |
| log10(x) | Base-10 log | log10(1000) → 3 |
| log2(x) | Base-2 log | log2(8) → 3 |
| max(...) / min(...) | Largest / smallest argument | max(1, 9, 4) → 9 |
| random() | Decimal from 0 up to (not including) 1 | see next lesson |
| sin(x) / cos(x) / tan(x) | x is an angle in radians |
| asin(x) / acos(x) / atan(x) | Inverse: returns an angle in radians |
| atan2(y, x) | Angle from the origin to the point (x, y) |
| PI | 3.141592653589793 |
| E | 2.718281828459045 |
| SQRT2 / SQRT1_2 | √2 ≈ 1.414, √½ ≈ 0.707 |
| LN2 / LN10 / LOG2E / LOG10E | log constants, e.g. LN2 ≈ 0.693 |
The full Math object has a few more methods. You will rarely need them in this course, but you should recognize them:
| sinh / cosh / tanh | Hyperbolic sine, cosine, tangent | cosh(0) → 1 |
| asinh / acosh / atanh | Inverse hyperbolic functions | acosh(1) → 0 |
| expm1(x) / log1p(x) | ex - 1 and log(1 + x), exact for tiny x | expm1(0) → 0 |
| fround(x) | Nearest 32-bit (single precision) float | fround(5.05) → 5.050000190734863 |
| f16round(x) | Nearest 16-bit (half precision) float | f16round(5.05) → 5.05078125 |
| clz32(x) | Count leading zero bits in 32-bit binary | clz32(1) → 31 |
| imul(a, b) | 32-bit integer multiply | imul(3, 4) → 12 |
f16round(x) as "rounded downwards to the nearest integer," the same text it uses for floor. That is wrong: it rounds to the nearest 16-bit float, so Math.f16round(2.7) is 2.69921875, not 2. Its list also leaves out hypot and imul, which are real Math methods.
asin and acos only accept -1 to 1, because sine and cosine never go outside that range. Math.acos(2) is NaN, just like Math.sqrt(-1) and Math.log(-1). No error is thrown, so check your inputs.
A full circle is 360 degrees, or 2π radians. JavaScript's trig functions only speak radians, so convert first:
const toRad = deg => deg * Math.PI / 180;
const toDeg = rad => rad * 180 / Math.PI;
Math.sin(30) // -0.988... (30 RADIANS: not what you meant)
Math.sin(toRad(30)) // 0.49999999999999994 (about 0.5, correct)
0.49999999999999994 instead of 0.5, or Math.sin(Math.PI) giving 1.22e-16 instead of 0. That is the decimal precision issue again. Round the result for display.
Two things come up constantly in games and graphics: how far apart two points are, and which direction one is from the other.
const player = { x: 2, y: 3 };
const enemy = { x: 5, y: 7 };
const dx = enemy.x - player.x; // 3
const dy = enemy.y - player.y; // 4
const distance = Math.hypot(dx, dy);
const angle = toDeg(Math.atan2(dy, dx));
console.log(distance, angle.toFixed(1));
atan2(y, x), not atan(y / x). atan2 knows which quadrant the point is in and handles x = 0 without dividing by zero. Note the order: y first.
A log answers "what power do I need?" Math.log10(1000) is 3 because 103 = 1000. A practical use: counting digits.
const digits = n => Math.floor(Math.log10(n)) + 1;
digits(7) // 1
digits(4096) // 4
digits(1000000) // 7
What does this log?
console.log(Math.hypot(6, 8), Math.log2(32));
10 5. The square root of 36 + 64 is 10, and 2 multiplied 5 times is 32.A player is at (0, 0) and a target is straight above at (0, 10). Which call gives the angle safely?
Math.atan(10 / 0)Math.atan2(10, 0)Math.atan2(0, 10)Math.tan(10, 0)atan2 takes y first and handles x = 0 without dividing by zero. C has the arguments backwards (it gives 0, pointing right).Math.sin(Math.PI) returns 1.2246467991473532e-16. What does that mean?
NaNMath.PI is not exactly pi, so the answer is a hair off 0 (0.00000000000000012). Round results for display.Math.cos of 0, 90, and 180 degrees (convert first) and round the resultsMath.hypotMath.log2 and Math.ceildigits() on several numbers, then work out why it fails for 0