Created
October 5, 2016 04:57
-
-
Save maxkoretskyi/e69d0bd55d559fce69efe77573fa9a52 to your computer and use it in GitHub Desktop.
Representing a number as 64 bit float
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function to64bitFloat(number) { | |
var i, result = ""; | |
var dv = new DataView(new ArrayBuffer(8)); | |
dv.setFloat64(0, number, false); | |
for (i = 0; i < 8; i++) { | |
var bits = dv.getUint8(i).toString(2); | |
if (bits.length < 8) { | |
bits = new Array(8 - bits.length).fill('0').join("") + bits; | |
} | |
result += bits; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found that separating the sign, exponent, and mantissa fields made the results easier to read: