Archives For floating point

… or what to do when there’s no DataView.

updated: corrected the output byte order in formatIEEE64().

i recently needed to be able to serialize a JavaScript Number to a 64-bit IEEE 754 floating point format.

there were two main challenges to overcome:

  1. there is no universal way to get direct access to the bits that make up the JavaScript Number. DataView will change this, but it’s not available on IE9. So I needed to find a way to decode the JavaScript number into its constituent sign, exponent, and mantissa, with proper handling of denormalized values.
  2. the mantissa, once calculated, is a 52-bit value, so we can’t use JavaScript’s bitwise operators to access the bits of the mantissa. The bitwise operators all coerce their arguments to 32-bit values.

jDataView is a library that has the ability to read 64-bit IEEE values from a buffer, and it has been recently updated to also support writing 64-bit IEEE values.

here is my solution to the problem until DataView becomes widely available.

Continue Reading…