Sub topic 15
Trigonometry, Radians, and Math.atan2
This chapter assumes you already know the Math object and its constants, especially Math.PI. You should also know from the floating-point chapter that results can carry tiny rounding errors. You will learn how JavaScript's trigonometric functions expect their input, why exact-looking answers sometimes come out slightly wrong, and why Math.atan2 is the right tool for finding angles.
Radians, Not Degrees
Every trig function on Math measures angles in radians. A full circle is 2 * Math.PI radians, or 360 degrees. If you pass 90 to Math.sin, it treats the value as 90 radians and gives you a number that looks meaningless.
const toRadians = (deg) => deg * (Math.PI / 180);
const toDegrees = (rad) => rad * (180 / Math.PI);
console.log(Math.sin(90)); // 0.8939966636005579 (90 radians!)
console.log(Math.sin(toRadians(90))); // 1
console.log(Math.cos(toRadians(60))); // 0.5000000000000001
console.log(toDegrees(Math.PI / 4)); // 45The 0.5000000000000001 result happens because Math.PI is only an approximation of π. The tiny error grows as the value goes through each function.
Expected Zeros That Aren't Zero
The same approximation means values that should be exactly zero usually are not:
console.log(Math.sin(Math.PI)); // 1.2246467991473532e-16
console.log(Math.cos(Math.PI / 2)); // 6.123233995736766e-17
console.log(Math.tan(Math.PI / 2)); // 16331239353195370, not InfinityTreat these results like any other float. Compare them within a tolerance, and round them only when you display them.
Inverse Functions and Their Ranges
Math.asin, Math.acos, and Math.atan go the other way: they take a ratio and return an angle in radians. Each has a limited input domain and a fixed output range.
| Function | Valid input | Output range (radians) |
|---|---|---|
| `Math.asin(x)` | -1 to 1 | -π/2 to π/2 |
| `Math.acos(x)` | -1 to 1 | 0 to π |
| `Math.atan(x)` | any number | -π/2 to π/2 |
| `Math.atan2(y, x)` | any two numbers | -π to π |
Input outside the domain gives NaN. For example, Math.asin(1.0000001) returns NaN, and floating-point drift can push a computed ratio just past 1. Clamping the value first avoids this: Math.acos(Math.min(1, Math.max(-1, r))).
Why atan2 Beats atan
Math.atan(y / x) loses information. The points (1, 1) and (-1, -1) give the same ratio, so atan cannot tell them apart. It also divides by zero when x is 0. Math.atan2(y, x) takes the two parts separately and uses their signs to work out the correct quadrant.
Note the argument order: y comes first. Swapping the arguments is the most common atan2 bug.
const deg = (rad) => rad * 180 / Math.PI;
console.log(deg(Math.atan(1 / 1))); // 45
console.log(deg(Math.atan(-1 / -1))); // 45 (wrong quadrant)
console.log(deg(Math.atan2(-1, -1))); // -135 (correct)
console.log(deg(Math.atan2(1, 0))); // 90 (no division by zero)
// Normalize to 0–360 for compass-style headings
const heading = (deg(Math.atan2(-1, -1)) + 360) % 360;
console.log(heading); // 225A typical use is finding the angle from one point to another, such as turning a sprite to face the mouse pointer:
function angleBetween(from, to) {
return Math.atan2(to.y - from.y, to.x - from.x);
}
const a = angleBetween({ x: 0, y: 0 }, { x: 3, y: 3 });
console.log(a * 180 / Math.PI); // 45Exercise
Write a function that returns the angle, in whole degrees from 0 to 359, from point (2, 5) to point (-1, 2). What value does it return, and why would Math.atan give the wrong answer?
Show answer
Use Math.atan2(2 - 5, -1 - 2), which is Math.atan2(-3, -3) = -3π/4 radians, or -135 degrees. Normalize it with (deg + 360) % 360 and round to get 225. Math.atan(-3 / -3) equals Math.atan(1), which gives 45 degrees. Dividing removes both negative signs, so atan cannot tell that the target is in the lower-left quadrant.
Exercise
Why does Math.cos(Math.PI / 2) === 0 evaluate to false, and how should you test it?
Show answer
Math.PI is a rounded approximation of π, so the cosine comes out as about 6.12e-17 instead of 0. Test it within a tolerance, for example Math.abs(Math.cos(Math.PI / 2)) < 1e-12.