Using the new Math methods in ES6
Canary releases are getting better and better each time. This time, we see the introduction of several useful Math
functions implemented according to the ES6 specification.
I spend quite a large chunk of time working on mathematical applications so these are a very nice addition for those of you in similar shoes.
clz32
clz32
(Count Leading Zeroes 32) does what it says on the tin, it counts all leading zeroes in the 32-bit representation of the provided int.
The easiest way to show this is using the binary notation of a 32-bit integer like follows:
hypot
As the name says, hypot
calculates the hypotenuse (yay pythagoras!), the usual sqrt(a^2 + b^2)
but with the ability to provide any number of arguments like so:
trunc
Native truncation functionality. This would be calculated by using ceil
when the value is less than zero and floor
greater than or equal to zero. However, now it is as simple as:
sign
A very handy function to get the sign of a value, useful in a bunch of places:
As you can see:
1
is positive-1
is negative0
is positive zero-0
is negative zeroNaN
is NaN
A few more
This release also sees the introduction of these handy functions:
log10
to calculate the base 10 logarithmlog2
to calculate the base 2 logarithmlog1p
to calculate the natural logarithmexpm1
to calculateexp(x) - 1
imul
for 32-bit multiplicationfround
to round a number to single precisioncbrt
to perform a cube root