Skip to content Skip to sidebar Skip to footer

Javascript Float From/to Bits

I am trying to perform something that is brain-dead simple in any other language but not javascript: get the bits out of float (and the other way around). In C/C++ it would be some

Solution 1:

function DoubleToIEEE(f)
{
    var buf = new ArrayBuffer(8);
    (new Float64Array(buf))[0] = f;
    return [ (new Uint32Array(buf))[0] ,(new Uint32Array(buf))[1] ];
}

Solution 2:

You certainly don't get anything low-level like that in JavaScript. It would be extremely dangerous to allow recasting and pointer-frobbing in a language that has to be safe for untrusted potential-attacker web sites to use.

If you want to get a 32-bit IEEE754 representation of a single-precision value in a Number (which remember is not an int either; the only number type you get in JavaScript is double), you will have to make it yourself by fiddling the sign, exponent and mantissa bits together. There's example code here.


Solution 3:

function FloatToIEEE(f)
{
    var buf = new ArrayBuffer(4);
    (new Float32Array(buf))[0] = f;
    return (new Uint32Array(buf))[0];
}

Unfortunately, this doesn't work with doubles and in old browsers.


Solution 4:

  1. JavaScript uses double (IEEE 754) to represent all numbers
  2. double consists of [sign, exponent(11bit), mantissa(52bit)] fields. Value of number is computed using formula (-1)^sign * (1.mantissa) * 2^(exponent - 1023). (1.mantissa - means that we take bits of mantissa add 1 at the beginning and tread that value as number, e.g. if mantissa = 101 we get number 1.101 (bin) = 1 + 1/2 + 1/8 (dec) = 1.625 (dec).
  3. We can get value of sign bit testing if number is greater than zero. There is a small issue with 0 here because double have +0 and -0 values, but we can distinguish these two by computing 1/value and checking if value is +Inf or -Inf.
  4. Since 1 <= 1.mantissa < 2 we can get value of exponent using Math.log2 e.g. Math.floor(Math.log2(666.0)) = 9 so exponent is exponent - 1023 = 9 and exponent = 1032, which in binary is (1032).toString(2) = "10000001000"
  5. After we get exponent we can scale number to zero exponent without changing mantissa, value = value / Math.pow(2, Math.floor(Math.log2(666.0))), now value represents number (-1)^sign * (1.mantissa). If we ignore sign and multiply that by 2^52 we get integer value that have same bits as 1.mantissa: ((666 / Math.pow(2, Math.floor(Math.log2(666)))) * Math.pow(2, 52)).toString(2) = "10100110100000000000000000000000000000000000000000000" (we must ignore leading 1).
  6. After some string concat's you will get what you want

This is only proof of concept, we didn't discuss denormalized numbers or special values such as NaN - but I think it can be expanded to account for these cases too.

@bensiu answers is fine, but if find yourself using some old JS interpreter you can use this approach.


Solution 5:

Like the other posters have said, JavaScript is loose typed, so there is no differentiation in data types from float to int or vice versa.

However, what you're looking for is

float to int:

Math.floor( 3.9 ); // result: 3 (truncate everything past .) or
Math.round( 3.9 ); // result: 4 (round to nearest whole number)

Depending on which you'd like. In C/C++ it would essentially be using Math.floor to convert to integer from float.

int to float:

var a = 10;
a.toFixed( 3 ); // result: 10.000

Post a Comment for "Javascript Float From/to Bits"