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++ i
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.
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
function DoubleToIEEE(f)
{
var buf = new ArrayBuffer(8);
(new Float64Array(buf))[0] = f;
return [ (new Uint32Array(buf))[0] ,(new Uint32Array(buf))[1] ];
}
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.
double
(IEEE 754) to represent all numbersdouble
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)
.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
.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"
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).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.