source: trip-planner-front/node_modules/core-js/internals/ieee754.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1// IEEE754 conversions based on https://github.com/feross/ieee754
2var abs = Math.abs;
3var pow = Math.pow;
4var floor = Math.floor;
5var log = Math.log;
6var LN2 = Math.LN2;
7
8var pack = function (number, mantissaLength, bytes) {
9 var buffer = new Array(bytes);
10 var exponentLength = bytes * 8 - mantissaLength - 1;
11 var eMax = (1 << exponentLength) - 1;
12 var eBias = eMax >> 1;
13 var rt = mantissaLength === 23 ? pow(2, -24) - pow(2, -77) : 0;
14 var sign = number < 0 || number === 0 && 1 / number < 0 ? 1 : 0;
15 var index = 0;
16 var exponent, mantissa, c;
17 number = abs(number);
18 // eslint-disable-next-line no-self-compare -- NaN check
19 if (number != number || number === Infinity) {
20 // eslint-disable-next-line no-self-compare -- NaN check
21 mantissa = number != number ? 1 : 0;
22 exponent = eMax;
23 } else {
24 exponent = floor(log(number) / LN2);
25 if (number * (c = pow(2, -exponent)) < 1) {
26 exponent--;
27 c *= 2;
28 }
29 if (exponent + eBias >= 1) {
30 number += rt / c;
31 } else {
32 number += rt * pow(2, 1 - eBias);
33 }
34 if (number * c >= 2) {
35 exponent++;
36 c /= 2;
37 }
38 if (exponent + eBias >= eMax) {
39 mantissa = 0;
40 exponent = eMax;
41 } else if (exponent + eBias >= 1) {
42 mantissa = (number * c - 1) * pow(2, mantissaLength);
43 exponent = exponent + eBias;
44 } else {
45 mantissa = number * pow(2, eBias - 1) * pow(2, mantissaLength);
46 exponent = 0;
47 }
48 }
49 for (; mantissaLength >= 8; buffer[index++] = mantissa & 255, mantissa /= 256, mantissaLength -= 8);
50 exponent = exponent << mantissaLength | mantissa;
51 exponentLength += mantissaLength;
52 for (; exponentLength > 0; buffer[index++] = exponent & 255, exponent /= 256, exponentLength -= 8);
53 buffer[--index] |= sign * 128;
54 return buffer;
55};
56
57var unpack = function (buffer, mantissaLength) {
58 var bytes = buffer.length;
59 var exponentLength = bytes * 8 - mantissaLength - 1;
60 var eMax = (1 << exponentLength) - 1;
61 var eBias = eMax >> 1;
62 var nBits = exponentLength - 7;
63 var index = bytes - 1;
64 var sign = buffer[index--];
65 var exponent = sign & 127;
66 var mantissa;
67 sign >>= 7;
68 for (; nBits > 0; exponent = exponent * 256 + buffer[index], index--, nBits -= 8);
69 mantissa = exponent & (1 << -nBits) - 1;
70 exponent >>= -nBits;
71 nBits += mantissaLength;
72 for (; nBits > 0; mantissa = mantissa * 256 + buffer[index], index--, nBits -= 8);
73 if (exponent === 0) {
74 exponent = 1 - eBias;
75 } else if (exponent === eMax) {
76 return mantissa ? NaN : sign ? -Infinity : Infinity;
77 } else {
78 mantissa = mantissa + pow(2, mantissaLength);
79 exponent = exponent - eBias;
80 } return (sign ? -1 : 1) * mantissa * pow(2, exponent - mantissaLength);
81};
82
83module.exports = {
84 pack: pack,
85 unpack: unpack
86};
Note: See TracBrowser for help on using the repository browser.