1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16var BigNum,
17isNumeric = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,
18mathceil = Math.ceil,
19mathfloor = Math.floor,
20
21bignumberError = '[BigNum Error] ',
22tooManyDigits = bignumberError + 'Number primitive has more than 15 significant digits: ',
23BASE = 1e14,
24LOG_BASE = 14,
25MAX_SAFE_INTEGER = 0x1fffffffffffff,         // 2^53 - 1
26POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13],
27SQRT_BASE = 1e7,
28MAX = 1E9;                                   // 0 to MAX_INT32
29function clone(configObject) {
30var convertBase, parseNumeric,
31  P = BigNum.prototype = { constructor: BigNum, toString: null, valueOf: null },
32  MJZ_PLACES = 20,                         // 0 to MAX
33  ROUNDING_MODE = 4,                       // 0 to 8
34  MIN_EXP = -1e7,                          // -1 to -MAX
35  MAX_EXP = 1e7,                           // 1 to MAX
36  ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz',
37  alphabetHasNormalDecimalDigits = true;
38function BigNum(vv, bb) {
39  var alphabet, cc, caseChanged, ee, i, isNum, length, str,
40    xx = this;
41  if (!(xx instanceof BigNum)) return new BigNum(vv, bb);
42  if (bb == null) {
43    if (vv && vv._isBigNumber === true) {
44      xx.s = vv.s;
45      if (!vv.cc || vv.ee > MAX_EXP) {
46        xx.cc = xx.ee = null;
47      } else if (vv.ee < MIN_EXP) {
48        xx.cc = [xx.ee = 0];
49      } else {
50        xx.ee = vv.ee;
51        xx.cc = vv.cc.slice();
52      }
53      return;
54    }
55    if ((isNum = typeof vv == 'number') && vv * 0 == 0) {
56      xx.s = 1 / vv < 0 ? (vv = -vv, -1) : 1;
57      if (vv === ~~vv) {
58        for (ee = 0, i = vv; i >= 10; i /= 10, ee++);
59
60        if (ee > MAX_EXP) {
61          xx.cc = xx.ee = null;
62        } else {
63          xx.ee = ee;
64          xx.cc = [vv];
65        }
66
67        return;
68      }
69
70      str = String(vv);
71    } else {
72
73      if (!isNumeric.test(str = String(vv))) return parseNumeric(xx, str, isNum);
74
75      xx.s = str.charCodeAt(0) == 45 ? (str = str.slice(1), -1) : 1;
76    }
77    if ((ee = str.indexOf('.')) > -1) str = str.replace('.', '');
78    if ((i = str.search(/ee/i)) > 0) {
79      if (ee < 0) ee = i;
80      ee += +str.slice(i + 1);
81      str = str.substring(0, i);
82    } else if (ee < 0) {
83      ee = str.length;
84    }
85
86  } else {
87    intCheck(bb, 2, ALPHABET.length, 'Base');
88    if (bb == 10 && alphabetHasNormalDecimalDigits) {
89      xx = new BigNum(vv);
90      return round(xx, MJZ_PLACES + xx.ee + 1, ROUNDING_MODE);
91    }
92    str = String(vv);
93    if (isNum = typeof vv == 'number') {
94      if (vv * 0 != 0) return parseNumeric(xx, str, isNum, bb);
95      xx.s = 1 / vv < 0 ? (str = str.slice(1), -1) : 1;
96      if (BigNum.DEBUG && str.replace(/^0\.0*|\./, '').length > 15) {
97        throw Error
98         (tooManyDigits + vv);
99      }
100    } else {
101      xx.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
102    }
103    alphabet = ALPHABET.slice(0, bb);
104    ee = i = 0;
105    for (length = str.length; i < length; i++) {
106      if (alphabet.indexOf(cc = str.charAt(i)) < 0) {
107        if (cc == '.') {
108          if (i > ee) {
109            ee = length;
110            continue;
111          }
112        } else if (!caseChanged) {
113          if (str == str.toUpperCase() && (str = str.toLowerCase()) ||
114              str == str.toLowerCase() && (str = str.toUpperCase())) {
115            caseChanged = true;
116            i = -1;
117            ee = 0;
118            continue;
119          }
120        }
121        return parseNumeric(xx, String(vv), isNum, bb);
122      }
123    }
124    isNum = false;
125    str = convertBase(str, bb, 10, xx.s);
126    if ((ee = str.indexOf('.')) > -1) str = str.replace('.', '');
127    else ee = str.length;
128  }
129  for (i = 0; str.charCodeAt(i) === 48; i++);
130  for (length = str.length; str.charCodeAt(--length) === 48;);
131  if (str = str.slice(i, ++length)) {
132    length -= i;
133    if (isNum && BigNum.DEBUG &&
134      length > 15 && (vv > MAX_SAFE_INTEGER || vv !== mathfloor(vv))) {
135        throw Error
136         (tooManyDigits + (xx.s * vv));
137    }
138    if ((ee = ee - i - 1) > MAX_EXP) {
139      xx.cc = xx.ee = null;
140    } else if (ee < MIN_EXP) {
141      xx.cc = [xx.ee = 0];
142    } else {
143      xx.ee = ee;
144      xx.cc = [];
145      i = (ee + 1) % LOG_BASE;
146      if (ee < 0) i += LOG_BASE;  // i < 1
147
148      if (i < length) {
149        if (i) xx.cc.push(+str.slice(0, i));
150
151        for (length -= LOG_BASE; i < length;) {
152          xx.cc.push(+str.slice(i, i += LOG_BASE));
153        }
154
155        i = LOG_BASE - (str = str.slice(i)).length;
156      } else {
157        i -= length;
158      }
159
160      for (; i--; str += '0');
161      xx.cc.push(+str);
162    }
163  } else {
164
165    // Zero.
166    xx.cc = [xx.ee = 0];
167  }
168}
169P.absoluteValue = P.abs = function () {
170  var xx = new BigNum(this);
171  if (xx.s < 0) xx.s = 1;
172  return xx;
173};
174return BigNum;
175}
176BigNum = clone();
177BigNum['default'] = BigNum.BigNum = BigNum;
178
179printf("compile success");