Skip to content

Instantly share code, notes, and snippets.

@afonsomatos
Last active August 29, 2015 14:23
Show Gist options
  • Save afonsomatos/e9664ef67988928a0588 to your computer and use it in GitHub Desktop.
Save afonsomatos/e9664ef67988928a0588 to your computer and use it in GitHub Desktop.
Math methods and constants in ES6
// Get sign of number (-1, +1) or NaN or (-+)zero
Math.sign(-8); // -1
Math.sign(2); // 1
Math.sign(0); // 0
Math.sign(-0); // -0
Math.sign(NaN); // NaN
// Remove decimal fraction of x
Math.trunc(Math.PI); // 3
Math.trunc(-3.99999); // -3
// Cube root of x
Math.cbrt(8); // 2
// Same as
Math.pow(8, 1/3);
// exp function - 1
Math.expm1(1) == Math.exp(1) - 1;
// Same as Math.log(1 + x)
Math.log1p(1);
// Logarithm base 2
Math.log2(4); // 2
// Logarithm base 10
Math.log10(1000); // 3
// -- Not that useful methods ---
Math.fround(x); // Rounds x to a 32 bit float point value
// x * y and return the lower 32 bits of the result
Math.imul(x, y);
// -- Bitwise operations
// Count leading zeros bits in a 32 bit integer
Math.clz32(0b01000000000000000000000000000000); // 1
Math.clz32(2); // 30, 2 = 0b{30x0}10
// -- Trigonometric methods
// Hyperbolic sine of x
Math.sinh(x);
// Hyperbolic cosine of x
Math.cosh(x);
// Hyperbolic tangent of x
Math.tanh(x);
// Inverse hyperbolic sine of x
Math.asinh(x);
// Inverse hyperbolic cosine of x
Math.acosh(x);
// Inverse hyperbolic tangent of x
Math.atanh(x);
// Square root of the sum of the squares of its arguments
Math.hypot(2, 2) == Math.sqrt(8);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment