Posts

Showing posts from April, 2010

Javascript and IEEE754 Redux

Here's the logical evolution of the previously posted code. I wanted to handle single precision in addition to double precision, and did some extensive tidying. Sorry about the round-trips through strings - JavaScript bit operations are limited to 32 bits.

It now correctly handles all of the boundary values on this IEEE-754 reference page as far as possible.

Don't use this - see the code in Typed Array Polyfill instead function toIEEE754(v, ebits, fbits) { var bias = (1 << (ebits - 1)) - 1; // Compute sign, exponent, fraction var s, e, f; if (isNaN(v)) { e = (1 << bias) - 1; f = 1; s = 0; } else if (v === Infinity || v === -Infinity) { e = (1 << bias) - 1; f = 0; s = (v < 0) ? 1 : 0; } else if (v === 0) { e = 0; f = 0; s = (1 / v === -Infinity) ? 1 : 0; } else { s = v < 0; v = Math.abs(v); if (v >= M…