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 something like
float a = 3.1415; int b = *((int*)&a);
and vise-versa
int a = 1000; float b = *((float*)&a);
In C# you can use the BitConverter ...floatBits or something alike in Java... Even in VB6 for Christ's sake you can memcpy a float32 into an int32. How on earth can I translate between and int and a float in javascript?
function DoubleToIEEE(f)
{
var buf = new ArrayBuffer(8);
(new Float64Array(buf))[0] = f;
return [ (new Uint32Array(buf))[0] ,(new Uint32Array(buf))[1] ];
}
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.
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.
- JavaScript uses
double
(IEEE 754) to represent all numbers -
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 number1.101 (bin) = 1 + 1/2 + 1/8 (dec) = 1.625 (dec)
. - We can get value of
sign
bit testing if number is greater than zero. There is a small issue with0
here becausedouble
have+0
and-0
values, but we can distinguish these two by computing1/value
and checking if value is+Inf
or-Inf
. - Since
1 <= 1.mantissa < 2
we can get value of exponent usingMath.log2
e.g.Math.floor(Math.log2(666.0)) = 9
so exponent isexponent - 1023 = 9
andexponent = 1032
, which in binary is(1032).toString(2) = "10000001000"
- 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 by2^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). - 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.