unicorn/prefer-modern-math-apis

Require Math.log10(), Math.log2(), Math.hypot() and Math.abs() over hand-written formulas

Formulas built from Math.log() and Math.sqrt() are longer than the method that does the same thing, and less exact. Math.log(1000) / Math.LN10 is 2.9999999999999996, but Math.log10(1000) is 3. Math.sqrt(x * x + y * y) is Infinity as soon as a square overflows, while Math.hypot(x, y) still gives you the answer.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/prefer-modern-math-apis" = "error" # or "warn"

Examples#

example.js4 problems
const digits = Math.log(value) * Math.LOG10E;
const bits = Math.log(value) / Math.LN2;
const distance = Math.sqrt(dx * dx + dy ** 2);
const size = Math.sqrt(delta * delta);
example.jsNo problems
const digits = Math.log10(value);
const bits = Math.log2(value);
const distance = Math.hypot(dx, dy);
const size = Math.abs(delta);