Last change
on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = parse;
|
---|
7 |
|
---|
8 | function parse(input) {
|
---|
9 | input = input.toUpperCase();
|
---|
10 | var splitIndex = input.indexOf("P");
|
---|
11 | var mantissa, exponent;
|
---|
12 |
|
---|
13 | if (splitIndex !== -1) {
|
---|
14 | mantissa = input.substring(0, splitIndex);
|
---|
15 | exponent = parseInt(input.substring(splitIndex + 1));
|
---|
16 | } else {
|
---|
17 | mantissa = input;
|
---|
18 | exponent = 0;
|
---|
19 | }
|
---|
20 |
|
---|
21 | var dotIndex = mantissa.indexOf(".");
|
---|
22 |
|
---|
23 | if (dotIndex !== -1) {
|
---|
24 | var integerPart = parseInt(mantissa.substring(0, dotIndex), 16);
|
---|
25 | var sign = Math.sign(integerPart);
|
---|
26 | integerPart = sign * integerPart;
|
---|
27 | var fractionLength = mantissa.length - dotIndex - 1;
|
---|
28 | var fractionalPart = parseInt(mantissa.substring(dotIndex + 1), 16);
|
---|
29 | var fraction = fractionLength > 0 ? fractionalPart / Math.pow(16, fractionLength) : 0;
|
---|
30 |
|
---|
31 | if (sign === 0) {
|
---|
32 | if (fraction === 0) {
|
---|
33 | mantissa = sign;
|
---|
34 | } else {
|
---|
35 | if (Object.is(sign, -0)) {
|
---|
36 | mantissa = -fraction;
|
---|
37 | } else {
|
---|
38 | mantissa = fraction;
|
---|
39 | }
|
---|
40 | }
|
---|
41 | } else {
|
---|
42 | mantissa = sign * (integerPart + fraction);
|
---|
43 | }
|
---|
44 | } else {
|
---|
45 | mantissa = parseInt(mantissa, 16);
|
---|
46 | }
|
---|
47 |
|
---|
48 | return mantissa * (splitIndex !== -1 ? Math.pow(2, exponent) : 1);
|
---|
49 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.