1 | /* |
||
2 | * Copyright (c) 2017-2018 Rafael da Silva Rocha. |
||
3 | * Copyright (c) 2013 DeNA Co., Ltd. |
||
4 | * |
||
5 | * Permission is hereby granted, free of charge, to any person obtaining |
||
6 | * a copy of this software and associated documentation files (the |
||
7 | * "Software"), to deal in the Software without restriction, including |
||
8 | * without limitation the rights to use, copy, modify, merge, publish, |
||
9 | * distribute, sublicense, and/or sell copies of the Software, and to |
||
10 | * permit persons to whom the Software is furnished to do so, subject to |
||
11 | * the following conditions: |
||
12 | * |
||
13 | * The above copyright notice and this permission notice shall be |
||
14 | * included in all copies or substantial portions of the Software. |
||
15 | * |
||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||
20 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||
21 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||
22 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
23 | * |
||
24 | */ |
||
25 | |||
26 | /** |
||
27 | * @fileoverview Functions to work with IEEE 754 floating point numbers. |
||
28 | * @see https://github.com/rochars/byte-data |
||
29 | * @see https://github.com/kazuho/ieee754.js |
||
30 | */ |
||
31 | |||
32 | /** |
||
33 | * Unpack a IEEE754 floating point number. |
||
34 | * |
||
35 | * Derived from IEEE754 by DeNA Co., Ltd., MIT License. |
||
36 | * Adapted to handle NaN. Should port the solution to the original repo. |
||
37 | * @see https://github.com/kazuho/ieee754.js/blob/master/ieee754.js |
||
38 | * @param {!Uint8Array|!Array<number>} bytes The byte buffer to unpack. |
||
39 | * @param {number} offset the start index to read. |
||
40 | * @param {number} numBytes the number of bytes used by the number. |
||
41 | * @param {number} exponentBits The number of bits of the exponent. |
||
42 | * @param {number} exponentBias The exponent bias. |
||
43 | */ |
||
44 | export function unpackIEEE754(bytes, offset, numBytes, exponentBits, exponentBias) { |
||
45 | let eMax = (1 << exponentBits) - 1; |
||
46 | let bias = Math.pow(2, -(8 * numBytes - 1 - exponentBits)); |
||
47 | let significand; |
||
48 | let leftBits = ""; |
||
49 | for (let i = numBytes - 1; i >= 0 ; i--) { |
||
50 | let t = bytes[i + offset].toString(2); |
||
51 | leftBits += "00000000".substring(t.length) + t; |
||
52 | } |
||
53 | let sign = leftBits.charAt(0) == "1" ? -1 : 1; |
||
54 | leftBits = leftBits.substring(1); |
||
55 | let exponent = parseInt(leftBits.substring(0, exponentBits), 2); |
||
56 | leftBits = leftBits.substring(exponentBits); |
||
57 | if (exponent == eMax) { |
||
58 | if (parseInt(leftBits, 2) !== 0) { |
||
59 | return NaN; |
||
60 | } else { |
||
0 ignored issues
–
show
Comprehensibility
introduced
by
Loading history...
|
|||
61 | return sign * Infinity; |
||
62 | } |
||
63 | } else if (exponent == 0) { |
||
0 ignored issues
–
show
|
|||
64 | exponent += 1; |
||
65 | significand = parseInt(leftBits, 2); |
||
66 | } else { |
||
67 | significand = parseInt("1" + leftBits, 2); |
||
68 | } |
||
69 | return sign * significand * bias * Math.pow(2, exponent - exponentBias); |
||
70 | } |
||
71 |