JS: Math fround, clz32, imul

By Xah Lee. Date: .
Math.fround(x)

Return the nearest 32bits representation of x.

Math.fround() rounds a JavaScript number (which is always 64-bit double precision) to the nearest 32-bit single-precision floating-point number (the same format used by float in C, Java, etc.).

JavaScript numbers are double precision (64-bit) by default. This gives very high precision, but sometimes you need to match 32-bit float behavior.

Math.clz32(x)

Return the number of leading zero bits in the 32-bit binary representation of x.

Math.clz32() returns the number of leading zero bits in the 32-bit binary representation of a given number. If the input is zero, it returns 32. This function is particularly useful for low-level bitwise operations and systems compiling to JavaScript, such as Emscripten.

Math.imul(a, b)

multiplies a and b as though they were 32 bit signed integers.

Math.imul() performs C-like 32-bit integer multiplication of two parameters. It ensures the result is treated as a signed 32-bit integer, reducing the product modulo $2^{32}$ if it exceeds the range. This method is optimized for performance in JavaScript engines and is essential for projects that compile C/C++ code to JavaScript.

Math.fround Math.clz32 Math.imul are all (new in ECMAScript 2015)