While working on my SqueakJS VM, it became necessary to deconstruct floating point numbers into their mantissa and exponent parts, and assembling them again. Peeking into the C sources of the regular VM, I saw they use the frexp() and ldexp() functions found in the standard C math library.

Unfortunately, JavaScript does not provide these two functions. But surely there must have been someone who needed these before me, right? Sure enough, a Google search came up with a few implementations. However, an hour later I was convinced none of them actually are fully equivalent to the C functions. They were imprecise, that is, deconstructing a float using frexp() and reconstructing it with ldexp() did not result in the original value. But that is the basic use case: for all float values, if

[mantissa, exponent] = frexp(value)then

value = ldexp(mantissa, exponent)even if the value is subnormal. None of the implementations (even the complex ones) really worked.

I had to implement it myself, and here is my implementation (also as JSFiddle):

function frexp(value) {My frexp() uses a DataView to extract the exponent bits of the IEEE-754 float representation. If those bits are 0 then it is a subnormal. In that case I normalize it by multiplying with 2

if (value === 0) return [value, 0];

var data = new DataView(new ArrayBuffer(8));

data.setFloat64(0, value);

var bits = (data.getUint32(0) >>> 20) & 0x7FF;

if (bits === 0) {

data.setFloat64(0, value * Math.pow(2, 64));

bits = ((data.getUint32(0) >>> 20) & 0x7FF) - 64;

}

var exponent = bits - 1022,

mantissa = ldexp(value, -exponent);

return [mantissa, exponent];

}

function ldexp(mantissa, exponent) {

return return exponent > 1023 // avoid multiplying by infinity

? mantissa * Math.pow(2, 1023) * Math.pow(2, exponent - 1023)

: exponent < -1074 // avoid multiplying by zero

? mantissa * Math.pow(2, -1074) * Math.pow(2, exponent + 1074)

: mantissa * Math.pow(2, exponent);

}

^{64}, getting the bits again, and subtracting 64. After applying the bias, the exponent is ready, and used to get the mantissa by canceling out the exponent from the original value.

My ldexp() is pretty straight-forward, except it needs to be able to multiply by very large and very small numbers. The smallest positive float is 0.5

^{-1073}, and to get its mantissa we need to to multiply with 2

^{1073}. That is larger then the largest float 2

^{1023}. By multiplying in two steps we can deal with that.

So there you have it. Hope it's useful to someone. And here is the version I put into SqueakJS, if you're curious.

**Correction:**The code I originally posted here for ldexp() still had a bug, it did not test for too small exponents. Here is the fix.

by Bert (noreply@blogger.com) at October 04, 2014 11:17 PM