Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

float_parser.js 3.8KB

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