valueToFloat64Bytes.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $parseInt = GetIntrinsic('%parseInt%');
  4. var $abs = GetIntrinsic('%Math.abs%');
  5. var $floor = GetIntrinsic('%Math.floor%');
  6. var callBound = require('call-bind/callBound');
  7. var $strIndexOf = callBound('String.prototype.indexOf');
  8. var $strSlice = callBound('String.prototype.slice');
  9. var fractionToBitString = require('../helpers/fractionToBinaryString');
  10. var intToBinString = require('../helpers/intToBinaryString');
  11. var isNegativeZero = require('./isNegativeZero');
  12. var float64bias = 1023;
  13. var elevenOnes = '11111111111';
  14. var elevenZeroes = '00000000000';
  15. var fiftyOneZeroes = elevenZeroes + elevenZeroes + elevenZeroes + elevenZeroes + '0000000';
  16. // IEEE 754-1985
  17. module.exports = function valueToFloat64Bytes(value, isLittleEndian) {
  18. var signBit = value < 0 || isNegativeZero(value) ? '1' : '0';
  19. var exponentBits;
  20. var significandBits;
  21. if (isNaN(value)) {
  22. exponentBits = elevenOnes;
  23. significandBits = '1' + fiftyOneZeroes;
  24. } else if (!isFinite(value)) {
  25. exponentBits = elevenOnes;
  26. significandBits = '0' + fiftyOneZeroes;
  27. } else if (value === 0) {
  28. exponentBits = elevenZeroes;
  29. significandBits = '0' + fiftyOneZeroes;
  30. } else {
  31. value = $abs(value); // eslint-disable-line no-param-reassign
  32. // Isolate the integer part (digits before the decimal):
  33. var integerPart = $floor(value);
  34. var intBinString = intToBinString(integerPart); // bit string for integer part
  35. var fracBinString = fractionToBitString(value - integerPart); // bit string for fractional part
  36. var numberOfBits;
  37. // find exponent needed to normalize integer+fractional parts
  38. if (intBinString) {
  39. exponentBits = intBinString.length - 1; // move the decimal to the left
  40. } else {
  41. var first1 = $strIndexOf(fracBinString, '1');
  42. if (first1 > -1) {
  43. numberOfBits = first1 + 1;
  44. }
  45. exponentBits = -numberOfBits; // move the decimal to the right
  46. }
  47. significandBits = intBinString + fracBinString;
  48. if (exponentBits < 0) {
  49. // subnormals
  50. if (exponentBits <= -float64bias) {
  51. numberOfBits = float64bias - 1; // limit number of removed bits
  52. }
  53. significandBits = $strSlice(significandBits, numberOfBits); // remove all leading 0s and the first 1 for normal values; for subnormals, remove up to `float64bias - 1` leading bits
  54. } else {
  55. significandBits = $strSlice(significandBits, 1); // remove the leading '1' (implicit/hidden bit)
  56. }
  57. exponentBits = $strSlice(elevenZeroes + intToBinString(exponentBits + float64bias), -11); // Convert the exponent to a bit string
  58. significandBits = $strSlice(significandBits + fiftyOneZeroes + '0', 0, 52); // fill in any trailing zeros and ensure we have only 52 fraction bits
  59. }
  60. var bits = signBit + exponentBits + significandBits;
  61. var rawBytes = [];
  62. for (var i = 0; i < 8; i++) {
  63. var targetIndex = isLittleEndian ? 8 - i - 1 : i;
  64. rawBytes[targetIndex] = $parseInt($strSlice(bits, i * 8, (i + 1) * 8), 2);
  65. }
  66. return rawBytes;
  67. };