8

Javascript stores all numbers as double-precision 64-bit format IEEE 754 values according to the spec:

The Number type has exactly 18437736874454810627 (that is, 264−253+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic

Is there any way to see the number in this form in Javascript?

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • Possible duplicate of [javascript float from/to bits](https://stackoverflow.com/questions/2003493/javascript-float-from-to-bits) – thorn0 Jan 28 '19 at 12:11

3 Answers3

4

You can use typed arrays to examine the raw bytes of a number. Create a Float64Array with one element, and then create a Uint8Array with the same buffer. You can then set the first element of the float array to your number, and examine the bytes via the Uint8Array. You'll have to do some shifting and combining for the various pieces of the number of course, but it's not hard.

There are no built-in facilities to do things like extract the exponent.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • thanks, I tried the following `var f = new Float64Array(1); f[0] = -12; new Uint8Array(f.buffer).join()` and it returned this `0,0,0,0,0,0,40,192` - I was expecting only zeros and ones, but there are `40` and `192` in it. What am I doing wrong? – Max Koretskyi May 07 '16 at 12:31
  • 1
    Well the `Uint8Array` will contain 8 8-bit values. If you want to see them as binary, you can use `.toString(2)` on each byte - also, you'll have to go through the array in reverse order, as you can see - the "interesting" bytes are at the end. That `192` is the sign bit and the exponent, and then `40` is the most significant part of the mantissa. – Pointy May 07 '16 at 13:06
  • I see, thanks. I'll try that. _you'll have to go through the array in reverse order,_ - is it becausee of `Little Endian Byte Order` ? – Max Koretskyi May 07 '16 at 13:16
  • thanks, now the next question I'll probably ask how to parse from binary IEE 754 float to javascript float :) – Max Koretskyi May 07 '16 at 13:31
  • I've implemented the function based on your suggestion [here](http://stackoverflow.com/a/37092416/2545680), take a look please if I got everything correct? – Max Koretskyi May 07 '16 at 18:49
3

Based on @Pointy's suggestion I've implemented the following function to get a number in it's 64 bit float IEEE754 representation:

function to64bitFloat(number) {
    var f = new Float64Array(1);
    f[0] = number;
    var view = new Uint8Array(f.buffer);
    var i, result = "";
    for (i = view.length - 1; i >= 0; i--) {
        var bits = view[i].toString(2);
        if (bits.length < 8) {
            bits = new Array(8 - bits.length).fill('0').join("") + bits;
        }
        result += bits;
    }
    return result;
}

console.log(to64bitFloat(12)); // 0100000000101000000000000000000000000000000000000000000000000000
console.log(to64bitFloat(-12)); // 1100000000101000000000000000000000000000000000000000000000000000
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
0

You can use Basenumber.js to transform a number into its IEEE754 representation:

let x = Base(326.9);
let y = Base(-326.9);

// specify double precision (64)
let a = x.toIEEE754(64);
let b = y.toIEEE754(64);

console.log(a);
console.log(b);

// You can join them in an unique string this way
console.log(Object.values(a).join(""));
console.log(Object.values(b).join(""));
<script src='https://cdn.jsdelivr.net/gh/AlexSp3/Basenumber.js@main/BaseNumber.min.js'></script>
AlexSp3
  • 2,201
  • 2
  • 7
  • 24