Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to see a number in it's 64 bit float IEEE754 representation

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?

like image 591
Max Koretskyi Avatar asked May 07 '16 11:05

Max Koretskyi


1 Answers

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.

like image 113
Pointy Avatar answered Oct 08 '22 01:10

Pointy