float_parser.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. // Copyright (c) 2008, Fair Oaks Labs, Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. //
  11. // * Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
  16. // may be used to endorse or promote products derived from this software
  17. // without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. // POSSIBILITY OF SUCH DAMAGE.
  30. //
  31. //
  32. // Modifications to writeIEEE754 to support negative zeroes made by Brian White
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. exports.writeIEEE754 = exports.readIEEE754 = void 0;
  35. function readIEEE754(buffer, offset, endian, mLen, nBytes) {
  36. var e;
  37. var m;
  38. var bBE = endian === 'big';
  39. var eLen = nBytes * 8 - mLen - 1;
  40. var eMax = (1 << eLen) - 1;
  41. var eBias = eMax >> 1;
  42. var nBits = -7;
  43. var i = bBE ? 0 : nBytes - 1;
  44. var d = bBE ? 1 : -1;
  45. var s = buffer[offset + i];
  46. i += d;
  47. e = s & ((1 << -nBits) - 1);
  48. s >>= -nBits;
  49. nBits += eLen;
  50. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8)
  51. ;
  52. m = e & ((1 << -nBits) - 1);
  53. e >>= -nBits;
  54. nBits += mLen;
  55. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8)
  56. ;
  57. if (e === 0) {
  58. e = 1 - eBias;
  59. }
  60. else if (e === eMax) {
  61. return m ? NaN : (s ? -1 : 1) * Infinity;
  62. }
  63. else {
  64. m = m + Math.pow(2, mLen);
  65. e = e - eBias;
  66. }
  67. return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
  68. }
  69. exports.readIEEE754 = readIEEE754;
  70. function writeIEEE754(buffer, value, offset, endian, mLen, nBytes) {
  71. var e;
  72. var m;
  73. var c;
  74. var bBE = endian === 'big';
  75. var eLen = nBytes * 8 - mLen - 1;
  76. var eMax = (1 << eLen) - 1;
  77. var eBias = eMax >> 1;
  78. var rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
  79. var i = bBE ? nBytes - 1 : 0;
  80. var d = bBE ? -1 : 1;
  81. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
  82. value = Math.abs(value);
  83. if (isNaN(value) || value === Infinity) {
  84. m = isNaN(value) ? 1 : 0;
  85. e = eMax;
  86. }
  87. else {
  88. e = Math.floor(Math.log(value) / Math.LN2);
  89. if (value * (c = Math.pow(2, -e)) < 1) {
  90. e--;
  91. c *= 2;
  92. }
  93. if (e + eBias >= 1) {
  94. value += rt / c;
  95. }
  96. else {
  97. value += rt * Math.pow(2, 1 - eBias);
  98. }
  99. if (value * c >= 2) {
  100. e++;
  101. c /= 2;
  102. }
  103. if (e + eBias >= eMax) {
  104. m = 0;
  105. e = eMax;
  106. }
  107. else if (e + eBias >= 1) {
  108. m = (value * c - 1) * Math.pow(2, mLen);
  109. e = e + eBias;
  110. }
  111. else {
  112. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
  113. e = 0;
  114. }
  115. }
  116. if (isNaN(value))
  117. m = 0;
  118. while (mLen >= 8) {
  119. buffer[offset + i] = m & 0xff;
  120. i += d;
  121. m /= 256;
  122. mLen -= 8;
  123. }
  124. e = (e << mLen) | m;
  125. if (isNaN(value))
  126. e += 8;
  127. eLen += mLen;
  128. while (eLen > 0) {
  129. buffer[offset + i] = e & 0xff;
  130. i += d;
  131. e /= 256;
  132. eLen -= 8;
  133. }
  134. buffer[offset + i - d] |= s * 128;
  135. }
  136. exports.writeIEEE754 = writeIEEE754;
  137. //# sourceMappingURL=float_parser.js.map