14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_civar BigNum,
174514f5e3Sopenharmony_ciisNumeric = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,
184514f5e3Sopenharmony_cimathceil = Math.ceil,
194514f5e3Sopenharmony_cimathfloor = Math.floor,
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cibignumberError = '[BigNum Error] ',
224514f5e3Sopenharmony_citooManyDigits = bignumberError + 'Number primitive has more than 15 significant digits: ',
234514f5e3Sopenharmony_ciBASE = 1e14,
244514f5e3Sopenharmony_ciLOG_BASE = 14,
254514f5e3Sopenharmony_ciMAX_SAFE_INTEGER = 0x1fffffffffffff,         // 2^53 - 1
264514f5e3Sopenharmony_ciPOWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13],
274514f5e3Sopenharmony_ciSQRT_BASE = 1e7,
284514f5e3Sopenharmony_ciMAX = 1E9;                                   // 0 to MAX_INT32
294514f5e3Sopenharmony_cifunction clone(configObject) {
304514f5e3Sopenharmony_civar convertBase, parseNumeric,
314514f5e3Sopenharmony_ci  P = BigNum.prototype = { constructor: BigNum, toString: null, valueOf: null },
324514f5e3Sopenharmony_ci  MJZ_PLACES = 20,                         // 0 to MAX
334514f5e3Sopenharmony_ci  ROUNDING_MODE = 4,                       // 0 to 8
344514f5e3Sopenharmony_ci  MIN_EXP = -1e7,                          // -1 to -MAX
354514f5e3Sopenharmony_ci  MAX_EXP = 1e7,                           // 1 to MAX
364514f5e3Sopenharmony_ci  ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz',
374514f5e3Sopenharmony_ci  alphabetHasNormalDecimalDigits = true;
384514f5e3Sopenharmony_cifunction BigNum(vv, bb) {
394514f5e3Sopenharmony_ci  var alphabet, cc, caseChanged, ee, i, isNum, length, str,
404514f5e3Sopenharmony_ci    xx = this;
414514f5e3Sopenharmony_ci  if (!(xx instanceof BigNum)) return new BigNum(vv, bb);
424514f5e3Sopenharmony_ci  if (bb == null) {
434514f5e3Sopenharmony_ci    if (vv && vv._isBigNumber === true) {
444514f5e3Sopenharmony_ci      xx.s = vv.s;
454514f5e3Sopenharmony_ci      if (!vv.cc || vv.ee > MAX_EXP) {
464514f5e3Sopenharmony_ci        xx.cc = xx.ee = null;
474514f5e3Sopenharmony_ci      } else if (vv.ee < MIN_EXP) {
484514f5e3Sopenharmony_ci        xx.cc = [xx.ee = 0];
494514f5e3Sopenharmony_ci      } else {
504514f5e3Sopenharmony_ci        xx.ee = vv.ee;
514514f5e3Sopenharmony_ci        xx.cc = vv.cc.slice();
524514f5e3Sopenharmony_ci      }
534514f5e3Sopenharmony_ci      return;
544514f5e3Sopenharmony_ci    }
554514f5e3Sopenharmony_ci    if ((isNum = typeof vv == 'number') && vv * 0 == 0) {
564514f5e3Sopenharmony_ci      xx.s = 1 / vv < 0 ? (vv = -vv, -1) : 1;
574514f5e3Sopenharmony_ci      if (vv === ~~vv) {
584514f5e3Sopenharmony_ci        for (ee = 0, i = vv; i >= 10; i /= 10, ee++);
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci        if (ee > MAX_EXP) {
614514f5e3Sopenharmony_ci          xx.cc = xx.ee = null;
624514f5e3Sopenharmony_ci        } else {
634514f5e3Sopenharmony_ci          xx.ee = ee;
644514f5e3Sopenharmony_ci          xx.cc = [vv];
654514f5e3Sopenharmony_ci        }
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci        return;
684514f5e3Sopenharmony_ci      }
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci      str = String(vv);
714514f5e3Sopenharmony_ci    } else {
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_ci      if (!isNumeric.test(str = String(vv))) return parseNumeric(xx, str, isNum);
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci      xx.s = str.charCodeAt(0) == 45 ? (str = str.slice(1), -1) : 1;
764514f5e3Sopenharmony_ci    }
774514f5e3Sopenharmony_ci    if ((ee = str.indexOf('.')) > -1) str = str.replace('.', '');
784514f5e3Sopenharmony_ci    if ((i = str.search(/ee/i)) > 0) {
794514f5e3Sopenharmony_ci      if (ee < 0) ee = i;
804514f5e3Sopenharmony_ci      ee += +str.slice(i + 1);
814514f5e3Sopenharmony_ci      str = str.substring(0, i);
824514f5e3Sopenharmony_ci    } else if (ee < 0) {
834514f5e3Sopenharmony_ci      ee = str.length;
844514f5e3Sopenharmony_ci    }
854514f5e3Sopenharmony_ci
864514f5e3Sopenharmony_ci  } else {
874514f5e3Sopenharmony_ci    intCheck(bb, 2, ALPHABET.length, 'Base');
884514f5e3Sopenharmony_ci    if (bb == 10 && alphabetHasNormalDecimalDigits) {
894514f5e3Sopenharmony_ci      xx = new BigNum(vv);
904514f5e3Sopenharmony_ci      return round(xx, MJZ_PLACES + xx.ee + 1, ROUNDING_MODE);
914514f5e3Sopenharmony_ci    }
924514f5e3Sopenharmony_ci    str = String(vv);
934514f5e3Sopenharmony_ci    if (isNum = typeof vv == 'number') {
944514f5e3Sopenharmony_ci      if (vv * 0 != 0) return parseNumeric(xx, str, isNum, bb);
954514f5e3Sopenharmony_ci      xx.s = 1 / vv < 0 ? (str = str.slice(1), -1) : 1;
964514f5e3Sopenharmony_ci      if (BigNum.DEBUG && str.replace(/^0\.0*|\./, '').length > 15) {
974514f5e3Sopenharmony_ci        throw Error
984514f5e3Sopenharmony_ci         (tooManyDigits + vv);
994514f5e3Sopenharmony_ci      }
1004514f5e3Sopenharmony_ci    } else {
1014514f5e3Sopenharmony_ci      xx.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
1024514f5e3Sopenharmony_ci    }
1034514f5e3Sopenharmony_ci    alphabet = ALPHABET.slice(0, bb);
1044514f5e3Sopenharmony_ci    ee = i = 0;
1054514f5e3Sopenharmony_ci    for (length = str.length; i < length; i++) {
1064514f5e3Sopenharmony_ci      if (alphabet.indexOf(cc = str.charAt(i)) < 0) {
1074514f5e3Sopenharmony_ci        if (cc == '.') {
1084514f5e3Sopenharmony_ci          if (i > ee) {
1094514f5e3Sopenharmony_ci            ee = length;
1104514f5e3Sopenharmony_ci            continue;
1114514f5e3Sopenharmony_ci          }
1124514f5e3Sopenharmony_ci        } else if (!caseChanged) {
1134514f5e3Sopenharmony_ci          if (str == str.toUpperCase() && (str = str.toLowerCase()) ||
1144514f5e3Sopenharmony_ci              str == str.toLowerCase() && (str = str.toUpperCase())) {
1154514f5e3Sopenharmony_ci            caseChanged = true;
1164514f5e3Sopenharmony_ci            i = -1;
1174514f5e3Sopenharmony_ci            ee = 0;
1184514f5e3Sopenharmony_ci            continue;
1194514f5e3Sopenharmony_ci          }
1204514f5e3Sopenharmony_ci        }
1214514f5e3Sopenharmony_ci        return parseNumeric(xx, String(vv), isNum, bb);
1224514f5e3Sopenharmony_ci      }
1234514f5e3Sopenharmony_ci    }
1244514f5e3Sopenharmony_ci    isNum = false;
1254514f5e3Sopenharmony_ci    str = convertBase(str, bb, 10, xx.s);
1264514f5e3Sopenharmony_ci    if ((ee = str.indexOf('.')) > -1) str = str.replace('.', '');
1274514f5e3Sopenharmony_ci    else ee = str.length;
1284514f5e3Sopenharmony_ci  }
1294514f5e3Sopenharmony_ci  for (i = 0; str.charCodeAt(i) === 48; i++);
1304514f5e3Sopenharmony_ci  for (length = str.length; str.charCodeAt(--length) === 48;);
1314514f5e3Sopenharmony_ci  if (str = str.slice(i, ++length)) {
1324514f5e3Sopenharmony_ci    length -= i;
1334514f5e3Sopenharmony_ci    if (isNum && BigNum.DEBUG &&
1344514f5e3Sopenharmony_ci      length > 15 && (vv > MAX_SAFE_INTEGER || vv !== mathfloor(vv))) {
1354514f5e3Sopenharmony_ci        throw Error
1364514f5e3Sopenharmony_ci         (tooManyDigits + (xx.s * vv));
1374514f5e3Sopenharmony_ci    }
1384514f5e3Sopenharmony_ci    if ((ee = ee - i - 1) > MAX_EXP) {
1394514f5e3Sopenharmony_ci      xx.cc = xx.ee = null;
1404514f5e3Sopenharmony_ci    } else if (ee < MIN_EXP) {
1414514f5e3Sopenharmony_ci      xx.cc = [xx.ee = 0];
1424514f5e3Sopenharmony_ci    } else {
1434514f5e3Sopenharmony_ci      xx.ee = ee;
1444514f5e3Sopenharmony_ci      xx.cc = [];
1454514f5e3Sopenharmony_ci      i = (ee + 1) % LOG_BASE;
1464514f5e3Sopenharmony_ci      if (ee < 0) i += LOG_BASE;  // i < 1
1474514f5e3Sopenharmony_ci
1484514f5e3Sopenharmony_ci      if (i < length) {
1494514f5e3Sopenharmony_ci        if (i) xx.cc.push(+str.slice(0, i));
1504514f5e3Sopenharmony_ci
1514514f5e3Sopenharmony_ci        for (length -= LOG_BASE; i < length;) {
1524514f5e3Sopenharmony_ci          xx.cc.push(+str.slice(i, i += LOG_BASE));
1534514f5e3Sopenharmony_ci        }
1544514f5e3Sopenharmony_ci
1554514f5e3Sopenharmony_ci        i = LOG_BASE - (str = str.slice(i)).length;
1564514f5e3Sopenharmony_ci      } else {
1574514f5e3Sopenharmony_ci        i -= length;
1584514f5e3Sopenharmony_ci      }
1594514f5e3Sopenharmony_ci
1604514f5e3Sopenharmony_ci      for (; i--; str += '0');
1614514f5e3Sopenharmony_ci      xx.cc.push(+str);
1624514f5e3Sopenharmony_ci    }
1634514f5e3Sopenharmony_ci  } else {
1644514f5e3Sopenharmony_ci
1654514f5e3Sopenharmony_ci    // Zero.
1664514f5e3Sopenharmony_ci    xx.cc = [xx.ee = 0];
1674514f5e3Sopenharmony_ci  }
1684514f5e3Sopenharmony_ci}
1694514f5e3Sopenharmony_ciP.absoluteValue = P.abs = function () {
1704514f5e3Sopenharmony_ci  var xx = new BigNum(this);
1714514f5e3Sopenharmony_ci  if (xx.s < 0) xx.s = 1;
1724514f5e3Sopenharmony_ci  return xx;
1734514f5e3Sopenharmony_ci};
1744514f5e3Sopenharmony_cireturn BigNum;
1754514f5e3Sopenharmony_ci}
1764514f5e3Sopenharmony_ciBigNum = clone();
1774514f5e3Sopenharmony_ciBigNum['default'] = BigNum.BigNum = BigNum;
1784514f5e3Sopenharmony_ci
1794514f5e3Sopenharmony_ciprintf("compile success");