source: imaps-frontend/node_modules/es-abstract/helpers/valueToFloat32Bytes.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[d565449]1'use strict';
2
[79a0317]3var $abs = require('math-intrinsics/abs');
4var $floor = require('math-intrinsics/floor');
5var $pow = require('math-intrinsics/pow');
[d565449]6
[79a0317]7var isFinite = require('math-intrinsics/isFinite');
8var isNaN = require('math-intrinsics/isNaN');
9var isNegativeZero = require('math-intrinsics/isNegativeZero');
[d565449]10
11var maxFiniteFloat32 = 3.4028234663852886e+38; // roughly 2 ** 128 - 1
12
13module.exports = function valueToFloat32Bytes(value, isLittleEndian) {
14 if (isNaN(value)) {
15 return isLittleEndian ? [0, 0, 192, 127] : [127, 192, 0, 0]; // hardcoded
16 }
17
18 var leastSig;
19
20 if (value === 0) {
21 leastSig = isNegativeZero(value) ? 0x80 : 0;
22 return isLittleEndian ? [0, 0, 0, leastSig] : [leastSig, 0, 0, 0];
23 }
24
25 if ($abs(value) > maxFiniteFloat32 || !isFinite(value)) {
26 leastSig = value < 0 ? 255 : 127;
27 return isLittleEndian ? [0, 0, 128, leastSig] : [leastSig, 128, 0, 0];
28 }
29
30 var sign = value < 0 ? 1 : 0;
31 value = $abs(value); // eslint-disable-line no-param-reassign
32
33 var exponent = 0;
34 while (value >= 2) {
35 exponent += 1;
36 value /= 2; // eslint-disable-line no-param-reassign
37 }
38
39 while (value < 1) {
40 exponent -= 1;
41 value *= 2; // eslint-disable-line no-param-reassign
42 }
43
44 var mantissa = value - 1;
45 mantissa *= $pow(2, 23) + 0.5;
46 mantissa = $floor(mantissa);
47
48 exponent += 127;
49 exponent <<= 23;
50
51 var result = (sign << 31)
52 | exponent
53 | mantissa;
54
55 var byte0 = result & 255;
56 result >>= 8;
57 var byte1 = result & 255;
58 result >>= 8;
59 var byte2 = result & 255;
60 result >>= 8;
61 var byte3 = result & 255;
62
63 if (isLittleEndian) {
64 return [byte0, byte1, byte2, byte3];
65 }
66 return [byte3, byte2, byte1, byte0];
67};
Note: See TracBrowser for help on using the repository browser.