diff --git a/break_eternity.cjs.js b/break_eternity.cjs.js index c5ebca4..5c08922 100644 --- a/break_eternity.cjs.js +++ b/break_eternity.cjs.js @@ -1,5 +1,4 @@ 'use strict'; - function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); diff --git a/break_eternity.esm.js b/break_eternity.esm.js index 22bb1b5..85fd9a2 100644 --- a/break_eternity.esm.js +++ b/break_eternity.esm.js @@ -1,3 +1,4 @@ +'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); @@ -3254,5 +3255,4 @@ FC_NN = Decimal.fromComponents_noNormalize; // eslint-disable-next-line @typescr Decimal.fromMantissaExponent; // eslint-disable-next-line @typescript-eslint/no-unused-vars Decimal.fromMantissaExponent_noNormalize; - export { Decimal as default }; diff --git a/break_eternity.js b/break_eternity.js index e15caff..a52681f 100644 --- a/break_eternity.js +++ b/break_eternity.js @@ -1,874 +1,816 @@ -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Decimal = factory()); -})(this, (function () { 'use strict'; +'use strict'; - function _classCallCheck(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } +function _classCallCheck(instance, Constructor) { + if (!(instance instanceof Constructor)) { + throw new TypeError("Cannot call a class as a function"); } - - function _defineProperties(target, props) { - for (var i = 0; i < props.length; i++) { - var descriptor = props[i]; - descriptor.enumerable = descriptor.enumerable || false; - descriptor.configurable = true; - if ("value" in descriptor) descriptor.writable = true; - Object.defineProperty(target, descriptor.key, descriptor); - } +} + +function _defineProperties(target, props) { + for (var i = 0; i < props.length; i++) { + var descriptor = props[i]; + descriptor.enumerable = descriptor.enumerable || false; + descriptor.configurable = true; + if ("value" in descriptor) descriptor.writable = true; + Object.defineProperty(target, descriptor.key, descriptor); } +} - function _createClass(Constructor, protoProps, staticProps) { - if (protoProps) _defineProperties(Constructor.prototype, protoProps); - if (staticProps) _defineProperties(Constructor, staticProps); - Object.defineProperty(Constructor, "prototype", { - writable: false - }); - return Constructor; - } +function _createClass(Constructor, protoProps, staticProps) { + if (protoProps) _defineProperties(Constructor.prototype, protoProps); + if (staticProps) _defineProperties(Constructor, staticProps); + Object.defineProperty(Constructor, "prototype", { + writable: false + }); + return Constructor; +} +/** + * A LRU cache intended for caching pure functions. + */ +var LRUCache = /*#__PURE__*/function () { /** - * A LRU cache intended for caching pure functions. + * @param maxSize The maximum size for this cache. We recommend setting this + * to be one less than a power of 2, as most hashtables - including V8's + * Object hashtable (https://crsrc.org/c/v8/src/objects/ordered-hash-table.cc) + * - uses powers of two for hashtable sizes. It can't exactly be a power of + * two, as a .set() call could temporarily set the size of the map to be + * maxSize + 1. */ - var LRUCache = /*#__PURE__*/function () { + function LRUCache(maxSize) { + _classCallCheck(this, LRUCache); + + this.map = new Map(); // Invariant: Exactly one of the below is true before and after calling a + // LRUCache method: + // - first and last are both undefined, and map.size() is 0. + // - first and last are the same object, and map.size() is 1. + // - first and last are different objects, and map.size() is greater than 1. + + this.first = undefined; + this.last = undefined; + this.maxSize = maxSize; + } + + _createClass(LRUCache, [{ + key: "size", + get: function get() { + return this.map.size; + } /** - * @param maxSize The maximum size for this cache. We recommend setting this - * to be one less than a power of 2, as most hashtables - including V8's - * Object hashtable (https://crsrc.org/c/v8/src/objects/ordered-hash-table.cc) - * - uses powers of two for hashtable sizes. It can't exactly be a power of - * two, as a .set() call could temporarily set the size of the map to be - * maxSize + 1. + * Gets the specified key from the cache, or undefined if it is not in the + * cache. + * @param key The key to get. + * @returns The cached value, or undefined if key is not in the cache. */ - function LRUCache(maxSize) { - _classCallCheck(this, LRUCache); - - this.map = new Map(); // Invariant: Exactly one of the below is true before and after calling a - // LRUCache method: - // - first and last are both undefined, and map.size() is 0. - // - first and last are the same object, and map.size() is 1. - // - first and last are different objects, and map.size() is greater than 1. - - this.first = undefined; - this.last = undefined; - this.maxSize = maxSize; - } - - _createClass(LRUCache, [{ - key: "size", - get: function get() { - return this.map.size; - } - /** - * Gets the specified key from the cache, or undefined if it is not in the - * cache. - * @param key The key to get. - * @returns The cached value, or undefined if key is not in the cache. - */ - - }, { - key: "get", - value: function get(key) { - var node = this.map.get(key); - - if (node === undefined) { - return undefined; - } // It is guaranteed that there is at least one item in the cache. - // Therefore, first and last are guaranteed to be a ListNode... - // but if there is only one item, they might be the same. - // Update the order of the list to make this node the first node in the - // list. - // This isn't needed if this node is already the first node in the list. - - - if (node !== this.first) { - // As this node is DIFFERENT from the first node, it is guaranteed that - // there are at least two items in the cache. - // However, this node could possibly be the last item. - if (node === this.last) { - // This node IS the last node. - this.last = node.prev; // From the invariants, there must be at least two items in the cache, - // so node - which is the original "last node" - must have a defined - // previous node. Therefore, this.last - set above - must be defined - // here. - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - - this.last.next = undefined; - } else { - // This node is somewhere in the middle of the list, so there must be at - // least THREE items in the list, and this node's prev and next must be - // defined here. - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - node.prev.next = node.next; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - node.next.prev = node.prev; - } + }, { + key: "get", + value: function get(key) { + var node = this.map.get(key); + + if (node === undefined) { + return undefined; + } // It is guaranteed that there is at least one item in the cache. + // Therefore, first and last are guaranteed to be a ListNode... + // but if there is only one item, they might be the same. + // Update the order of the list to make this node the first node in the + // list. + // This isn't needed if this node is already the first node in the list. + + + if (node !== this.first) { + // As this node is DIFFERENT from the first node, it is guaranteed that + // there are at least two items in the cache. + // However, this node could possibly be the last item. + if (node === this.last) { + // This node IS the last node. + this.last = node.prev; // From the invariants, there must be at least two items in the cache, + // so node - which is the original "last node" - must have a defined + // previous node. Therefore, this.last - set above - must be defined + // here. + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - node.next = this.first; // From the invariants, there must be at least two items in the cache, so - // this.first must be a valid ListNode. + this.last.next = undefined; + } else { + // This node is somewhere in the middle of the list, so there must be at + // least THREE items in the list, and this node's prev and next must be + // defined here. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + node.prev.next = node.next; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.first.prev = node; - this.first = node; + node.next.prev = node.prev; } - return node.value; - } - /** - * Sets an entry in the cache. - * - * @param key The key of the entry. - * @param value The value of the entry. - * @throws Error, if the map already contains the key. - */ - - }, { - key: "set", - value: function set(key, value) { - // Ensure that this.maxSize >= 1. - if (this.maxSize < 1) { - return; - } + node.next = this.first; // From the invariants, there must be at least two items in the cache, so + // this.first must be a valid ListNode. + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - if (this.map.has(key)) { - throw new Error("Cannot update existing keys in the cache"); - } + this.first.prev = node; + this.first = node; + } - var node = new ListNode(key, value); // Move node to the front of the list. + return node.value; + } + /** + * Sets an entry in the cache. + * + * @param key The key of the entry. + * @param value The value of the entry. + * @throws Error, if the map already contains the key. + */ - if (this.first === undefined) { - // If the first is undefined, the last is undefined too. - // Therefore, this cache has no items in it. - this.first = node; - this.last = node; - } else { - // This cache has at least one item in it. - node.next = this.first; - this.first.prev = node; - this.first = node; - } + }, { + key: "set", + value: function set(key, value) { + // Ensure that this.maxSize >= 1. + if (this.maxSize < 1) { + return; + } - this.map.set(key, node); + if (this.map.has(key)) { + throw new Error("Cannot update existing keys in the cache"); + } - while (this.map.size > this.maxSize) { - // We are guaranteed that this.maxSize >= 1, - // so this.map.size is guaranteed to be >= 2, - // so this.first and this.last must be different valid ListNodes, - // and this.last.prev must also be a valid ListNode (possibly this.first). - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - var last = this.last; - this.map["delete"](last.key); - this.last = last.prev; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + var node = new ListNode(key, value); // Move node to the front of the list. - this.last.next = undefined; - } + if (this.first === undefined) { + // If the first is undefined, the last is undefined too. + // Therefore, this cache has no items in it. + this.first = node; + this.last = node; + } else { + // This cache has at least one item in it. + node.next = this.first; + this.first.prev = node; + this.first = node; } - }]); - return LRUCache; - }(); - /** - * A node in a doubly linked list. - */ + this.map.set(key, node); - var ListNode = /*#__PURE__*/_createClass(function ListNode(key, value) { - _classCallCheck(this, ListNode); + while (this.map.size > this.maxSize) { + // We are guaranteed that this.maxSize >= 1, + // so this.map.size is guaranteed to be >= 2, + // so this.first and this.last must be different valid ListNodes, + // and this.last.prev must also be a valid ListNode (possibly this.first). + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + var last = this.last; + this.map["delete"](last.key); + this.last = last.prev; // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.next = undefined; - this.prev = undefined; - this.key = key; - this.value = value; - }); + this.last.next = undefined; + } + } + }]); - var MAX_SIGNIFICANT_DIGITS = 17; //Maximum number of digits of precision to assume in Number - - var EXP_LIMIT = 9e15; //If we're ABOVE this value, increase a layer. (9e15 is close to the largest integer that can fit in a Number.) - - var LAYER_DOWN = Math.log10(9e15); - var FIRST_NEG_LAYER = 1 / 9e15; //At layer 0, smaller non-zero numbers than this become layer 1 numbers with negative mag. After that the pattern continues as normal. - - var NUMBER_EXP_MAX = 308; //The largest exponent that can appear in a Number, though not all mantissas are valid here. - - var NUMBER_EXP_MIN = -324; //The smallest exponent that can appear in a Number, though not all mantissas are valid here. - - var MAX_ES_IN_A_ROW = 5; //For default toString behaviour, when to swap from eee... to (e^n) syntax. - - var DEFAULT_FROM_STRING_CACHE_SIZE = (1 << 10) - 1; // The default size of the LRU cache used to cache Decimal.fromString. - - var powerOf10 = function () { - // We need this lookup table because Math.pow(10, exponent) - // when exponent's absolute value is large is slightly inaccurate. - // You can fix it with the power of math... or just make a lookup table. - // Faster AND simpler - var powersOf10 = []; - - for (var i = NUMBER_EXP_MIN + 1; i <= NUMBER_EXP_MAX; i++) { - powersOf10.push(Number("1e" + i)); - } - - var indexOf0InPowersOf10 = 323; - return function (power) { - return powersOf10[power + indexOf0InPowersOf10]; - }; - }(); //tetration/slog to real height stuff - //background info and tables of values for critical functions taken here: https://github.com/Patashu/break_eternity.js/issues/22 - - - var critical_headers = [2, Math.E, 3, 4, 5, 6, 7, 8, 9, 10]; - var critical_tetr_values = [[// Base 2 (using http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html ) - 1, 1.0891180521811202527, 1.1789767925673958433, 1.2701455431742086633, 1.3632090180450091941, 1.4587818160364217007, 1.5575237916251418333, 1.6601571006859253673, 1.7674858188369780435, 1.8804192098842727359, 2], [// Base E (using http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html ) - 1, 1.1121114330934078681, 1.2310389249316089299, 1.3583836963111376089, 1.4960519303993531879, 1.6463542337511945810, 1.8121385357018724464, 1.9969713246183068478, 2.2053895545527544330, 2.4432574483385252544, Math.E //1.0 - ], [// Base 3 - 1, 1.1187738849693603, 1.2464963939368214, 1.38527004705667, 1.5376664685821402, 1.7068895236551784, 1.897001227148399, 2.1132403089001035, 2.362480153784171, 2.6539010333870774, 3], [// Base 4 - 1, 1.1367350847096405, 1.2889510672956703, 1.4606478703324786, 1.6570295196661111, 1.8850062585672889, 2.1539465047453485, 2.476829779693097, 2.872061932789197, 3.3664204535587183, 4], [// Base 5 - 1, 1.1494592900767588, 1.319708228183931, 1.5166291280087583, 1.748171114438024, 2.0253263297298045, 2.3636668498288547, 2.7858359149579424, 3.3257226212448145, 4.035730287722532, 5], [// Base 6 - 1, 1.159225940787673, 1.343712473580932, 1.5611293155111927, 1.8221199554561318, 2.14183924486326, 2.542468319282638, 3.0574682501653316, 3.7390572020926873, 4.6719550537360774, 6], [// Base 7 - 1, 1.1670905356972596, 1.3632807444991446, 1.5979222279405536, 1.8842640123816674, 2.2416069644878687, 2.69893426559423, 3.3012632110403577, 4.121250340630164, 5.281493033448316, 7], [// Base 8 - 1, 1.1736630594087796, 1.379783782386201, 1.6292821855668218, 1.9378971836180754, 2.3289975651071977, 2.8384347394720835, 3.5232708454565906, 4.478242031114584, 5.868592169644505, 8], [// Base 9 - 1, 1.1793017514670474, 1.394054150657457, 1.65664127441059, 1.985170999970283, 2.4069682290577457, 2.9647310119960752, 3.7278665320924946, 4.814462547283592, 6.436522247411611, 9], [// Base 10 (using http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html ) - 1, 1.1840100246247336579, 1.4061375836156954169, 1.6802272208863963918, 2.026757028388618927, 2.4770056063449647580, 3.0805252717554819987, 3.9191964192627283911, 5.1351528408331864230, 6.9899611795347148455, 10]]; - var critical_slog_values = [[// Base 2 - -1, -0.9194161097107025, -0.8335625019330468, -0.7425599821143978, -0.6466611521029437, -0.5462617907227869, -0.4419033816638769, -0.3342645487554494, -0.224140440909962, -0.11241087890006762, 0], [// Base E - -1, -0.90603157029014, -0.80786507256596, -0.7064666939634, -0.60294836853664, -0.49849837513117, -0.39430303318768, -0.29147201034755, -0.19097820800866, -0.09361896280296, 0 //1.0 - ], [// Base 3 - -1, -0.9021579584316141, -0.8005762598234203, -0.6964780623319391, -0.5911906810998454, -0.486050182576545, -0.3823089430815083, -0.28106046722897615, -0.1831906535795894, -0.08935809204418144, 0], [// Base 4 - -1, -0.8917227442365535, -0.781258746326964, -0.6705130326902455, -0.5612813129406509, -0.4551067709033134, -0.35319256652135966, -0.2563741554088552, -0.1651412821106526, -0.0796919581982668, 0], [// Base 5 - -1, -0.8843387974366064, -0.7678744063886243, -0.6529563724510552, -0.5415870994657841, -0.4352842206588936, -0.33504449124791424, -0.24138853420685147, -0.15445285440944467, -0.07409659641336663, 0], [// Base 6 - -1, -0.8786709358426346, -0.7577735191184886, -0.6399546189952064, -0.527284921869926, -0.4211627631006314, -0.3223479611761232, -0.23107655627789858, -0.1472057700818259, -0.07035171210706326, 0], [// Base 7 - -1, -0.8740862815291583, -0.7497032990976209, -0.6297119746181752, -0.5161838335958787, -0.41036238255751956, -0.31277212146489963, -0.2233976621705518, -0.1418697367979619, -0.06762117662323441, 0], [// Base 8 - -1, -0.8702632331800649, -0.7430366914122081, -0.6213373075161548, -0.5072025698095242, -0.40171437727184167, -0.30517930701410456, -0.21736343968190863, -0.137710238299109, -0.06550774483471955, 0], [// Base 9 - -1, -0.8670016295947213, -0.7373984232432306, -0.6143173985094293, -0.49973884395492807, -0.394584953527678, -0.2989649949848695, -0.21245647317021688, -0.13434688362382652, -0.0638072667348083, 0], [// Base 10 - -1, -0.8641642839543857, -0.732534623168535, -0.6083127477059322, -0.4934049257184696, -0.3885773075899922, -0.29376029055315767, -0.2083678561173622, -0.13155653399373268, -0.062401588652553186, 0]]; - - var D = function D(value) { - return Decimal.fromValue_noAlloc(value); - }; + return LRUCache; +}(); +/** + * A node in a doubly linked list. + */ - var FC = function FC(sign, layer, mag) { - return Decimal.fromComponents(sign, layer, mag); - }; +var ListNode = /*#__PURE__*/_createClass(function ListNode(key, value) { + _classCallCheck(this, ListNode); - var FC_NN = function FC_NN(sign, layer, mag) { - return Decimal.fromComponents_noNormalize(sign, layer, mag); - }; // eslint-disable-next-line @typescript-eslint/no-unused-vars + this.next = undefined; + this.prev = undefined; + this.key = key; + this.value = value; +}); - var decimalPlaces = function decimalPlaces(value, places) { - var len = places + 1; - var numDigits = Math.ceil(Math.log10(Math.abs(value))); - var rounded = Math.round(value * Math.pow(10, len - numDigits)) * Math.pow(10, numDigits - len); - return parseFloat(rounded.toFixed(Math.max(len - numDigits, 0))); - }; +var MAX_SIGNIFICANT_DIGITS = 17; //Maximum number of digits of precision to assume in Number - var f_maglog10 = function f_maglog10(n) { - return Math.sign(n) * Math.log10(Math.abs(n)); - }; //from HyperCalc source code +var EXP_LIMIT = 9e15; //If we're ABOVE this value, increase a layer. (9e15 is close to the largest integer that can fit in a Number.) +var LAYER_DOWN = Math.log10(9e15); +var FIRST_NEG_LAYER = 1 / 9e15; //At layer 0, smaller non-zero numbers than this become layer 1 numbers with negative mag. After that the pattern continues as normal. - var f_gamma = function f_gamma(n) { - if (!isFinite(n)) { - return n; - } +var NUMBER_EXP_MAX = 308; //The largest exponent that can appear in a Number, though not all mantissas are valid here. - if (n < -50) { - if (n === Math.trunc(n)) { - return Number.NEGATIVE_INFINITY; - } +var NUMBER_EXP_MIN = -324; //The smallest exponent that can appear in a Number, though not all mantissas are valid here. - return 0; - } +var MAX_ES_IN_A_ROW = 5; //For default toString behaviour, when to swap from eee... to (e^n) syntax. - var scal1 = 1; - - while (n < 10) { - scal1 = scal1 * n; - ++n; - } - - n -= 1; - var l = 0.9189385332046727; //0.5*Math.log(2*Math.PI) - - l = l + (n + 0.5) * Math.log(n); - l = l - n; - var n2 = n * n; - var np = n; - l = l + 1 / (12 * np); - np = np * n2; - l = l + 1 / (360 * np); - np = np * n2; - l = l + 1 / (1260 * np); - np = np * n2; - l = l + 1 / (1680 * np); - np = np * n2; - l = l + 1 / (1188 * np); - np = np * n2; - l = l + 691 / (360360 * np); - np = np * n2; - l = l + 7 / (1092 * np); - np = np * n2; - l = l + 3617 / (122400 * np); - return Math.exp(l) / scal1; - }; +var DEFAULT_FROM_STRING_CACHE_SIZE = (1 << 10) - 1; // The default size of the LRU cache used to cache Decimal.fromString. - var OMEGA = 0.56714329040978387299997; // W(1, 0) - //from https://math.stackexchange.com/a/465183 - // The evaluation can become inaccurate very close to the branch point +var powerOf10 = function () { + // We need this lookup table because Math.pow(10, exponent) + // when exponent's absolute value is large is slightly inaccurate. + // You can fix it with the power of math... or just make a lookup table. + // Faster AND simpler + var powersOf10 = []; - var f_lambertw = function f_lambertw(z) { - var tol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1e-10; - var w; - var wn; + for (var i = NUMBER_EXP_MIN + 1; i <= NUMBER_EXP_MAX; i++) { + powersOf10.push(Number("1e" + i)); + } - if (!Number.isFinite(z)) { - return z; - } + var indexOf0InPowersOf10 = 323; + return function (power) { + return powersOf10[power + indexOf0InPowersOf10]; + }; +}(); //tetration/slog to real height stuff +//background info and tables of values for critical functions taken here: https://github.com/Patashu/break_eternity.js/issues/22 + + +var critical_headers = [2, Math.E, 3, 4, 5, 6, 7, 8, 9, 10]; +var critical_tetr_values = [[// Base 2 (using http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html ) +1, 1.0891180521811202527, 1.1789767925673958433, 1.2701455431742086633, 1.3632090180450091941, 1.4587818160364217007, 1.5575237916251418333, 1.6601571006859253673, 1.7674858188369780435, 1.8804192098842727359, 2], [// Base E (using http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html ) +1, 1.1121114330934078681, 1.2310389249316089299, 1.3583836963111376089, 1.4960519303993531879, 1.6463542337511945810, 1.8121385357018724464, 1.9969713246183068478, 2.2053895545527544330, 2.4432574483385252544, Math.E //1.0 +], [// Base 3 +1, 1.1187738849693603, 1.2464963939368214, 1.38527004705667, 1.5376664685821402, 1.7068895236551784, 1.897001227148399, 2.1132403089001035, 2.362480153784171, 2.6539010333870774, 3], [// Base 4 +1, 1.1367350847096405, 1.2889510672956703, 1.4606478703324786, 1.6570295196661111, 1.8850062585672889, 2.1539465047453485, 2.476829779693097, 2.872061932789197, 3.3664204535587183, 4], [// Base 5 +1, 1.1494592900767588, 1.319708228183931, 1.5166291280087583, 1.748171114438024, 2.0253263297298045, 2.3636668498288547, 2.7858359149579424, 3.3257226212448145, 4.035730287722532, 5], [// Base 6 +1, 1.159225940787673, 1.343712473580932, 1.5611293155111927, 1.8221199554561318, 2.14183924486326, 2.542468319282638, 3.0574682501653316, 3.7390572020926873, 4.6719550537360774, 6], [// Base 7 +1, 1.1670905356972596, 1.3632807444991446, 1.5979222279405536, 1.8842640123816674, 2.2416069644878687, 2.69893426559423, 3.3012632110403577, 4.121250340630164, 5.281493033448316, 7], [// Base 8 +1, 1.1736630594087796, 1.379783782386201, 1.6292821855668218, 1.9378971836180754, 2.3289975651071977, 2.8384347394720835, 3.5232708454565906, 4.478242031114584, 5.868592169644505, 8], [// Base 9 +1, 1.1793017514670474, 1.394054150657457, 1.65664127441059, 1.985170999970283, 2.4069682290577457, 2.9647310119960752, 3.7278665320924946, 4.814462547283592, 6.436522247411611, 9], [// Base 10 (using http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html ) +1, 1.1840100246247336579, 1.4061375836156954169, 1.6802272208863963918, 2.026757028388618927, 2.4770056063449647580, 3.0805252717554819987, 3.9191964192627283911, 5.1351528408331864230, 6.9899611795347148455, 10]]; +var critical_slog_values = [[// Base 2 +-1, -0.9194161097107025, -0.8335625019330468, -0.7425599821143978, -0.6466611521029437, -0.5462617907227869, -0.4419033816638769, -0.3342645487554494, -0.224140440909962, -0.11241087890006762, 0], [// Base E +-1, -0.90603157029014, -0.80786507256596, -0.7064666939634, -0.60294836853664, -0.49849837513117, -0.39430303318768, -0.29147201034755, -0.19097820800866, -0.09361896280296, 0 //1.0 +], [// Base 3 +-1, -0.9021579584316141, -0.8005762598234203, -0.6964780623319391, -0.5911906810998454, -0.486050182576545, -0.3823089430815083, -0.28106046722897615, -0.1831906535795894, -0.08935809204418144, 0], [// Base 4 +-1, -0.8917227442365535, -0.781258746326964, -0.6705130326902455, -0.5612813129406509, -0.4551067709033134, -0.35319256652135966, -0.2563741554088552, -0.1651412821106526, -0.0796919581982668, 0], [// Base 5 +-1, -0.8843387974366064, -0.7678744063886243, -0.6529563724510552, -0.5415870994657841, -0.4352842206588936, -0.33504449124791424, -0.24138853420685147, -0.15445285440944467, -0.07409659641336663, 0], [// Base 6 +-1, -0.8786709358426346, -0.7577735191184886, -0.6399546189952064, -0.527284921869926, -0.4211627631006314, -0.3223479611761232, -0.23107655627789858, -0.1472057700818259, -0.07035171210706326, 0], [// Base 7 +-1, -0.8740862815291583, -0.7497032990976209, -0.6297119746181752, -0.5161838335958787, -0.41036238255751956, -0.31277212146489963, -0.2233976621705518, -0.1418697367979619, -0.06762117662323441, 0], [// Base 8 +-1, -0.8702632331800649, -0.7430366914122081, -0.6213373075161548, -0.5072025698095242, -0.40171437727184167, -0.30517930701410456, -0.21736343968190863, -0.137710238299109, -0.06550774483471955, 0], [// Base 9 +-1, -0.8670016295947213, -0.7373984232432306, -0.6143173985094293, -0.49973884395492807, -0.394584953527678, -0.2989649949848695, -0.21245647317021688, -0.13434688362382652, -0.0638072667348083, 0], [// Base 10 +-1, -0.8641642839543857, -0.732534623168535, -0.6083127477059322, -0.4934049257184696, -0.3885773075899922, -0.29376029055315767, -0.2083678561173622, -0.13155653399373268, -0.062401588652553186, 0]]; + +var D = function D(value) { + return Decimal.fromValue_noAlloc(value); +}; + +var FC = function FC(sign, layer, mag) { + return Decimal.fromComponents(sign, layer, mag); +}; + +var FC_NN = function FC_NN(sign, layer, mag) { + return Decimal.fromComponents_noNormalize(sign, layer, mag); +}; // eslint-disable-next-line @typescript-eslint/no-unused-vars + +var decimalPlaces = function decimalPlaces(value, places) { + var len = places + 1; + var numDigits = Math.ceil(Math.log10(Math.abs(value))); + var rounded = Math.round(value * Math.pow(10, len - numDigits)) * Math.pow(10, numDigits - len); + return parseFloat(rounded.toFixed(Math.max(len - numDigits, 0))); +}; + +var f_maglog10 = function f_maglog10(n) { + return Math.sign(n) * Math.log10(Math.abs(n)); +}; //from HyperCalc source code + + +var f_gamma = function f_gamma(n) { + if (!isFinite(n)) { + return n; + } - if (z === 0) { - return z; + if (n < -50) { + if (n === Math.trunc(n)) { + return Number.NEGATIVE_INFINITY; } - if (z === 1) { - return OMEGA; - } + return 0; + } - if (z < 10) { - w = 0; - } else { - w = Math.log(z) - Math.log(Math.log(z)); - } + var scal1 = 1; - for (var i = 0; i < 100; ++i) { - wn = (z * Math.exp(-w) + w * w) / (w + 1); + while (n < 10) { + scal1 = scal1 * n; + ++n; + } - if (Math.abs(wn - w) < tol * Math.abs(wn)) { - return wn; - } else { - w = wn; - } - } + n -= 1; + var l = 0.9189385332046727; //0.5*Math.log(2*Math.PI) + + l = l + (n + 0.5) * Math.log(n); + l = l - n; + var n2 = n * n; + var np = n; + l = l + 1 / (12 * np); + np = np * n2; + l = l + 1 / (360 * np); + np = np * n2; + l = l + 1 / (1260 * np); + np = np * n2; + l = l + 1 / (1680 * np); + np = np * n2; + l = l + 1 / (1188 * np); + np = np * n2; + l = l + 691 / (360360 * np); + np = np * n2; + l = l + 7 / (1092 * np); + np = np * n2; + l = l + 3617 / (122400 * np); + return Math.exp(l) / scal1; +}; + +var OMEGA = 0.56714329040978387299997; // W(1, 0) +//from https://math.stackexchange.com/a/465183 +// The evaluation can become inaccurate very close to the branch point + +var f_lambertw = function f_lambertw(z,tol = 1e-10) { + var w; + var wn; + + if (!Number.isFinite(z)) { + return z; + } - throw Error("Iteration failed to converge: ".concat(z.toString())); //return Number.NaN; - }; //from https://github.com/scipy/scipy/blob/8dba340293fe20e62e173bdf2c10ae208286692f/scipy/special/lambertw.pxd - // The evaluation can become inaccurate very close to the branch point - // at ``-1/e``. In some corner cases, `lambertw` might currently - // fail to converge, or can end up on the wrong branch. + if (z === 0) { + return z; + } + if (z === 1) { + return OMEGA; + } - function d_lambertw(z) { - var tol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1e-10; - var w; - var ew, wewz, wn; + if (z < 10) { + w = 0; + } else { + w = Math.log(z) - Math.log(Math.log(z)); + } - if (!Number.isFinite(z.mag)) { - return z; - } + for (var i = 0; i < 200; ++i) { + wn = (z * Math.exp(-w) + w ** 2) / (w + 1); - if (z.eq(Decimal.dZero)) { - return z; + if (Math.abs(wn - w) < tol * Math.abs(wn)) { + return wn; + } else { + w = wn; } + } - if (z.eq(Decimal.dOne)) { - //Split out this case because the asymptotic series blows up - return Decimal.fromNumber(OMEGA); - } //Get an initial guess for Halley's method - + throw Error("Iteration failed to converge: ".concat(z.toString())); //return Number.NaN; +}; //from https://github.com/scipy/scipy/blob/8dba340293fe20e62e173bdf2c10ae208286692f/scipy/special/lambertw.pxd +// The evaluation can become inaccurate very close to the branch point +// at ``-1/e``. In some corner cases, `lambertw` might currently +// fail to converge, or can end up on the wrong branch. - w = Decimal.ln(z); //Halley's method; see 5.9 in [1] - for (var i = 0; i < 100; ++i) { - ew = w.neg().exp(); - wewz = w.sub(z.mul(ew)); - wn = w.sub(wewz.div(w.add(1).sub(w.add(2).mul(wewz).div(Decimal.mul(2, w).add(2))))); +function d_lambertw(z , tol=1e-10) { + var w; + var ew, wewz, wn; - if (Decimal.abs(wn.sub(w)).lt(Decimal.abs(wn).mul(tol))) { - return wn; - } else { - w = wn; - } - } + if (!Number.isFinite(z.mag)) { + return z; + } - throw Error("Iteration failed to converge: ".concat(z.toString())); //return Decimal.dNaN; + if (z.eq(Decimal.dZero)) { + return z; } - /** - * The Decimal's value is simply mantissa * 10^exponent. - */ + if (z.eq(Decimal.dOne)) { + //Split out this case because the asymptotic series blows up + return Decimal.fromNumber(OMEGA); + } //Get an initial guess for Halley's method - var Decimal = /*#__PURE__*/function () { - function Decimal(value) { - _classCallCheck(this, Decimal); - this.sign = 0; - this.mag = 0; - this.layer = 0; + w = Decimal.ln(z); //Halley's method; see 5.9 in [1] - if (value instanceof Decimal) { - this.fromDecimal(value); - } else if (typeof value === "number") { - this.fromNumber(value); - } else if (typeof value === "string") { - this.fromString(value); - } + for (var i = 0; i < 200; ++i) { + ew = w.neg().exp(); + wewz = w.sub(z.mul(ew)); + wn = w.sub(wewz.div(w.add(1).sub(w.add(2).mul(wewz).div(Decimal.mul(2, w).add(2))))); + + if (Decimal.abs(wn.sub(w)).lt(Decimal.abs(wn).mul(tol))) { + return wn; + } else { + w = wn; } + } - _createClass(Decimal, [{ - key: "m", - get: function get() { - if (this.sign === 0) { - return 0; - } else if (this.layer === 0) { - var exp = Math.floor(Math.log10(this.mag)); //handle special case 5e-324 + throw Error("Iteration failed to converge: ".concat(z.toString())); //return Decimal.dNaN; +} +/** + * The Decimal's value is simply mantissa * 10^exponent. + */ + + +var Decimal = /*#__PURE__*/function () { + function Decimal(value) { + _classCallCheck(this, Decimal); + + this.sign = 0; + this.mag = 0; + this.layer = 0; + + if (value instanceof Decimal) { + this.fromDecimal(value); + } else if (typeof value === "number") { + this.fromNumber(value); + } else if (typeof value === "string") { + this.fromString(value); + } else if (typeof value === "bigint") { + this.fromString(value + ''); + } + } - var man; + _createClass(Decimal, [{ + key: "m", + get: function get() { + if (this.sign === 0) { + return 0; + } else if (this.layer === 0) { + var exp = Math.floor(Math.log10(this.mag)); //handle special case 5e-324 - if (this.mag === 5e-324) { - man = 5; - } else { - man = this.mag / powerOf10(exp); - } + var man; - return this.sign * man; - } else if (this.layer === 1) { - var residue = this.mag - Math.floor(this.mag); - return this.sign * Math.pow(10, residue); + if (this.mag === 5e-324) { + man = 5; } else { - //mantissa stops being relevant past 1e9e15 / ee15.954 - return this.sign; + man = this.mag / powerOf10(exp); } - }, - set: function set(value) { - if (this.layer <= 2) { - this.fromMantissaExponent(value, this.e); - } else { - //don't even pretend mantissa is meaningful - this.sign = Math.sign(value); - if (this.sign === 0) { - this.layer = 0; - this.exponent = 0; - } - } + return this.sign * man; + } else if (this.layer === 1) { + var residue = this.mag - Math.floor(this.mag); + return this.sign * Math.pow(10, residue); + } else { + //mantissa stops being relevant past 1e9e15 / ee15.954 + return this.sign; } - }, { - key: "e", - get: function get() { + }, + set: function set(value) { + if (this.layer <= 2) { + this.fromMantissaExponent(value, this.e); + } else { + //don't even pretend mantissa is meaningful + this.sign = Math.sign(value); + if (this.sign === 0) { - return 0; - } else if (this.layer === 0) { - return Math.floor(Math.log10(this.mag)); - } else if (this.layer === 1) { - return Math.floor(this.mag); - } else if (this.layer === 2) { - return Math.floor(Math.sign(this.mag) * Math.pow(10, Math.abs(this.mag))); - } else { - return this.mag * Number.POSITIVE_INFINITY; - } - }, - set: function set(value) { - this.fromMantissaExponent(this.m, value); - } - }, { - key: "s", - get: function get() { - return this.sign; - }, - set: function set(value) { - if (value === 0) { - this.sign = 0; - this.layer = 0; - this.mag = 0; - } else { - this.sign = value; - } - } // Object.defineProperty(Decimal.prototype, "mantissa", { - - }, { - key: "mantissa", - get: function get() { - return this.m; - }, - set: function set(value) { - this.m = value; - } - }, { - key: "exponent", - get: function get() { - return this.e; - }, - set: function set(value) { - this.e = value; - } - }, { - key: "normalize", - value: function normalize() { - /* - PSEUDOCODE: - Whenever we are partially 0 (sign is 0 or mag and layer is 0), make it fully 0. - Whenever we are at or hit layer 0, extract sign from negative mag. - If layer === 0 and mag < FIRST_NEG_LAYER (1/9e15), shift to 'first negative layer' (add layer, log10 mag). - While abs(mag) > EXP_LIMIT (9e15), layer += 1, mag = maglog10(mag). - While abs(mag) < LAYER_DOWN (15.954) and layer > 0, layer -= 1, mag = pow(10, mag). - When we're done, all of the following should be true OR one of the numbers is not IsFinite OR layer is not IsInteger (error state): - Any 0 is totally zero (0, 0, 0). - Anything layer 0 has mag 0 OR mag > 1/9e15 and < 9e15. - Anything layer 1 or higher has abs(mag) >= 15.954 and < 9e15. - We will assume in calculations that all Decimals are either erroneous or satisfy these criteria. (Otherwise: Garbage in, garbage out.) - */ - if (this.sign === 0 || this.mag === 0 && this.layer === 0) { - this.sign = 0; - this.mag = 0; this.layer = 0; - return this; - } - - if (this.layer === 0 && this.mag < 0) { - //extract sign from negative mag at layer 0 - this.mag = -this.mag; - this.sign = -this.sign; - } //Handle shifting from layer 0 to negative layers. - - - if (this.layer === 0 && this.mag < FIRST_NEG_LAYER) { - this.layer += 1; - this.mag = Math.log10(this.mag); - return this; - } - - var absmag = Math.abs(this.mag); - var signmag = Math.sign(this.mag); - - if (absmag >= EXP_LIMIT) { - this.layer += 1; - this.mag = signmag * Math.log10(absmag); - return this; - } else { - while (absmag < LAYER_DOWN && this.layer > 0) { - this.layer -= 1; - - if (this.layer === 0) { - this.mag = Math.pow(10, this.mag); - } else { - this.mag = signmag * Math.pow(10, absmag); - absmag = Math.abs(this.mag); - signmag = Math.sign(this.mag); - } - } - - if (this.layer === 0) { - if (this.mag < 0) { - //extract sign from negative mag at layer 0 - this.mag = -this.mag; - this.sign = -this.sign; - } else if (this.mag === 0) { - //excessive rounding can give us all zeroes - this.sign = 0; - } - } + this.exponent = 0; } - - return this; - } - }, { - key: "fromComponents", - value: function fromComponents(sign, layer, mag) { - this.sign = sign; - this.layer = layer; - this.mag = mag; - this.normalize(); - return this; - } - }, { - key: "fromComponents_noNormalize", - value: function fromComponents_noNormalize(sign, layer, mag) { - this.sign = sign; - this.layer = layer; - this.mag = mag; - return this; } - }, { - key: "fromMantissaExponent", - value: function fromMantissaExponent(mantissa, exponent) { - this.layer = 1; - this.sign = Math.sign(mantissa); - mantissa = Math.abs(mantissa); - this.mag = exponent + Math.log10(mantissa); - this.normalize(); - return this; - } - }, { - key: "fromMantissaExponent_noNormalize", - value: function fromMantissaExponent_noNormalize(mantissa, exponent) { - //The idea of 'normalizing' a break_infinity.js style Decimal doesn't really apply. So just do the same thing. - this.fromMantissaExponent(mantissa, exponent); - return this; + } + }, { + key: "e", + get: function get() { + if (this.sign === 0) { + return 0; + } else if (this.layer === 0) { + return Math.floor(Math.log10(this.mag)); + } else if (this.layer === 1) { + return Math.floor(this.mag); + } else if (this.layer === 2) { + return Math.floor(Math.sign(this.mag) * Math.pow(10, Math.abs(this.mag))); + } else { + return this.mag * Number.POSITIVE_INFINITY; } - }, { - key: "fromDecimal", - value: function fromDecimal(value) { - this.sign = value.sign; - this.layer = value.layer; - this.mag = value.mag; - return this; + }, + set: function set(value) { + this.fromMantissaExponent(this.m, value); + } + }, { + key: "s", + get: function get() { + return this.sign; + }, + set: function set(value) { + if (value === 0) { + this.sign = 0; + this.layer = 0; + this.mag = 0; + } else { + this.sign = value; } - }, { - key: "fromNumber", - value: function fromNumber(value) { - this.mag = Math.abs(value); - this.sign = Math.sign(value); + } // Object.defineProperty(Decimal.prototype, "mantissa", { + + }, { + key: "mantissa", + get: function get() { + return this.m; + }, + set: function set(value) { + this.m = value; + } + }, { + key: "exponent", + get: function get() { + return this.e; + }, + set: function set(value) { + this.e = value; + } + }, { + key: "normalize", + value: function normalize() { + /* + PSEUDOCODE: + Whenever we are partially 0 (sign is 0 or mag and layer is 0), make it fully 0. + Whenever we are at or hit layer 0, extract sign from negative mag. + If layer === 0 and mag < FIRST_NEG_LAYER (1/9e15), shift to 'first negative layer' (add layer, log10 mag). + While abs(mag) > EXP_LIMIT (9e15), layer += 1, mag = maglog10(mag). + While abs(mag) < LAYER_DOWN (15.954) and layer > 0, layer -= 1, mag = pow(10, mag). + When we're done, all of the following should be true OR one of the numbers is not IsFinite OR layer is not IsInteger (error state): + Any 0 is totally zero (0, 0, 0). + Anything layer 0 has mag 0 OR mag > 1/9e15 and < 9e15. + Anything layer 1 or higher has abs(mag) >= 15.954 and < 9e15. + We will assume in calculations that all Decimals are either erroneous or satisfy these criteria. (Otherwise: Garbage in, garbage out.) + */ + if (this.sign === 0 || this.mag === 0 && this.layer === 0) { + this.sign = 0; + this.mag = 0; this.layer = 0; - this.normalize(); return this; } - }, { - key: "fromString", - value: function fromString(value) { - var originalValue = value; - var cached = Decimal.fromStringCache.get(originalValue); - - if (cached !== undefined) { - return this.fromDecimal(cached); - } - - { - value = value.replace(",", ""); - } //Handle x^^^y format. - - var pentationparts = value.split("^^^"); + if (this.layer === 0 && this.mag < 0) { + //extract sign from negative mag at layer 0 + this.mag = -this.mag; + this.sign = -this.sign; + } //Handle shifting from layer 0 to negative layers. - if (pentationparts.length === 2) { - var _base = parseFloat(pentationparts[0]); - var _height = parseFloat(pentationparts[1]); + if (this.layer === 0 && this.mag < FIRST_NEG_LAYER) { + this.layer += 1; + this.mag = Math.log10(this.mag); + return this; + } - var heightparts = pentationparts[1].split(";"); - var payload = 1; + var absmag = Math.abs(this.mag); + var signmag = Math.sign(this.mag); - if (heightparts.length === 2) { - payload = parseFloat(heightparts[1]); + if (absmag >= EXP_LIMIT) { + this.layer += 1; + this.mag = signmag * Math.log10(absmag); + return this; + } else { + while (absmag < LAYER_DOWN && this.layer > 0) { + this.layer -= 1; - if (!isFinite(payload)) { - payload = 1; - } + if (this.layer === 0) { + this.mag = Math.pow(10, this.mag); + } else { + this.mag = signmag * Math.pow(10, absmag); + absmag = Math.abs(this.mag); + signmag = Math.sign(this.mag); } + } - if (isFinite(_base) && isFinite(_height)) { - var result = Decimal.pentate(_base, _height, payload); - this.sign = result.sign; - this.layer = result.layer; - this.mag = result.mag; - - if (Decimal.fromStringCache.maxSize >= 1) { - Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); - } - - return this; + if (this.layer === 0) { + if (this.mag < 0) { + //extract sign from negative mag at layer 0 + this.mag = -this.mag; + this.sign = -this.sign; + } else if (this.mag === 0) { + //excessive rounding can give us all zeroes + this.sign = 0; } - } //Handle x^^y format. - - - var tetrationparts = value.split("^^"); + } + } - if (tetrationparts.length === 2) { - var _base2 = parseFloat(tetrationparts[0]); + return this; + } + }, { + key: "fromComponents", + value: function fromComponents(sign, layer, mag) { + this.sign = sign; + this.layer = layer; + this.mag = mag; + this.normalize(); + return this; + } + }, { + key: "fromComponents_noNormalize", + value: function fromComponents_noNormalize(sign, layer, mag) { + this.sign = sign; + this.layer = layer; + this.mag = mag; + return this; + } + }, { + key: "fromMantissaExponent", + value: function fromMantissaExponent(mantissa, exponent) { + this.layer = 1; + this.sign = Math.sign(mantissa); + mantissa = Math.abs(mantissa); + this.mag = exponent + Math.log10(mantissa); + this.normalize(); + return this; + } + }, { + key: "fromMantissaExponent_noNormalize", + value: function fromMantissaExponent_noNormalize(mantissa, exponent) { + //The idea of 'normalizing' a break_infinity.js style Decimal doesn't really apply. So just do the same thing. + this.fromMantissaExponent(mantissa, exponent); + return this; + } + }, { + key: "fromDecimal", + value: function fromDecimal(value) { + this.sign = value.sign; + this.layer = value.layer; + this.mag = value.mag; + return this; + } + }, { + key: "fromNumber", + value: function fromNumber(value) { + this.mag = Math.abs(value); + this.sign = Math.sign(value); + this.layer = 0; + this.normalize(); + return this; + } + }, { + key: "fromString", + value: function fromString(value) { + var originalValue = value; + var cached = Decimal.fromStringCache.get(originalValue); - var _height2 = parseFloat(tetrationparts[1]); + if (cached !== undefined) { + return this.fromDecimal(cached); + } - var _heightparts = tetrationparts[1].split(";"); + { + value = value.replace(",", ""); + } //Handle x^^^y format. - var _payload = 1; - if (_heightparts.length === 2) { - _payload = parseFloat(_heightparts[1]); + var pentationparts = value.split("^^^"); - if (!isFinite(_payload)) { - _payload = 1; - } - } + if (pentationparts.length === 2) { + var _base = parseFloat(pentationparts[0]); - if (isFinite(_base2) && isFinite(_height2)) { - var _result = Decimal.tetrate(_base2, _height2, _payload); + var _height = parseFloat(pentationparts[1]); - this.sign = _result.sign; - this.layer = _result.layer; - this.mag = _result.mag; + var heightparts = pentationparts[1].split(";"); + var payload = 1; - if (Decimal.fromStringCache.maxSize >= 1) { - Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); - } + if (heightparts.length === 2) { + payload = parseFloat(heightparts[1]); - return this; + if (!isFinite(payload)) { + payload = 1; } - } //Handle x^y format. - - - var powparts = value.split("^"); - - if (powparts.length === 2) { - var _base3 = parseFloat(powparts[0]); + } - var _exponent = parseFloat(powparts[1]); + if (isFinite(_base) && isFinite(_height)) { + var result = Decimal.pentate(_base, _height, payload); + this.sign = result.sign; + this.layer = result.layer; + this.mag = result.mag; - if (isFinite(_base3) && isFinite(_exponent)) { - var _result2 = Decimal.pow(_base3, _exponent); + if (Decimal.fromStringCache.maxSize >= 1) { + Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); + } - this.sign = _result2.sign; - this.layer = _result2.layer; - this.mag = _result2.mag; + return this; + } + } //Handle x^^y format. - if (Decimal.fromStringCache.maxSize >= 1) { - Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); - } - return this; - } - } //Handle various cases involving it being a Big Number. + var tetrationparts = value.split("^^"); + if (tetrationparts.length === 2) { + var _base2 = parseFloat(tetrationparts[0]); - value = value.trim().toLowerCase(); //handle X PT Y format. + var _height2 = parseFloat(tetrationparts[1]); - var base; - var height; - var ptparts = value.split("pt"); + var _heightparts = tetrationparts[1].split(";"); - if (ptparts.length === 2) { - base = 10; - height = parseFloat(ptparts[0]); - ptparts[1] = ptparts[1].replace("(", ""); - ptparts[1] = ptparts[1].replace(")", ""); + var _payload = 1; - var _payload2 = parseFloat(ptparts[1]); + if (_heightparts.length === 2) { + _payload = parseFloat(_heightparts[1]); - if (!isFinite(_payload2)) { - _payload2 = 1; + if (!isFinite(_payload)) { + _payload = 1; } + } - if (isFinite(base) && isFinite(height)) { - var _result3 = Decimal.tetrate(base, height, _payload2); - - this.sign = _result3.sign; - this.layer = _result3.layer; - this.mag = _result3.mag; + if (isFinite(_base2) && isFinite(_height2)) { + var _result = Decimal.tetrate(_base2, _height2, _payload); - if (Decimal.fromStringCache.maxSize >= 1) { - Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); - } + this.sign = _result.sign; + this.layer = _result.layer; + this.mag = _result.mag; - return this; + if (Decimal.fromStringCache.maxSize >= 1) { + Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); } - } //handle XpY format (it's the same thing just with p). + return this; + } + } //Handle x^y format. - ptparts = value.split("p"); - - if (ptparts.length === 2) { - base = 10; - height = parseFloat(ptparts[0]); - ptparts[1] = ptparts[1].replace("(", ""); - ptparts[1] = ptparts[1].replace(")", ""); - var _payload3 = parseFloat(ptparts[1]); + var powparts = value.split("^"); - if (!isFinite(_payload3)) { - _payload3 = 1; - } + if (powparts.length === 2) { + var _base3 = parseFloat(powparts[0]); - if (isFinite(base) && isFinite(height)) { - var _result4 = Decimal.tetrate(base, height, _payload3); + var _exponent = parseFloat(powparts[1]); - this.sign = _result4.sign; - this.layer = _result4.layer; - this.mag = _result4.mag; + if (isFinite(_base3) && isFinite(_exponent)) { + var _result2 = Decimal.pow(_base3, _exponent); - if (Decimal.fromStringCache.maxSize >= 1) { - Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); - } + this.sign = _result2.sign; + this.layer = _result2.layer; + this.mag = _result2.mag; - return this; + if (Decimal.fromStringCache.maxSize >= 1) { + Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); } + + return this; } + } //Handle various cases involving it being a Big Number. - var parts = value.split("e"); - var ecount = parts.length - 1; //Handle numbers that are exactly floats (0 or 1 es). - if (ecount === 0) { - var numberAttempt = parseFloat(value); + value = value.trim().toLowerCase(); //handle X PT Y format. - if (isFinite(numberAttempt)) { - this.fromNumber(numberAttempt); + var base; + var height; + var ptparts = value.split("pt"); + if (ptparts.length === 1) ptparts = value.split("PT"); + if (ptparts.length === 2) { + base = 10; + height = parseFloat(ptparts[0]); + ptparts[1] = ptparts[1].replace("(", ""); + ptparts[1] = ptparts[1].replace(")", ""); - if (Decimal.fromStringCache.size >= 1) { - Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); - } + var _payload2 = parseFloat(ptparts[1]); - return this; - } - } else if (ecount === 1) { - //Very small numbers ("2e-3000" and so on) may look like valid floats but round to 0. - var _numberAttempt = parseFloat(value); + if (!isFinite(_payload2)) { + _payload2 = 1; + } - if (isFinite(_numberAttempt) && _numberAttempt !== 0) { - this.fromNumber(_numberAttempt); + if (isFinite(base) && isFinite(height)) { + var _result3 = Decimal.tetrate(base, height, _payload2); - if (Decimal.fromStringCache.maxSize >= 1) { - Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); - } + this.sign = _result3.sign; + this.layer = _result3.layer; + this.mag = _result3.mag; - return this; + if (Decimal.fromStringCache.maxSize >= 1) { + Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); } - } //Handle new (e^N)X format. - - var newparts = value.split("e^"); + return this; + } + } //handle XpY format (it's the same thing just with p). - if (newparts.length === 2) { - this.sign = 1; - if (newparts[0].charAt(0) == "-") { - this.sign = -1; - } + ptparts = value.split("p"); + if (ptparts.length === 1) ptparts = value.split("P"); + if (ptparts.length === 2) { + base = 10; + height = parseFloat(ptparts[0]); + ptparts[1] = ptparts[1].replace("(", ""); + ptparts[1] = ptparts[1].replace(")", ""); - var layerstring = ""; + var _payload3 = parseFloat(ptparts[1]); - for (var i = 0; i < newparts[1].length; ++i) { - var chrcode = newparts[1].charCodeAt(i); + if (!isFinite(_payload3)) { + _payload3 = 1; + } - if (chrcode >= 43 && chrcode <= 57 || chrcode === 101) { - //is "0" to "9" or "+" or "-" or "." or "e" (or "," or "/") - layerstring += newparts[1].charAt(i); - } //we found the end of the layer count - else { - this.layer = parseFloat(layerstring); - this.mag = parseFloat(newparts[1].substr(i + 1)); - this.normalize(); + if (isFinite(base) && isFinite(height)) { + var _result4 = Decimal.tetrate(base, height, _payload3); - if (Decimal.fromStringCache.maxSize >= 1) { - Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); - } + this.sign = _result4.sign; + this.layer = _result4.layer; + this.mag = _result4.mag; - return this; - } + if (Decimal.fromStringCache.maxSize >= 1) { + Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); } + + return this; } + } - if (ecount < 1) { - this.sign = 0; - this.layer = 0; - this.mag = 0; + var parts = value.split("e"); + var ecount = parts.length - 1; //Handle numbers that are exactly floats (0 or 1 es). - if (Decimal.fromStringCache.maxSize >= 1) { + if (ecount === 0) { + var numberAttempt = parseFloat(value); + + if (isFinite(numberAttempt)) { + this.fromNumber(numberAttempt); + + if (Decimal.fromStringCache.size >= 1) { Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); } + return this; + }else { + if(value[0] === '-')return Decimal.fromString(value.substr(1)).mul(-1); + var exponent = value.length - 1; + var mantissa = value[0] + '.' + value.substr(1); + this.fromMantissaExponent(parseFloat(mantissa),exponent); return this; } + } else if (ecount === 1) { + //Very small numbers ("2e-3000" and so on) may look like valid floats but round to 0. + var _numberAttempt = parseFloat(value); - var mantissa = parseFloat(parts[0]); - - if (mantissa === 0) { - this.sign = 0; - this.layer = 0; - this.mag = 0; + if (isFinite(_numberAttempt) && _numberAttempt !== 0) { + this.fromNumber(_numberAttempt); if (Decimal.fromStringCache.maxSize >= 1) { Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); @@ -876,53 +818,45 @@ return this; } + } //Handle new (e^N)X format. - var exponent = parseFloat(parts[parts.length - 1]); //handle numbers like AeBeC and AeeeeBeC - if (ecount >= 2) { - var me = parseFloat(parts[parts.length - 2]); - - if (isFinite(me)) { - exponent *= Math.sign(me); - exponent += f_maglog10(me); - } - } //Handle numbers written like eee... (N es) X + var newparts = value.split("e^"); + if (newparts.length === 2) { + this.sign = 1; - if (!isFinite(mantissa)) { - this.sign = parts[0] === "-" ? -1 : 1; - this.layer = ecount; - this.mag = exponent; - } //Handle numbers written like XeY - else if (ecount === 1) { - this.sign = Math.sign(mantissa); - this.layer = 1; //Example: 2e10 is equal to 10^log10(2e10) which is equal to 10^(10+log10(2)) + if (newparts[0].charAt(0) == "-") { + this.sign = -1; + } - this.mag = exponent + Math.log10(Math.abs(mantissa)); - } //Handle numbers written like Xeee... (N es) Y - else { - this.sign = Math.sign(mantissa); - this.layer = ecount; + var layerstring = ""; - if (ecount === 2) { - var _result5 = Decimal.mul(FC(1, 2, exponent), D(mantissa)); + for (var i = 0; i < newparts[1].length; ++i) { + var chrcode = newparts[1].charCodeAt(i); - this.sign = _result5.sign; - this.layer = _result5.layer; - this.mag = _result5.mag; + if (chrcode >= 43 && chrcode <= 57 || chrcode === 101) { + //is "0" to "9" or "+" or "-" or "." or "e" (or "," or "/") + layerstring += newparts[1].charAt(i); + } //we found the end of the layer count + else { + this.layer = parseFloat(layerstring); + this.mag = parseFloat(newparts[1].substr(i + 1)); + this.normalize(); if (Decimal.fromStringCache.maxSize >= 1) { Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); } return this; - } else { - //at eee and above, mantissa is too small to be recognizable! - this.mag = exponent; } } + } - this.normalize(); + if (ecount < 1) { + this.sign = 0; + this.layer = 0; + this.mag = 0; if (Decimal.fromStringCache.maxSize >= 1) { Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); @@ -930,2337 +864,2403 @@ return this; } - }, { - key: "fromValue", - value: function fromValue(value) { - if (value instanceof Decimal) { - return this.fromDecimal(value); - } - if (typeof value === "number") { - return this.fromNumber(value); - } - - if (typeof value === "string") { - return this.fromString(value); - } + var mantissa = parseFloat(parts[0]); + if (mantissa === 0) { this.sign = 0; this.layer = 0; this.mag = 0; - return this; - } - }, { - key: "toNumber", - value: function toNumber() { - if (!Number.isFinite(this.layer)) { - return Number.NaN; - } - - if (this.layer === 0) { - return this.sign * this.mag; - } else if (this.layer === 1) { - return this.sign * Math.pow(10, this.mag); - } //overflow for any normalized Decimal - else { - return this.mag > 0 ? this.sign > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY : 0; - } - } - }, { - key: "mantissaWithDecimalPlaces", - value: function mantissaWithDecimalPlaces(places) { - // https://stackoverflow.com/a/37425022 - if (isNaN(this.m)) { - return Number.NaN; - } - if (this.m === 0) { - return 0; + if (Decimal.fromStringCache.maxSize >= 1) { + Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); } - return decimalPlaces(this.m, places); + return this; } - }, { - key: "magnitudeWithDecimalPlaces", - value: function magnitudeWithDecimalPlaces(places) { - // https://stackoverflow.com/a/37425022 - if (isNaN(this.mag)) { - return Number.NaN; - } - if (this.mag === 0) { - return 0; - } + var exponent = parseFloat(parts[parts.length - 1]); //handle numbers like AeBeC and AeeeeBeC - return decimalPlaces(this.mag, places); - } - }, { - key: "toString", - value: function toString() { - if (isNaN(this.layer) || isNaN(this.sign) || isNaN(this.mag)) { - return "NaN"; - } + if (ecount >= 2) { + var me = parseFloat(parts[parts.length - 2]); - if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) { - return this.sign === 1 ? "Infinity" : "-Infinity"; + if (isFinite(me)) { + exponent *= Math.sign(me); + exponent += f_maglog10(me); } + } //Handle numbers written like eee... (N es) X - if (this.layer === 0) { - if (this.mag < 1e21 && this.mag > 1e-7 || this.mag === 0) { - return (this.sign * this.mag).toString(); - } - return this.m + "e" + this.e; - } else if (this.layer === 1) { - return this.m + "e" + this.e; - } else { - //layer 2+ - if (this.layer <= MAX_ES_IN_A_ROW) { - return (this.sign === -1 ? "-" : "") + "e".repeat(this.layer) + this.mag; - } else { - return (this.sign === -1 ? "-" : "") + "(e^" + this.layer + ")" + this.mag; - } - } - } - }, { - key: "toExponential", - value: function toExponential(places) { - if (this.layer === 0) { - return (this.sign * this.mag).toExponential(places); - } + if (!isFinite(mantissa)) { + this.sign = parts[0] === "-" ? -1 : 1; + this.layer = ecount; + this.mag = exponent; + } //Handle numbers written like XeY + else if (ecount === 1) { + this.sign = Math.sign(mantissa); + this.layer = 1; //Example: 2e10 is equal to 10^log10(2e10) which is equal to 10^(10+log10(2)) - return this.toStringWithDecimalPlaces(places); - } - }, { - key: "toFixed", - value: function toFixed(places) { - if (this.layer === 0) { - return (this.sign * this.mag).toFixed(places); - } + this.mag = exponent + Math.log10(Math.abs(mantissa)); + } //Handle numbers written like Xeee... (N es) Y + else { + this.sign = Math.sign(mantissa); + this.layer = ecount; - return this.toStringWithDecimalPlaces(places); - } - }, { - key: "toPrecision", - value: function toPrecision(places) { - if (this.e <= -7) { - return this.toExponential(places - 1); - } + if (ecount === 2) { + var _result5 = Decimal.mul(FC(1, 2, exponent), D(mantissa)); - if (places > this.e) { - return this.toFixed(places - this.exponent - 1); - } + this.sign = _result5.sign; + this.layer = _result5.layer; + this.mag = _result5.mag; - return this.toExponential(places - 1); - } - }, { - key: "valueOf", - value: function valueOf() { - return this.toString(); - } - }, { - key: "toJSON", - value: function toJSON() { - return this.toString(); - } - }, { - key: "toStringWithDecimalPlaces", - value: function toStringWithDecimalPlaces(places) { - if (this.layer === 0) { - if (this.mag < 1e21 && this.mag > 1e-7 || this.mag === 0) { - return (this.sign * this.mag).toFixed(places); + if (Decimal.fromStringCache.maxSize >= 1) { + Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); } - return decimalPlaces(this.m, places) + "e" + decimalPlaces(this.e, places); - } else if (this.layer === 1) { - return decimalPlaces(this.m, places) + "e" + decimalPlaces(this.e, places); + return this; } else { - //layer 2+ - if (this.layer <= MAX_ES_IN_A_ROW) { - return (this.sign === -1 ? "-" : "") + "e".repeat(this.layer) + decimalPlaces(this.mag, places); - } else { - return (this.sign === -1 ? "-" : "") + "(e^" + this.layer + ")" + decimalPlaces(this.mag, places); - } + //at eee and above, mantissa is too small to be recognizable! + this.mag = exponent; } } - }, { - key: "abs", - value: function abs() { - return FC_NN(this.sign === 0 ? 0 : 1, this.layer, this.mag); - } - }, { - key: "neg", - value: function neg() { - return FC_NN(-this.sign, this.layer, this.mag); - } - }, { - key: "negate", - value: function negate() { - return this.neg(); - } - }, { - key: "negated", - value: function negated() { - return this.neg(); - } // public sign () { - // return this.sign; - // } - - }, { - key: "sgn", - value: function sgn() { - return this.sign; - } - }, { - key: "round", - value: function round() { - if (this.mag < 0) { - return Decimal.dZero; - } - if (this.layer === 0) { - return FC(this.sign, 0, Math.round(this.mag)); - } + this.normalize(); - return this; + if (Decimal.fromStringCache.maxSize >= 1) { + Decimal.fromStringCache.set(originalValue, Decimal.fromDecimal(this)); } - }, { - key: "floor", - value: function floor() { - if (this.mag < 0) { - return Decimal.dZero; - } - if (this.layer === 0) { - return FC(this.sign, 0, Math.floor(this.mag)); - } - - return this; + return this; + } + }, { + key: "fromValue", + value: function fromValue(value) { + if (value instanceof Decimal) { + return this.fromDecimal(value); } - }, { - key: "ceil", - value: function ceil() { - if (this.mag < 0) { - return Decimal.dZero; - } - if (this.layer === 0) { - return FC(this.sign, 0, Math.ceil(this.mag)); - } - - return this; + if (typeof value === "number") { + return this.fromNumber(value); } - }, { - key: "trunc", - value: function trunc() { - if (this.mag < 0) { - return Decimal.dZero; - } - - if (this.layer === 0) { - return FC(this.sign, 0, Math.trunc(this.mag)); - } - return this; + if (typeof value === "string") { + return this.fromString(value); } - }, { - key: "add", - value: function add(value) { - var decimal = D(value); //inf/nan check - - if (!Number.isFinite(this.layer)) { - return this; - } - if (!Number.isFinite(decimal.layer)) { - return decimal; - } //Special case - if one of the numbers is 0, return the other number. + this.sign = 0; + this.layer = 0; + this.mag = 0; + return this; + } + }, { + key: "toNumber", + value: function toNumber() { + if (!Number.isFinite(this.layer)) { + return Number.NaN; + } + if (this.layer === 0) { + return this.sign * this.mag; + } else if (this.layer === 1) { + return this.sign * Math.pow(10, this.mag); + } //overflow for any normalized Decimal + else { + return this.mag > 0 ? this.sign > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY : 0; + } + } + }, { + key: "mantissaWithDecimalPlaces", + value: function mantissaWithDecimalPlaces(places) { + // https://stackoverflow.com/a/37425022 + if (isNaN(this.m)) { + return Number.NaN; + } - if (this.sign === 0) { - return decimal; - } + if (this.m === 0) { + return 0; + } - if (decimal.sign === 0) { - return this; - } //Special case - Adding a number to its negation produces 0, no matter how large. + return decimalPlaces(this.m, places); + } + }, { + key: "magnitudeWithDecimalPlaces", + value: function magnitudeWithDecimalPlaces(places) { + // https://stackoverflow.com/a/37425022 + if (isNaN(this.mag)) { + return Number.NaN; + } + if (this.mag === 0) { + return 0; + } - if (this.sign === -decimal.sign && this.layer === decimal.layer && this.mag === decimal.mag) { - return FC_NN(0, 0, 0); - } + return decimalPlaces(this.mag, places); + } + }, { + key: "toString", + value: function toString() { + if (isNaN(this.layer) || isNaN(this.sign) || isNaN(this.mag)) { + return "NaN"; + } - var a; - var b; //Special case: If one of the numbers is layer 2 or higher, just take the bigger number. + if (this.mag === Number.POSITIVE_INFINITY || this.layer === Number.POSITIVE_INFINITY) { + return this.sign === 1 ? "Infinity" : "-Infinity"; + } - if (this.layer >= 2 || decimal.layer >= 2) { - return this.maxabs(decimal); + if (this.layer === 0) { + if (this.mag < 1e21 && this.mag > 1e-7 || this.mag === 0) { + return (this.sign * this.mag).toString(); } - if (Decimal.cmpabs(this, decimal) > 0) { - a = this; - b = decimal; + return this.m + "e" + this.e; + } else if (this.layer === 1) { + return this.m + "e" + this.e; + } else { + //layer 2+ + if (this.layer <= MAX_ES_IN_A_ROW) { + return (this.sign === -1 ? "-" : "") + "e".repeat(this.layer) + this.mag; } else { - a = decimal; - b = this; - } - - if (a.layer === 0 && b.layer === 0) { - return Decimal.fromNumber(a.sign * a.mag + b.sign * b.mag); - } - - var layera = a.layer * Math.sign(a.mag); - var layerb = b.layer * Math.sign(b.mag); //If one of the numbers is 2+ layers higher than the other, just take the bigger number. - - if (layera - layerb >= 2) { - return a; + return (this.sign === -1 ? "-" : "") + "(e^" + this.layer + ")" + this.mag; } + } + } + }, { + key: "toExponential", + value: function toExponential(places) { + if (this.layer === 0) { + return (this.sign * this.mag).toExponential(places); + } - if (layera === 0 && layerb === -1) { - if (Math.abs(b.mag - Math.log10(a.mag)) > MAX_SIGNIFICANT_DIGITS) { - return a; - } else { - var magdiff = Math.pow(10, Math.log10(a.mag) - b.mag); - var mantissa = b.sign + a.sign * magdiff; - return FC(Math.sign(mantissa), 1, b.mag + Math.log10(Math.abs(mantissa))); - } - } + return this.toStringWithDecimalPlaces(places); + } + }, { + key: "toFixed", + value: function toFixed(places) { + if (this.layer === 0) { + return (this.sign * this.mag).toFixed(places); + } - if (layera === 1 && layerb === 0) { - if (Math.abs(a.mag - Math.log10(b.mag)) > MAX_SIGNIFICANT_DIGITS) { - return a; - } else { - var _magdiff = Math.pow(10, a.mag - Math.log10(b.mag)); + return this.toStringWithDecimalPlaces(places); + } + }, { + key: "toPrecision", + value: function toPrecision(places) { + if (this.e <= -7) { + return this.toExponential(places - 1); + } - var _mantissa = b.sign + a.sign * _magdiff; + if (places > this.e) { + return this.toFixed(places - this.exponent - 1); + } - return FC(Math.sign(_mantissa), 1, Math.log10(b.mag) + Math.log10(Math.abs(_mantissa))); - } + return this.toExponential(places - 1); + } + }, { + key: "valueOf", + value: function valueOf() { + return this.toString(); + } + }, { + key: "toJSON", + value: function toJSON() { + return this.toString(); + } + }, { + key: "toStringWithDecimalPlaces", + value: function toStringWithDecimalPlaces(places) { + if (this.layer === 0) { + if (this.mag < 1e21 && this.mag > 1e-7 || this.mag === 0) { + return (this.sign * this.mag).toFixed(places); } - if (Math.abs(a.mag - b.mag) > MAX_SIGNIFICANT_DIGITS) { - return a; + return decimalPlaces(this.m, places) + "e" + decimalPlaces(this.e, places); + } else if (this.layer === 1) { + return decimalPlaces(this.m, places) + "e" + decimalPlaces(this.e, places); + } else { + //layer 2+ + if (this.layer <= MAX_ES_IN_A_ROW) { + return (this.sign === -1 ? "-" : "") + "e".repeat(this.layer) + decimalPlaces(this.mag, places); } else { - var _magdiff2 = Math.pow(10, a.mag - b.mag); - - var _mantissa2 = b.sign + a.sign * _magdiff2; - - return FC(Math.sign(_mantissa2), 1, b.mag + Math.log10(Math.abs(_mantissa2))); + return (this.sign === -1 ? "-" : "") + "(e^" + this.layer + ")" + decimalPlaces(this.mag, places); } } - }, { - key: "plus", - value: function plus(value) { - return this.add(value); - } - }, { - key: "sub", - value: function sub(value) { - return this.add(D(value).neg()); - } - }, { - key: "subtract", - value: function subtract(value) { - return this.sub(value); - } - }, { - key: "minus", - value: function minus(value) { - return this.sub(value); + } + }, { + key: "abs", + value: function abs() { + return FC_NN(this.sign === 0 ? 0 : 1, this.layer, this.mag); + } + }, { + key: "neg", + value: function neg() { + return FC_NN(-this.sign, this.layer, this.mag); + } + }, { + key: "negate", + value: function negate() { + return this.neg(); + } + }, { + key: "negated", + value: function negated() { + return this.neg(); + } // public sign () { + // return this.sign; + // } + + }, { + key: "sgn", + value: function sgn() { + return this.sign; + } + }, { + key: "round", + value: function round() { + if (this.mag < 0) { + return Decimal.dZero; } - }, { - key: "mul", - value: function mul(value) { - var decimal = D(value); //inf/nan check - if (!Number.isFinite(this.layer)) { - return this; - } + if (this.layer === 0) { + return FC(this.sign, 0, Math.round(this.mag)); + } - if (!Number.isFinite(decimal.layer)) { - return decimal; - } //Special case - if one of the numbers is 0, return 0. + return this; + } + }, { + key: "floor", + value: function floor() { + if (this.mag < 0) { + return Decimal.dZero; + } + if (this.layer === 0) { + return FC(this.sign, 0, Math.floor(this.mag)); + } - if (this.sign === 0 || decimal.sign === 0) { - return FC_NN(0, 0, 0); - } //Special case - Multiplying a number by its own reciprocal yields +/- 1, no matter how large. + return this; + } + }, { + key: "ceil", + value: function ceil() { + if (this.mag < 0) { + return Decimal.dZero; + } + if (this.layer === 0) { + return FC(this.sign, 0, Math.ceil(this.mag)); + } - if (this.layer === decimal.layer && this.mag === -decimal.mag) { - return FC_NN(this.sign * decimal.sign, 0, 1); - } + return this; + } + }, { + key: "trunc", + value: function trunc() { + if (this.mag < 0) { + return Decimal.dZero; + } - var a; - var b; //Which number is bigger in terms of its multiplicative distance from 1? + if (this.layer === 0) { + return FC(this.sign, 0, Math.trunc(this.mag)); + } - if (this.layer > decimal.layer || this.layer == decimal.layer && Math.abs(this.mag) > Math.abs(decimal.mag)) { - a = this; - b = decimal; - } else { - a = decimal; - b = this; - } + return this; + } + }, { + key: "add", + value: function add(value) { + var decimal = D(value); //inf/nan check - if (a.layer === 0 && b.layer === 0) { - return Decimal.fromNumber(a.sign * b.sign * a.mag * b.mag); - } //Special case: If one of the numbers is layer 3 or higher or one of the numbers is 2+ layers bigger than the other, just take the bigger number. + if (!Number.isFinite(this.layer)) { + return this; + } + if (!Number.isFinite(decimal.layer)) { + return decimal; + } //Special case - if one of the numbers is 0, return the other number. - if (a.layer >= 3 || a.layer - b.layer >= 2) { - return FC(a.sign * b.sign, a.layer, a.mag); - } - if (a.layer === 1 && b.layer === 0) { - return FC(a.sign * b.sign, 1, a.mag + Math.log10(b.mag)); - } + if (this.sign === 0) { + return decimal; + } - if (a.layer === 1 && b.layer === 1) { - return FC(a.sign * b.sign, 1, a.mag + b.mag); - } + if (decimal.sign === 0) { + return this; + } //Special case - Adding a number to its negation produces 0, no matter how large. - if (a.layer === 2 && b.layer === 1) { - var newmag = FC(Math.sign(a.mag), a.layer - 1, Math.abs(a.mag)).add(FC(Math.sign(b.mag), b.layer - 1, Math.abs(b.mag))); - return FC(a.sign * b.sign, newmag.layer + 1, newmag.sign * newmag.mag); - } - if (a.layer === 2 && b.layer === 2) { - var _newmag = FC(Math.sign(a.mag), a.layer - 1, Math.abs(a.mag)).add(FC(Math.sign(b.mag), b.layer - 1, Math.abs(b.mag))); + if (this.sign === -decimal.sign && this.layer === decimal.layer && this.mag === decimal.mag) { + return FC_NN(0, 0, 0); + } - return FC(a.sign * b.sign, _newmag.layer + 1, _newmag.sign * _newmag.mag); - } + var a; + var b; //Special case: If one of the numbers is layer 2 or higher, just take the bigger number. - throw Error("Bad arguments to mul: " + this + ", " + value); - } - }, { - key: "multiply", - value: function multiply(value) { - return this.mul(value); - } - }, { - key: "times", - value: function times(value) { - return this.mul(value); - } - }, { - key: "div", - value: function div(value) { - var decimal = D(value); - return this.mul(decimal.recip()); - } - }, { - key: "divide", - value: function divide(value) { - return this.div(value); - } - }, { - key: "divideBy", - value: function divideBy(value) { - return this.div(value); - } - }, { - key: "dividedBy", - value: function dividedBy(value) { - return this.div(value); - } - }, { - key: "recip", - value: function recip() { - if (this.mag === 0) { - return Decimal.dNaN; - } else if (this.layer === 0) { - return FC(this.sign, 0, 1 / this.mag); - } else { - return FC(this.sign, this.layer, -this.mag); - } + if (this.layer >= 2 || decimal.layer >= 2) { + return this.maxabs(decimal); } - }, { - key: "reciprocal", - value: function reciprocal() { - return this.recip(); + + if (Decimal.cmpabs(this, decimal) > 0) { + a = this; + b = decimal; + } else { + a = decimal; + b = this; } - }, { - key: "reciprocate", - value: function reciprocate() { - return this.recip(); + + if (a.layer === 0 && b.layer === 0) { + return Decimal.fromNumber(a.sign * a.mag + b.sign * b.mag); } - /** - * -1 for less than value, 0 for equals value, 1 for greater than value - */ - }, { - key: "cmp", - value: function cmp(value) { - var decimal = D(value); + var layera = a.layer * Math.sign(a.mag); + var layerb = b.layer * Math.sign(b.mag); //If one of the numbers is 2+ layers higher than the other, just take the bigger number. - if (this.sign > decimal.sign) { - return 1; - } + if (layera - layerb >= 2) { + return a; + } - if (this.sign < decimal.sign) { - return -1; + if (layera === 0 && layerb === -1) { + if (Math.abs(b.mag - Math.log10(a.mag)) > MAX_SIGNIFICANT_DIGITS) { + return a; + } else { + var magdiff = Math.pow(10, Math.log10(a.mag) - b.mag); + var mantissa = b.sign + a.sign * magdiff; + return FC(Math.sign(mantissa), 1, b.mag + Math.log10(Math.abs(mantissa))); } - - return this.sign * this.cmpabs(value); } - }, { - key: "cmpabs", - value: function cmpabs(value) { - var decimal = D(value); - var layera = this.mag > 0 ? this.layer : -this.layer; - var layerb = decimal.mag > 0 ? decimal.layer : -decimal.layer; - if (layera > layerb) { - return 1; - } + if (layera === 1 && layerb === 0) { + if (Math.abs(a.mag - Math.log10(b.mag)) > MAX_SIGNIFICANT_DIGITS) { + return a; + } else { + var _magdiff = Math.pow(10, a.mag - Math.log10(b.mag)); - if (layera < layerb) { - return -1; - } + var _mantissa = b.sign + a.sign * _magdiff; - if (this.mag > decimal.mag) { - return 1; + return FC(Math.sign(_mantissa), 1, Math.log10(b.mag) + Math.log10(Math.abs(_mantissa))); } + } - if (this.mag < decimal.mag) { - return -1; - } + if (Math.abs(a.mag - b.mag) > MAX_SIGNIFICANT_DIGITS) { + return a; + } else { + var _magdiff2 = Math.pow(10, a.mag - b.mag); - return 0; + var _mantissa2 = b.sign + a.sign * _magdiff2; + + return FC(Math.sign(_mantissa2), 1, b.mag + Math.log10(Math.abs(_mantissa2))); } - }, { - key: "compare", - value: function compare(value) { - return this.cmp(value); - } - }, { - key: "isNan", - value: function isNan() { - return isNaN(this.sign) || isNaN(this.layer) || isNaN(this.mag); - } - }, { - key: "isFinite", - value: function (_isFinite2) { - function isFinite() { - return _isFinite2.apply(this, arguments); - } + } + }, { + key: "plus", + value: function plus(value) { + return this.add(value); + } + }, { + key: "sub", + value: function sub(value) { + return this.add(D(value).neg()); + } + }, { + key: "subtract", + value: function subtract(value) { + return this.sub(value); + } + }, { + key: "minus", + value: function minus(value) { + return this.sub(value); + } + }, { + key: "mul", + value: function mul(value) { + var decimal = D(value); //inf/nan check - isFinite.toString = function () { - return _isFinite2.toString(); - }; - - return isFinite; - }(function () { - return isFinite(this.sign) && isFinite(this.layer) && isFinite(this.mag); - }) - }, { - key: "eq", - value: function eq(value) { - var decimal = D(value); - return this.sign === decimal.sign && this.layer === decimal.layer && this.mag === decimal.mag; - } - }, { - key: "equals", - value: function equals(value) { - return this.eq(value); - } - }, { - key: "neq", - value: function neq(value) { - return !this.eq(value); - } - }, { - key: "notEquals", - value: function notEquals(value) { - return this.neq(value); - } - }, { - key: "lt", - value: function lt(value) { - return this.cmp(value) === -1; - } - }, { - key: "lte", - value: function lte(value) { - return !this.gt(value); - } - }, { - key: "gt", - value: function gt(value) { - return this.cmp(value) === 1; - } - }, { - key: "gte", - value: function gte(value) { - return !this.lt(value); - } - }, { - key: "max", - value: function max(value) { - var decimal = D(value); - return this.lt(decimal) ? decimal : this; - } - }, { - key: "min", - value: function min(value) { - var decimal = D(value); - return this.gt(decimal) ? decimal : this; - } - }, { - key: "maxabs", - value: function maxabs(value) { - var decimal = D(value); - return this.cmpabs(decimal) < 0 ? decimal : this; - } - }, { - key: "minabs", - value: function minabs(value) { - var decimal = D(value); - return this.cmpabs(decimal) > 0 ? decimal : this; - } - }, { - key: "clamp", - value: function clamp(min, max) { - return this.max(min).min(max); - } - }, { - key: "clampMin", - value: function clampMin(min) { - return this.max(min); - } - }, { - key: "clampMax", - value: function clampMax(max) { - return this.min(max); - } - }, { - key: "cmp_tolerance", - value: function cmp_tolerance(value, tolerance) { - var decimal = D(value); - return this.eq_tolerance(decimal, tolerance) ? 0 : this.cmp(decimal); - } - }, { - key: "compare_tolerance", - value: function compare_tolerance(value, tolerance) { - return this.cmp_tolerance(value, tolerance); - } - /** - * Tolerance is a relative tolerance, multiplied by the greater of the magnitudes of the two arguments. - * For example, if you put in 1e-9, then any number closer to the - * larger number than (larger number)*1e-9 will be considered equal. - */ - - }, { - key: "eq_tolerance", - value: function eq_tolerance(value, tolerance) { - var decimal = D(value); // https://stackoverflow.com/a/33024979 - - if (tolerance == null) { - tolerance = 1e-7; - } //Numbers that are too far away are never close. - - - if (this.sign !== decimal.sign) { - return false; - } + if (!Number.isFinite(this.layer)) { + return this; + } - if (Math.abs(this.layer - decimal.layer) > 1) { - return false; - } // return abs(a-b) <= tolerance * max(abs(a), abs(b)) + if (!Number.isFinite(decimal.layer)) { + return decimal; + } //Special case - if one of the numbers is 0, return 0. - var magA = this.mag; - var magB = decimal.mag; + if (this.sign === 0 || decimal.sign === 0) { + return FC_NN(0, 0, 0); + } //Special case - Multiplying a number by its own reciprocal yields +/- 1, no matter how large. - if (this.layer > decimal.layer) { - magB = f_maglog10(magB); - } - if (this.layer < decimal.layer) { - magA = f_maglog10(magA); - } + if (this.layer === decimal.layer && this.mag === -decimal.mag) { + return FC_NN(this.sign * decimal.sign, 0, 1); + } - return Math.abs(magA - magB) <= tolerance * Math.max(Math.abs(magA), Math.abs(magB)); - } - }, { - key: "equals_tolerance", - value: function equals_tolerance(value, tolerance) { - return this.eq_tolerance(value, tolerance); - } - }, { - key: "neq_tolerance", - value: function neq_tolerance(value, tolerance) { - return !this.eq_tolerance(value, tolerance); - } - }, { - key: "notEquals_tolerance", - value: function notEquals_tolerance(value, tolerance) { - return this.neq_tolerance(value, tolerance); - } - }, { - key: "lt_tolerance", - value: function lt_tolerance(value, tolerance) { - var decimal = D(value); - return !this.eq_tolerance(decimal, tolerance) && this.lt(decimal); - } - }, { - key: "lte_tolerance", - value: function lte_tolerance(value, tolerance) { - var decimal = D(value); - return this.eq_tolerance(decimal, tolerance) || this.lt(decimal); - } - }, { - key: "gt_tolerance", - value: function gt_tolerance(value, tolerance) { - var decimal = D(value); - return !this.eq_tolerance(decimal, tolerance) && this.gt(decimal); - } - }, { - key: "gte_tolerance", - value: function gte_tolerance(value, tolerance) { - var decimal = D(value); - return this.eq_tolerance(decimal, tolerance) || this.gt(decimal); - } - }, { - key: "pLog10", - value: function pLog10() { - if (this.lt(Decimal.dZero)) { - return Decimal.dZero; - } + var a; + var b; //Which number is bigger in terms of its multiplicative distance from 1? - return this.log10(); - } - }, { - key: "absLog10", - value: function absLog10() { - if (this.sign === 0) { - return Decimal.dNaN; - } else if (this.layer > 0) { - return FC(Math.sign(this.mag), this.layer - 1, Math.abs(this.mag)); - } else { - return FC(1, 0, Math.log10(this.mag)); - } - } - }, { - key: "log10", - value: function log10() { - if (this.sign <= 0) { - return Decimal.dNaN; - } else if (this.layer > 0) { - return FC(Math.sign(this.mag), this.layer - 1, Math.abs(this.mag)); - } else { - return FC(this.sign, 0, Math.log10(this.mag)); - } + if (this.layer > decimal.layer || this.layer == decimal.layer && Math.abs(this.mag) > Math.abs(decimal.mag)) { + a = this; + b = decimal; + } else { + a = decimal; + b = this; } - }, { - key: "log", - value: function log(base) { - base = D(base); - - if (this.sign <= 0) { - return Decimal.dNaN; - } - if (base.sign <= 0) { - return Decimal.dNaN; - } + if (a.layer === 0 && b.layer === 0) { + return Decimal.fromNumber(a.sign * b.sign * a.mag * b.mag); + } //Special case: If one of the numbers is layer 3 or higher or one of the numbers is 2+ layers bigger than the other, just take the bigger number. - if (base.sign === 1 && base.layer === 0 && base.mag === 1) { - return Decimal.dNaN; - } else if (this.layer === 0 && base.layer === 0) { - return FC(this.sign, 0, Math.log(this.mag) / Math.log(base.mag)); - } - return Decimal.div(this.log10(), base.log10()); - } - }, { - key: "log2", - value: function log2() { - if (this.sign <= 0) { - return Decimal.dNaN; - } else if (this.layer === 0) { - return FC(this.sign, 0, Math.log2(this.mag)); - } else if (this.layer === 1) { - return FC(Math.sign(this.mag), 0, Math.abs(this.mag) * 3.321928094887362); //log2(10) - } else if (this.layer === 2) { - return FC(Math.sign(this.mag), 1, Math.abs(this.mag) + 0.5213902276543247); //-log10(log10(2)) - } else { - return FC(Math.sign(this.mag), this.layer - 1, Math.abs(this.mag)); - } + if (a.layer >= 3 || a.layer - b.layer >= 2) { + return FC(a.sign * b.sign, a.layer, a.mag); } - }, { - key: "ln", - value: function ln() { - if (this.sign <= 0) { - return Decimal.dNaN; - } else if (this.layer === 0) { - return FC(this.sign, 0, Math.log(this.mag)); - } else if (this.layer === 1) { - return FC(Math.sign(this.mag), 0, Math.abs(this.mag) * 2.302585092994046); //ln(10) - } else if (this.layer === 2) { - return FC(Math.sign(this.mag), 1, Math.abs(this.mag) + 0.36221568869946325); //log10(log10(e)) - } else { - return FC(Math.sign(this.mag), this.layer - 1, Math.abs(this.mag)); - } - } - }, { - key: "logarithm", - value: function logarithm(base) { - return this.log(base); + + if (a.layer === 1 && b.layer === 0) { + return FC(a.sign * b.sign, 1, a.mag + Math.log10(b.mag)); } - }, { - key: "pow", - value: function pow(value) { - var decimal = D(value); - var a = this; - var b = decimal; //special case: if a is 0, then return 0 (UNLESS b is 0, then return 1) - if (a.sign === 0) { - return b.eq(0) ? FC_NN(1, 0, 1) : a; - } //special case: if a is 1, then return 1 + if (a.layer === 1 && b.layer === 1) { + return FC(a.sign * b.sign, 1, a.mag + b.mag); + } + if (a.layer === 2 && b.layer === 1) { + var newmag = FC(Math.sign(a.mag), a.layer - 1, Math.abs(a.mag)).add(FC(Math.sign(b.mag), b.layer - 1, Math.abs(b.mag))); + return FC(a.sign * b.sign, newmag.layer + 1, newmag.sign * newmag.mag); + } - if (a.sign === 1 && a.layer === 0 && a.mag === 1) { - return a; - } //special case: if b is 0, then return 1 + if (a.layer === 2 && b.layer === 2) { + var _newmag = FC(Math.sign(a.mag), a.layer - 1, Math.abs(a.mag)).add(FC(Math.sign(b.mag), b.layer - 1, Math.abs(b.mag))); + return FC(a.sign * b.sign, _newmag.layer + 1, _newmag.sign * _newmag.mag); + } - if (b.sign === 0) { - return FC_NN(1, 0, 1); - } //special case: if b is 1, then return a + throw Error("Bad arguments to mul: " + this + ", " + value); + } + }, { + key: "multiply", + value: function multiply(value) { + return this.mul(value); + } + }, { + key: "times", + value: function times(value) { + return this.mul(value); + } + }, { + key: "div", + value: function div(value) { + var decimal = D(value); + return this.mul(decimal.recip()); + } + }, { + key: "divide", + value: function divide(value) { + return this.div(value); + } + }, { + key: "divideBy", + value: function divideBy(value) { + return this.div(value); + } + }, { + key: "dividedBy", + value: function dividedBy(value) { + return this.div(value); + } + }, { + key: "recip", + value: function recip() { + if (this.mag === 0) { + return Decimal.dNaN; + } else if (this.layer === 0) { + return FC(this.sign, 0, 1 / this.mag); + } else { + return FC(this.sign, this.layer, -this.mag); + } + } + }, { + key: "reciprocal", + value: function reciprocal() { + return this.recip(); + } + }, { + key: "reciprocate", + value: function reciprocate() { + return this.recip(); + } + /** + * -1 for less than value, 0 for equals value, 1 for greater than value + */ + }, { + key: "cmp", + value: function cmp(value) { + var decimal = D(value); - if (b.sign === 1 && b.layer === 0 && b.mag === 1) { - return a; - } + if (this.sign > decimal.sign) { + return 1; + } - var result = a.absLog10().mul(b).pow10(); + if (this.sign < decimal.sign) { + return -1; + } - if (this.sign === -1) { - if (Math.abs(b.toNumber() % 2) % 2 === 1) { - return result.neg(); - } else if (Math.abs(b.toNumber() % 2) % 2 === 0) { - return result; - } + return this.sign * this.cmpabs(value); + } + }, { + key: "cmpabs", + value: function cmpabs(value) { + var decimal = D(value); + var layera = this.mag > 0 ? this.layer : -this.layer; + var layerb = decimal.mag > 0 ? decimal.layer : -decimal.layer; - return Decimal.dNaN; - } + if (layera > layerb) { + return 1; + } - return result; + if (layera < layerb) { + return -1; } - }, { - key: "pow10", - value: function pow10() { - /* - There are four cases we need to consider: - 1) positive sign, positive mag (e15, ee15): +1 layer (e.g. 10^15 becomes e15, 10^e15 becomes ee15) - 2) negative sign, positive mag (-e15, -ee15): +1 layer but sign and mag sign are flipped (e.g. 10^-15 becomes e-15, 10^-e15 becomes ee-15) - 3) positive sign, negative mag (e-15, ee-15): layer 0 case would have been handled in the Math.pow check, so just return 1 - 4) negative sign, negative mag (-e-15, -ee-15): layer 0 case would have been handled in the Math.pow check, so just return 1 - */ - if (!Number.isFinite(this.layer) || !Number.isFinite(this.mag)) { - return Decimal.dNaN; - } - var a = this; //handle layer 0 case - if no precision is lost just use Math.pow, else promote one layer + if (this.mag > decimal.mag) { + return 1; + } - if (a.layer === 0) { - var newmag = Math.pow(10, a.sign * a.mag); - - if (Number.isFinite(newmag) && Math.abs(newmag) >= 0.1) { - return FC(1, 0, newmag); - } else { - if (a.sign === 0) { - return Decimal.dOne; - } else { - a = FC_NN(a.sign, a.layer + 1, Math.log10(a.mag)); - } - } - } //handle all 4 layer 1+ cases individually + if (this.mag < decimal.mag) { + return -1; + } + return 0; + } + }, { + key: "compare", + value: function compare(value) { + return this.cmp(value); + } + }, { + key: "isNan", + value: function isNan() { + return isNaN(this.sign) || isNaN(this.layer) || isNaN(this.mag); + } + }, { + key: "isFinite", + value: function (_isFinite2) { + function isFinite() { + return _isFinite2.apply(this, arguments); + } + + isFinite.toString = function () { + return _isFinite2.toString(); + }; + + return isFinite; + }(function () { + return isFinite(this.sign) && isFinite(this.layer) && isFinite(this.mag); + }) + }, { + key: "eq", + value: function eq(value) { + var decimal = D(value); + return this.sign === decimal.sign && this.layer === decimal.layer && this.mag === decimal.mag; + } + }, { + key: "equals", + value: function equals(value) { + return this.eq(value); + } + }, { + key: "neq", + value: function neq(value) { + return !this.eq(value); + } + }, { + key: "notEquals", + value: function notEquals(value) { + return this.neq(value); + } + }, { + key: "lt", + value: function lt(value) { + return this.cmp(value) === -1; + } + }, { + key: "lte", + value: function lte(value) { + return !this.gt(value); + } + }, { + key: "gt", + value: function gt(value) { + return this.cmp(value) === 1; + } + }, { + key: "gte", + value: function gte(value) { + return !this.lt(value); + } + }, { + key: "max", + value: function max(value) { + var decimal = D(value); + return this.lt(decimal) ? decimal : this; + } + }, { + key: "min", + value: function min(value) { + var decimal = D(value); + return this.gt(decimal) ? decimal : this; + } + }, { + key: "maxabs", + value: function maxabs(value) { + var decimal = D(value); + return this.cmpabs(decimal) < 0 ? decimal : this; + } + }, { + key: "minabs", + value: function minabs(value) { + var decimal = D(value); + return this.cmpabs(decimal) > 0 ? decimal : this; + } + }, { + key: "clamp", + value: function clamp(min, max) { + return this.max(min).min(max); + } + }, { + key: "clampMin", + value: function clampMin(min) { + return this.max(min); + } + }, { + key: "clampMax", + value: function clampMax(max) { + return this.min(max); + } + }, { + key: "cmp_tolerance", + value: function cmp_tolerance(value, tolerance) { + var decimal = D(value); + return this.eq_tolerance(decimal, tolerance) ? 0 : this.cmp(decimal); + } + }, { + key: "compare_tolerance", + value: function compare_tolerance(value, tolerance) { + return this.cmp_tolerance(value, tolerance); + } + /** + * Tolerance is a relative tolerance, multiplied by the greater of the magnitudes of the two arguments. + * For example, if you put in 1e-9, then any number closer to the + * larger number than (larger number)*1e-9 will be considered equal. + */ - if (a.sign > 0 && a.mag >= 0) { - return FC(a.sign, a.layer + 1, a.mag); - } + }, { + key: "eq_tolerance", + value: function eq_tolerance(value, tolerance) { + var decimal = D(value); // https://stackoverflow.com/a/33024979 - if (a.sign < 0 && a.mag >= 0) { - return FC(-a.sign, a.layer + 1, -a.mag); - } //both the negative mag cases are identical: one +/- rounding error + if (tolerance == null) { + tolerance = 1e-7; + } //Numbers that are too far away are never close. - return Decimal.dOne; + if (this.sign !== decimal.sign) { + return false; } - }, { - key: "pow_base", - value: function pow_base(value) { - return D(value).pow(this); - } - }, { - key: "root", - value: function root(value) { - var decimal = D(value); - return this.pow(decimal.recip()); - } - }, { - key: "factorial", - value: function factorial() { - if (this.mag < 0) { - return this.add(1).gamma(); - } else if (this.layer === 0) { - return this.add(1).gamma(); - } else if (this.layer === 1) { - return Decimal.exp(Decimal.mul(this, Decimal.ln(this).sub(1))); - } else { - return Decimal.exp(this); - } - } //from HyperCalc source code - - }, { - key: "gamma", - value: function gamma() { - if (this.mag < 0) { - return this.recip(); - } else if (this.layer === 0) { - if (this.lt(FC_NN(1, 0, 24))) { - return Decimal.fromNumber(f_gamma(this.sign * this.mag)); - } - - var t = this.mag - 1; - var l = 0.9189385332046727; //0.5*Math.log(2*Math.PI) - l = l + (t + 0.5) * Math.log(t); - l = l - t; - var n2 = t * t; - var np = t; - var lm = 12 * np; - var adj = 1 / lm; - var l2 = l + adj; + if (Math.abs(this.layer - decimal.layer) > 1) { + return false; + } // return abs(a-b) <= tolerance * max(abs(a), abs(b)) - if (l2 === l) { - return Decimal.exp(l); - } - l = l2; - np = np * n2; - lm = 360 * np; - adj = 1 / lm; - l2 = l - adj; + var magA = this.mag; + var magB = decimal.mag; - if (l2 === l) { - return Decimal.exp(l); - } + if (this.layer > decimal.layer) { + magB = f_maglog10(magB); + } - l = l2; - np = np * n2; - lm = 1260 * np; - var lt = 1 / lm; - l = l + lt; - np = np * n2; - lm = 1680 * np; - lt = 1 / lm; - l = l - lt; - return Decimal.exp(l); - } else if (this.layer === 1) { - return Decimal.exp(Decimal.mul(this, Decimal.ln(this).sub(1))); - } else { - return Decimal.exp(this); - } + if (this.layer < decimal.layer) { + magA = f_maglog10(magA); } - }, { - key: "lngamma", - value: function lngamma() { - return this.gamma().ln(); + + return Math.abs(magA - magB) <= tolerance * Math.max(Math.abs(magA), Math.abs(magB)); + } + }, { + key: "equals_tolerance", + value: function equals_tolerance(value, tolerance) { + return this.eq_tolerance(value, tolerance); + } + }, { + key: "neq_tolerance", + value: function neq_tolerance(value, tolerance) { + return !this.eq_tolerance(value, tolerance); + } + }, { + key: "notEquals_tolerance", + value: function notEquals_tolerance(value, tolerance) { + return this.neq_tolerance(value, tolerance); + } + }, { + key: "lt_tolerance", + value: function lt_tolerance(value, tolerance) { + var decimal = D(value); + return !this.eq_tolerance(decimal, tolerance) && this.lt(decimal); + } + }, { + key: "lte_tolerance", + value: function lte_tolerance(value, tolerance) { + var decimal = D(value); + return this.eq_tolerance(decimal, tolerance) || this.lt(decimal); + } + }, { + key: "gt_tolerance", + value: function gt_tolerance(value, tolerance) { + var decimal = D(value); + return !this.eq_tolerance(decimal, tolerance) && this.gt(decimal); + } + }, { + key: "gte_tolerance", + value: function gte_tolerance(value, tolerance) { + var decimal = D(value); + return this.eq_tolerance(decimal, tolerance) || this.gt(decimal); + } + }, { + key: "pLog10", + value: function pLog10() { + if (this.lt(Decimal.dZero)) { + return Decimal.dZero; } - }, { - key: "exp", - value: function exp() { - if (this.mag < 0) { - return Decimal.dOne; - } - if (this.layer === 0 && this.mag <= 709.7) { - return Decimal.fromNumber(Math.exp(this.sign * this.mag)); - } else if (this.layer === 0) { - return FC(1, 1, this.sign * Math.log10(Math.E) * this.mag); - } else if (this.layer === 1) { - return FC(1, 2, this.sign * (Math.log10(0.4342944819032518) + this.mag)); - } else { - return FC(1, this.layer + 1, this.sign * this.mag); - } + return this.log10(); + } + }, { + key: "absLog10", + value: function absLog10() { + if (this.sign === 0) { + return Decimal.dNaN; + } else if (this.layer > 0) { + return FC(Math.sign(this.mag), this.layer - 1, Math.abs(this.mag)); + } else { + return FC(1, 0, Math.log10(this.mag)); } - }, { - key: "sqr", - value: function sqr() { - return this.pow(2); + } + }, { + key: "log10", + value: function log10() { + if (this.sign <= 0) { + return Decimal.dNaN; + } else if (this.layer > 0) { + return FC(Math.sign(this.mag), this.layer - 1, Math.abs(this.mag)); + } else { + return FC(this.sign, 0, Math.log10(this.mag)); } - }, { - key: "sqrt", - value: function sqrt() { - if (this.layer === 0) { - return Decimal.fromNumber(Math.sqrt(this.sign * this.mag)); - } else if (this.layer === 1) { - return FC(1, 2, Math.log10(this.mag) - 0.3010299956639812); - } else { - var result = Decimal.div(FC_NN(this.sign, this.layer - 1, this.mag), FC_NN(1, 0, 2)); - result.layer += 1; - result.normalize(); - return result; - } + } + }, { + key: "log", + value: function log(base) { + base = D(base); + + if (this.sign <= 0) { + return Decimal.dNaN; } - }, { - key: "cube", - value: function cube() { - return this.pow(3); + + if (base.sign <= 0) { + return Decimal.dNaN; } - }, { - key: "cbrt", - value: function cbrt() { - return this.pow(1 / 3); - } //Tetration/tetrate: The result of exponentiating 'this' to 'this' 'height' times in a row. https://en.wikipedia.org/wiki/Tetration - //If payload != 1, then this is 'iterated exponentiation', the result of exping (payload) to base (this) (height) times. https://andydude.github.io/tetration/archives/tetration2/ident.html - //Works with negative and positive real heights. - }, { - key: "tetrate", - value: function tetrate() { - var height = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2; - var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : FC_NN(1, 0, 1); + if (base.sign === 1 && base.layer === 0 && base.mag === 1) { + return Decimal.dNaN; + } else if (this.layer === 0 && base.layer === 0) { + return FC(this.sign, 0, Math.log(this.mag) / Math.log(base.mag)); + } - //x^^1 == x - if (height === 1) { - return Decimal.pow(this, payload); - } //x^^0 == 1 + return Decimal.div(this.log10(), base.log10()); + } + }, { + key: "log2", + value: function log2() { + if (this.sign <= 0) { + return Decimal.dNaN; + } else if (this.layer === 0) { + return FC(this.sign, 0, Math.log2(this.mag)); + } else if (this.layer === 1) { + return FC(Math.sign(this.mag), 0, Math.abs(this.mag) * 3.321928094887362); //log2(10) + } else if (this.layer === 2) { + return FC(Math.sign(this.mag), 1, Math.abs(this.mag) + 0.5213902276543247); //-log10(log10(2)) + } else { + return FC(Math.sign(this.mag), this.layer - 1, Math.abs(this.mag)); + } + } + }, { + key: "ln", + value: function ln() { + if (this.sign <= 0) { + return Decimal.dNaN; + } else if (this.layer === 0) { + return FC(this.sign, 0, Math.log(this.mag)); + } else if (this.layer === 1) { + return FC(Math.sign(this.mag), 0, Math.abs(this.mag) * 2.302585092994046); //ln(10) + } else if (this.layer === 2) { + return FC(Math.sign(this.mag), 1, Math.abs(this.mag) + 0.36221568869946325); //log10(log10(e)) + } else { + return FC(Math.sign(this.mag), this.layer - 1, Math.abs(this.mag)); + } + } + }, { + key: "logarithm", + value: function logarithm(base) { + return this.log(base); + } + }, { + key: "pow", + value: function pow(value) { + var decimal = D(value); + var a = this; + var b = decimal; //special case: if a is 0, then return 0 (UNLESS b is 0, then return 1) + if (a.sign === 0) { + return b.eq(0) ? FC_NN(1, 0, 1) : a; + } //special case: if a is 1, then return 1 - if (height === 0) { - return new Decimal(payload); - } //1^^x == 1 + if (a.sign === 1 && a.layer === 0 && a.mag === 1) { + return a; + } //special case: if b is 0, then return 1 - if (this.eq(Decimal.dOne)) { - return Decimal.dOne; - } //-1^^x == -1 + if (b.sign === 0) { + return FC_NN(1, 0, 1); + } //special case: if b is 1, then return a - if (this.eq(-1)) { - return Decimal.pow(this, payload); - } - if (height === Number.POSITIVE_INFINITY) { - var this_num = this.toNumber(); //within the convergence range? + if (b.sign === 1 && b.layer === 0 && b.mag === 1) { + return a; + } - if (this_num <= 1.44466786100976613366 && this_num >= 0.06598803584531253708) { - //hotfix for the very edge of the number range not being handled properly - if (this_num > 1.444667861009099) { - return Decimal.fromNumber(Math.E); - } //Formula for infinite height power tower. + var result = a.absLog10().mul(b).pow10(); + if (this.sign === -1) { + if (Math.abs(b.toNumber() % 2) % 2 === 1) { + return result.neg(); + } else if (Math.abs(b.toNumber() % 2) % 2 === 0) { + return result; + } - var negln = Decimal.ln(this).neg(); - return negln.lambertw().div(negln); - } else if (this_num > 1.44466786100976613366) { - //explodes to infinity - // TODO: replace this with Decimal.dInf - return Decimal.fromNumber(Number.POSITIVE_INFINITY); + return Decimal.dNaN; + } + + return result; + } + }, { + key: "pow10", + value: function pow10() { + /* + There are four cases we need to consider: + 1) positive sign, positive mag (e15, ee15): +1 layer (e.g. 10^15 becomes e15, 10^e15 becomes ee15) + 2) negative sign, positive mag (-e15, -ee15): +1 layer but sign and mag sign are flipped (e.g. 10^-15 becomes e-15, 10^-e15 becomes ee-15) + 3) positive sign, negative mag (e-15, ee-15): layer 0 case would have been handled in the Math.pow check, so just return 1 + 4) negative sign, negative mag (-e-15, -ee-15): layer 0 case would have been handled in the Math.pow check, so just return 1 + */ + if (!Number.isFinite(this.layer) || !Number.isFinite(this.mag)) { + return Decimal.dNaN; + } + + var a = this; //handle layer 0 case - if no precision is lost just use Math.pow, else promote one layer + + if (a.layer === 0) { + var newmag = Math.pow(10, a.sign * a.mag); + + if (Number.isFinite(newmag) && Math.abs(newmag) >= 0.1) { + return FC(1, 0, newmag); + } else { + if (a.sign === 0) { + return Decimal.dOne; } else { - //0.06598803584531253708 > this_num >= 0: never converges - //this_num < 0: quickly becomes a complex number - return Decimal.dNaN; + a = FC_NN(a.sign, a.layer + 1, Math.log10(a.mag)); } - } //0^^x oscillates if we define 0^0 == 1 (which in javascript land we do), since then 0^^1 is 0, 0^^2 is 1, 0^^3 is 0, etc. payload is ignored - //using the linear approximation for height (TODO: don't know a better way to calculate it ATM, but it wouldn't surprise me if it's just NaN) + } + } //handle all 4 layer 1+ cases individually - if (this.eq(Decimal.dZero)) { - var result = Math.abs((height + 1) % 2); + if (a.sign > 0 && a.mag >= 0) { + return FC(a.sign, a.layer + 1, a.mag); + } - if (result > 1) { - result = 2 - result; - } + if (a.sign < 0 && a.mag >= 0) { + return FC(-a.sign, a.layer + 1, -a.mag); + } //both the negative mag cases are identical: one +/- rounding error - return Decimal.fromNumber(result); + + return Decimal.dOne; + } + }, { + key: "pow_base", + value: function pow_base(value) { + return D(value).pow(this); + } + }, { + key: "root", + value: function root(value) { + var decimal = D(value); + return this.pow(decimal.recip()); + } + }, { + key: "factorial", + value: function factorial() { + if (this.mag < 0) { + return this.add(1).gamma(); + } else if (this.layer === 0) { + return this.add(1).gamma(); + } else if (this.layer === 1) { + return Decimal.exp(Decimal.mul(this, Decimal.ln(this).sub(1))); + } else { + return Decimal.exp(this); + } + } //from HyperCalc source code + + }, { + key: "gamma", + value: function gamma() { + if (this.mag < 0) { + return this.recip(); + } else if (this.layer === 0) { + if (this.lt(FC_NN(1, 0, 24))) { + return Decimal.fromNumber(f_gamma(this.sign * this.mag)); } - if (height < 0) { - return Decimal.iteratedlog(payload, this, -height); + var t = this.mag - 1; + var l = 0.9189385332046727; //0.5*Math.log(2*Math.PI) + + l = l + (t + 0.5) * Math.log(t); + l = l - t; + var n2 = t * t; + var np = t; + var lm = 12 * np; + var adj = 1 / lm; + var l2 = l + adj; + + if (l2 === l) { + return Decimal.exp(l); } - payload = D(payload); - var oldheight = height; - height = Math.trunc(height); - var fracheight = oldheight - height; + l = l2; + np = np * n2; + lm = 360 * np; + adj = 1 / lm; + l2 = l - adj; - if (this.gt(Decimal.dZero) && this.lte(1.44466786100976613366)) { - //similar to 0^^n, flip-flops between two values, converging slowly (or if it's below 0.06598803584531253708, never. so once again, the fractional part at the end will be a linear approximation (TODO: again pending knowledge of how to approximate better, although tbh I think it should in reality just be NaN) - height = Math.min(10000, height); + if (l2 === l) { + return Decimal.exp(l); + } - for (var i = 0; i < height; ++i) { - var old_payload = payload; - payload = this.pow(payload); //stop early if we converge + l = l2; + np = np * n2; + lm = 1260 * np; + var lt = 1 / lm; + l = l + lt; + np = np * n2; + lm = 1680 * np; + lt = 1 / lm; + l = l - lt; + return Decimal.exp(l); + } else if (this.layer === 1) { + return Decimal.exp(Decimal.mul(this, Decimal.ln(this).sub(1))); + } else { + return Decimal.exp(this); + } + } + }, { + key: "lngamma", + value: function lngamma() { + return this.gamma().ln(); + } + }, { + key: "exp", + value: function exp() { + if (this.mag < 0) { + return Decimal.dOne; + } - if (old_payload.eq(payload)) { - return payload; - } - } + if (this.layer === 0 && this.mag <= 709.7) { + return Decimal.fromNumber(Math.exp(this.sign * this.mag)); + } else if (this.layer === 0) { + return FC(1, 1, this.sign * Math.log10(Math.E) * this.mag); + } else if (this.layer === 1) { + return FC(1, 2, this.sign * (Math.log10(0.4342944819032518) + this.mag)); + } else { + return FC(1, this.layer + 1, this.sign * this.mag); + } + } + }, { + key: "sqr", + value: function sqr() { + return this.pow(2); + } + }, { + key: "sqrt", + value: function sqrt() { + if (this.layer === 0) { + return Decimal.fromNumber(Math.sqrt(this.sign * this.mag)); + } else if (this.layer === 1) { + return FC(1, 2, Math.log10(this.mag) - 0.3010299956639812); + } else { + var result = Decimal.div(FC_NN(this.sign, this.layer - 1, this.mag), FC_NN(1, 0, 2)); + result.layer += 1; + result.normalize(); + return result; + } + } + }, { + key: "cube", + value: function cube() { + return this.pow(3); + } + }, { + key: "cbrt", + value: function cbrt() { + return this.pow(1 / 3); + } //Tetration/tetrate: The result of exponentiating 'this' to 'this' 'height' times in a row. https://en.wikipedia.org/wiki/Tetration + //If payload != 1, then this is 'iterated exponentiation', the result of exping (payload) to base (this) (height) times. https://andydude.github.io/tetration/archives/tetration2/ident.html + //Works with negative and positive real heights. - if (fracheight != 0) { - var next_payload = this.pow(payload); - return payload.mul(1 - fracheight).add(next_payload.mul(fracheight)); - } + }, { + key: "tetrate", + value: function tetrate() { + var height = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2; + var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : FC_NN(1, 0, 1); - return payload; - } //TODO: base < 0, but it's hard for me to reason about (probably all non-integer heights are NaN automatically?) + //x^^1 == x + if (height === 1) { + return Decimal.pow(this, payload); + } //x^^0 == 1 - if (fracheight !== 0) { - if (payload.eq(Decimal.dOne)) { - //TODO: for bases above 10, revert to old linear approximation until I can think of something better - if (this.gt(10)) { - payload = this.pow(fracheight); - } else { - payload = Decimal.fromNumber(Decimal.tetrate_critical(this.toNumber(), fracheight)); //TODO: until the critical section grid can handle numbers below 2, scale them to the base - //TODO: maybe once the critical section grid has very large bases, this math can be appropriate for them too? I'll think about it + if (height === 0) { + return new Decimal(payload); + } //1^^x == 1 - if (this.lt(2)) { - payload = payload.sub(1).mul(this.minus(1)).plus(1); - } - } - } else { - if (this.eq(10)) { - payload = payload.layeradd10(fracheight); - } else { - payload = payload.layeradd(fracheight, this); - } - } - } - for (var _i = 0; _i < height; ++_i) { - payload = this.pow(payload); //bail if we're NaN + if (this.eq(Decimal.dOne)) { + return Decimal.dOne; + } //-1^^x == -1 - if (!isFinite(payload.layer) || !isFinite(payload.mag)) { - return payload.normalize(); - } //shortcut + if (this.eq(-1)) { + return Decimal.pow(this, payload); + } - if (payload.layer - this.layer > 3) { - return FC_NN(payload.sign, payload.layer + (height - _i - 1), payload.mag); - } //give up after 10000 iterations if nothing is happening + if (height === Number.POSITIVE_INFINITY) { + var this_num = this.toNumber(); //within the convergence range? + if (this_num <= 1.44466786100976613366 && this_num >= 0.06598803584531253708) { + //hotfix for the very edge of the number range not being handled properly + if (this_num > 1.444667861009099) { + return Decimal.fromNumber(Math.E); + } //Formula for infinite height power tower. - if (_i > 10000) { - return payload; - } - } - return payload; - } //iteratedexp/iterated exponentiation: - all cases handled in tetrate, so just call it - - }, { - key: "iteratedexp", - value: function iteratedexp() { - var height = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2; - var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : FC_NN(1, 0, 1); - return this.tetrate(height, payload); - } //iterated log/repeated log: The result of applying log(base) 'times' times in a row. Approximately equal to subtracting (times) from the number's slog representation. Equivalent to tetrating to a negative height. - //Works with negative and positive real heights. - - }, { - key: "iteratedlog", - value: function iteratedlog() { - var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; - var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; - - if (times < 0) { - return Decimal.tetrate(base, -times, this); + var negln = Decimal.ln(this).neg(); + return negln.lambertw().div(negln); + } else if (this_num > 1.44466786100976613366) { + //explodes to infinity + // TODO: replace this with Decimal.dInf + return Decimal.fromNumber(Number.POSITIVE_INFINITY); + } else { + //0.06598803584531253708 > this_num >= 0: never converges + //this_num < 0: quickly becomes a complex number + return Decimal.dNaN; } + } //0^^x oscillates if we define 0^0 == 1 (which in javascript land we do), since then 0^^1 is 0, 0^^2 is 1, 0^^3 is 0, etc. payload is ignored + //using the linear approximation for height (TODO: don't know a better way to calculate it ATM, but it wouldn't surprise me if it's just NaN) - base = D(base); - var result = Decimal.fromDecimal(this); - var fulltimes = times; - times = Math.trunc(times); - var fraction = fulltimes - times; - if (result.layer - base.layer > 3) { - var layerloss = Math.min(times, result.layer - base.layer - 3); - times -= layerloss; - result.layer -= layerloss; + if (this.eq(Decimal.dZero)) { + var result = Math.abs((height + 1) % 2); + + if (result > 1) { + result = 2 - result; } - for (var i = 0; i < times; ++i) { - result = result.log(base); //bail if we're NaN + return Decimal.fromNumber(result); + } - if (!isFinite(result.layer) || !isFinite(result.mag)) { - return result.normalize(); - } //give up after 10000 iterations if nothing is happening + if (height < 0) { + return Decimal.iteratedlog(payload, this, -height); + } + payload = D(payload); + var oldheight = height; + height = Math.trunc(height); + var fracheight = oldheight - height; - if (i > 10000) { - return result; - } - } //handle fractional part + if (this.gt(Decimal.dZero) && this.lte(1.44466786100976613366)) { + //similar to 0^^n, flip-flops between two values, converging slowly (or if it's below 0.06598803584531253708, never. so once again, the fractional part at the end will be a linear approximation (TODO: again pending knowledge of how to approximate better, although tbh I think it should in reality just be NaN) + height = Math.min(10000, height); + for (var i = 0; i < height; ++i) { + var old_payload = payload; + payload = this.pow(payload); //stop early if we converge - if (fraction > 0 && fraction < 1) { - if (base.eq(10)) { - result = result.layeradd10(-fraction); - } else { - result = result.layeradd(-fraction, base); + if (old_payload.eq(payload)) { + return payload; } } - return result; - } //Super-logarithm, one of tetration's inverses, tells you what size power tower you'd have to tetrate base to to get number. By definition, will never be higher than 1.8e308 in break_eternity.js, since a power tower 1.8e308 numbers tall is the largest representable number. - // https://en.wikipedia.org/wiki/Super-logarithm - // NEW: Accept a number of iterations, and use binary search to, after making an initial guess, hone in on the true value, assuming tetration as the ground truth. - - }, { - key: "slog", - value: function slog() { - var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; - var iterations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100; - var step_size = 0.001; - var has_changed_directions_once = false; - var previously_rose = false; - var result = this.slog_internal(base).toNumber(); - - for (var i = 1; i < iterations; ++i) { - var new_decimal = new Decimal(base).tetrate(result); - var currently_rose = new_decimal.gt(this); - - if (i > 1) { - if (previously_rose != currently_rose) { - has_changed_directions_once = true; - } - } + if (fracheight != 0) { + var next_payload = this.pow(payload); + return payload.mul(1 - fracheight).add(next_payload.mul(fracheight)); + } - previously_rose = currently_rose; + return payload; + } //TODO: base < 0, but it's hard for me to reason about (probably all non-integer heights are NaN automatically?) - if (has_changed_directions_once) { - step_size /= 2; - } else { - step_size *= 2; - } - step_size = Math.abs(step_size) * (currently_rose ? -1 : 1); - result += step_size; + if (fracheight !== 0) { + if (payload.eq(Decimal.dOne)) { + //TODO: for bases above 10, revert to old linear approximation until I can think of something better + if (this.gt(10)) { + payload = this.pow(fracheight); + } else { + payload = Decimal.fromNumber(Decimal.tetrate_critical(this.toNumber(), fracheight)); //TODO: until the critical section grid can handle numbers below 2, scale them to the base + //TODO: maybe once the critical section grid has very large bases, this math can be appropriate for them too? I'll think about it - if (step_size === 0) { - break; + if (this.lt(2)) { + payload = payload.sub(1).mul(this.minus(1)).plus(1); + } + } + } else { + if (this.eq(10)) { + payload = payload.layeradd10(fracheight); + } else { + payload = payload.layeradd(fracheight, this); } } + } - return Decimal.fromNumber(result); + for (var _i = 0; _i < height; ++_i) { + payload = this.pow(payload); //bail if we're NaN + + if (!isFinite(payload.layer) || !isFinite(payload.mag)) { + return payload.normalize(); + } //shortcut + + + if (payload.layer - this.layer > 3) { + return FC_NN(payload.sign, payload.layer + (height - _i - 1), payload.mag); + } //give up after 10000 iterations if nothing is happening + + + if (_i > 10000) { + return payload; + } } - }, { - key: "slog_internal", - value: function slog_internal() { - var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; - base = D(base); //special cases: - //slog base 0 or lower is NaN - if (base.lte(Decimal.dZero)) { - return Decimal.dNaN; - } //slog base 1 is NaN + return payload; + } //iteratedexp/iterated exponentiation: - all cases handled in tetrate, so just call it + }, { + key: "iteratedexp", + value: function iteratedexp() { + var height = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2; + var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : FC_NN(1, 0, 1); + return this.tetrate(height, payload); + } //iterated log/repeated log: The result of applying log(base) 'times' times in a row. Approximately equal to subtracting (times) from the number's slog representation. Equivalent to tetrating to a negative height. + //Works with negative and positive real heights. - if (base.eq(Decimal.dOne)) { - return Decimal.dNaN; - } //need to handle these small, wobbling bases specially + }, { + key: "iteratedlog", + value: function iteratedlog() { + var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; + var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; + if (times < 0) { + return Decimal.tetrate(base, -times, this); + } - if (base.lt(Decimal.dOne)) { - if (this.eq(Decimal.dOne)) { - return Decimal.dZero; - } + base = D(base); + var result = Decimal.fromDecimal(this); + var fulltimes = times; + times = Math.trunc(times); + var fraction = fulltimes - times; - if (this.eq(Decimal.dZero)) { - return Decimal.dNegOne; - } //0 < this < 1: ambiguous (happens multiple times) - //this < 0: impossible (as far as I can tell) - //this > 1: partially complex (http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html base 0.25 for proof) + if (result.layer - base.layer > 3) { + var layerloss = Math.min(times, result.layer - base.layer - 3); + times -= layerloss; + result.layer -= layerloss; + } + for (var i = 0; i < times; ++i) { + result = result.log(base); //bail if we're NaN - return Decimal.dNaN; - } //slog_n(0) is -1 + if (!isFinite(result.layer) || !isFinite(result.mag)) { + return result.normalize(); + } //give up after 10000 iterations if nothing is happening - if (this.mag < 0 || this.eq(Decimal.dZero)) { - return Decimal.dNegOne; + if (i > 10000) { + return result; } + } //handle fractional part - var result = 0; - var copy = Decimal.fromDecimal(this); - if (copy.layer - base.layer > 3) { - var layerloss = copy.layer - base.layer - 3; - result += layerloss; - copy.layer -= layerloss; + if (fraction > 0 && fraction < 1) { + if (base.eq(10)) { + result = result.layeradd10(-fraction); + } else { + result = result.layeradd(-fraction, base); } + } - for (var i = 0; i < 100; ++i) { - if (copy.lt(Decimal.dZero)) { - copy = Decimal.pow(base, copy); - result -= 1; - } else if (copy.lte(Decimal.dOne)) { - return Decimal.fromNumber(result + Decimal.slog_critical(base.toNumber(), copy.toNumber())); - } else { - result += 1; - copy = Decimal.log(copy, base); + return result; + } //Super-logarithm, one of tetration's inverses, tells you what size power tower you'd have to tetrate base to to get number. By definition, will never be higher than 1.8e308 in break_eternity.js, since a power tower 1.8e308 numbers tall is the largest representable number. + // https://en.wikipedia.org/wiki/Super-logarithm + // NEW: Accept a number of iterations, and use binary search to, after making an initial guess, hone in on the true value, assuming tetration as the ground truth. + + }, { + key: "slog", + value: function slog() { + var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; + var iterations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100; + var step_size = 0.001; + var has_changed_directions_once = false; + var previously_rose = false; + var result = this.slog_internal(base).toNumber(); + + for (var i = 1; i < iterations; ++i) { + var new_decimal = new Decimal(base).tetrate(result); + var currently_rose = new_decimal.gt(this); + + if (i > 1) { + if (previously_rose != currently_rose) { + has_changed_directions_once = true; } } - return Decimal.fromNumber(result); - } //background info and tables of values for critical functions taken here: https://github.com/Patashu/break_eternity.js/issues/22 - - }, { - key: "layeradd10", - value: //Function for adding/removing layers from a Decimal, even fractional layers (e.g. its slog10 representation). - //Moved this over to use the same critical section as tetrate/slog. - function layeradd10(diff) { - diff = Decimal.fromValue_noAlloc(diff).toNumber(); - var result = Decimal.fromDecimal(this); - - if (diff >= 1) { - //bug fix: if result is very smol (mag < 0, layer > 0) turn it into 0 first - if (result.mag < 0 && result.layer > 0) { - result.sign = 0; - result.mag = 0; - result.layer = 0; - } else if (result.sign === -1 && result.layer == 0) { - //bug fix - for stuff like -3.layeradd10(1) we need to move the sign to the mag - result.sign = 1; - result.mag = -result.mag; - } + previously_rose = currently_rose; - var layeradd = Math.trunc(diff); - diff -= layeradd; - result.layer += layeradd; + if (has_changed_directions_once) { + step_size /= 2; + } else { + step_size *= 2; } - if (diff <= -1) { - var _layeradd = Math.trunc(diff); + step_size = Math.abs(step_size) * (currently_rose ? -1 : 1); + result += step_size; - diff -= _layeradd; - result.layer += _layeradd; + if (step_size === 0) { + break; + } + } - if (result.layer < 0) { - for (var i = 0; i < 100; ++i) { - result.layer++; - result.mag = Math.log10(result.mag); + return Decimal.fromNumber(result); + } + }, { + key: "slog_internal", + value: function slog_internal() { + var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10; + base = D(base); //special cases: + //slog base 0 or lower is NaN - if (!isFinite(result.mag)) { - //another bugfix: if we hit -Infinity mag, then we should return negative infinity, not 0. 0.layeradd10(-1) h its this - if (result.sign === 0) { - result.sign = 1; - } //also this, for 0.layeradd10(-2) + if (base.lte(Decimal.dZero)) { + return Decimal.dNaN; + } //slog base 1 is NaN - if (result.layer < 0) { - result.layer = 0; - } + if (base.eq(Decimal.dOne)) { + return Decimal.dNaN; + } //need to handle these small, wobbling bases specially - return result.normalize(); - } - if (result.layer >= 0) { - break; - } - } - } + if (base.lt(Decimal.dOne)) { + if (this.eq(Decimal.dOne)) { + return Decimal.dZero; } - while (result.layer < 0) { - result.layer++; - result.mag = Math.log10(result.mag); - } //bugfix: before we normalize: if we started with 0, we now need to manually fix a layer ourselves! - + if (this.eq(Decimal.dZero)) { + return Decimal.dNegOne; + } //0 < this < 1: ambiguous (happens multiple times) + //this < 0: impossible (as far as I can tell) + //this > 1: partially complex (http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html base 0.25 for proof) - if (result.sign === 0) { - result.sign = 1; - if (result.mag === 0 && result.layer >= 1) { - result.layer -= 1; - result.mag = 1; - } - } + return Decimal.dNaN; + } //slog_n(0) is -1 - result.normalize(); //layeradd10: like adding 'diff' to the number's slog(base) representation. Very similar to tetrate base 10 and iterated log base 10. Also equivalent to adding a fractional amount to the number's layer in its break_eternity.js representation. - if (diff !== 0) { - return result.layeradd(diff, 10); //safe, only calls positive height 1 payload tetration, slog and log - } + if (this.mag < 0 || this.eq(Decimal.dZero)) { + return Decimal.dNegOne; + } - return result; - } //layeradd: like adding 'diff' to the number's slog(base) representation. Very similar to tetrate base 'base' and iterated log base 'base'. + var result = 0; + var copy = Decimal.fromDecimal(this); - }, { - key: "layeradd", - value: function layeradd(diff, base) { - var slogthis = this.slog(base).toNumber(); - var slogdest = slogthis + diff; + if (copy.layer - base.layer > 3) { + var layerloss = copy.layer - base.layer - 3; + result += layerloss; + copy.layer -= layerloss; + } - if (slogdest >= 0) { - return Decimal.tetrate(base, slogdest); - } else if (!Number.isFinite(slogdest)) { - return Decimal.dNaN; - } else if (slogdest >= -1) { - return Decimal.log(Decimal.tetrate(base, slogdest + 1), base); + for (var i = 0; i < 100; ++i) { + if (copy.lt(Decimal.dZero)) { + copy = Decimal.pow(base, copy); + result -= 1; + } else if (copy.lte(Decimal.dOne)) { + return Decimal.fromNumber(result + Decimal.slog_critical(base.toNumber(), copy.toNumber())); } else { - return Decimal.log(Decimal.log(Decimal.tetrate(base, slogdest + 2), base), base); - } - } //The Lambert W function, also called the omega function or product logarithm, is the solution W(x) === x*e^x. - // https://en.wikipedia.org/wiki/Lambert_W_function - //Some special values, for testing: https://en.wikipedia.org/wiki/Lambert_W_function#Special_values - - }, { - key: "lambertw", - value: function lambertw() { - if (this.lt(-0.3678794411710499)) { - throw Error("lambertw is unimplemented for results less than -1, sorry!"); - } else if (this.mag < 0) { - return Decimal.fromNumber(f_lambertw(this.toNumber())); - } else if (this.layer === 0) { - return Decimal.fromNumber(f_lambertw(this.sign * this.mag)); - } else if (this.layer === 1) { - return d_lambertw(this); - } else if (this.layer === 2) { - return d_lambertw(this); + result += 1; + copy = Decimal.log(copy, base); + } + } + + return Decimal.fromNumber(result); + } //background info and tables of values for critical functions taken here: https://github.com/Patashu/break_eternity.js/issues/22 + + }, { + key: "layeradd10", + value: //Function for adding/removing layers from a Decimal, even fractional layers (e.g. its slog10 representation). + //Moved this over to use the same critical section as tetrate/slog. + function layeradd10(diff) { + diff = Decimal.fromValue_noAlloc(diff).toNumber(); + var result = Decimal.fromDecimal(this); + + if (diff >= 1) { + //bug fix: if result is very smol (mag < 0, layer > 0) turn it into 0 first + if (result.mag < 0 && result.layer > 0) { + result.sign = 0; + result.mag = 0; + result.layer = 0; + } else if (result.sign === -1 && result.layer == 0) { + //bug fix - for stuff like -3.layeradd10(1) we need to move the sign to the mag + result.sign = 1; + result.mag = -result.mag; } - if (this.layer >= 3) { - return FC_NN(this.sign, this.layer - 1, this.mag); - } + var layeradd = Math.trunc(diff); + diff -= layeradd; + result.layer += layeradd; + } - throw "Unhandled behavior in lambertw()"; - } //The super square-root function - what number, tetrated to height 2, equals this? - //Other sroots are possible to calculate probably through guess and check methods, this one is easy though. - // https://en.wikipedia.org/wiki/Tetration#Super-root + if (diff <= -1) { + var _layeradd = Math.trunc(diff); - }, { - key: "ssqrt", - value: function ssqrt() { - if (this.sign == 1 && this.layer >= 3) { - return FC_NN(this.sign, this.layer - 1, this.mag); - } + diff -= _layeradd; + result.layer += _layeradd; - var lnx = this.ln(); - return lnx.div(lnx.lambertw()); - } //Pentation/pentate: The result of tetrating 'height' times in a row. An absurdly strong operator - Decimal.pentate(2, 4.28) and Decimal.pentate(10, 2.37) are already too huge for break_eternity.js! - // https://en.wikipedia.org/wiki/Pentation - - }, { - key: "pentate", - value: function pentate() { - var height = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2; - var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : FC_NN(1, 0, 1); - payload = D(payload); - var oldheight = height; - height = Math.trunc(height); - var fracheight = oldheight - height; //I have no idea if this is a meaningful approximation for pentation to continuous heights, but it is monotonic and continuous. - - if (fracheight !== 0) { - if (payload.eq(Decimal.dOne)) { - ++height; - payload = Decimal.fromNumber(fracheight); - } else { - if (this.eq(10)) { - payload = payload.layeradd10(fracheight); - } else { - payload = payload.layeradd(fracheight, this); + if (result.layer < 0) { + for (var i = 0; i < 100; ++i) { + result.layer++; + result.mag = Math.log10(result.mag); + + if (!isFinite(result.mag)) { + //another bugfix: if we hit -Infinity mag, then we should return negative infinity, not 0. 0.layeradd10(-1) h its this + if (result.sign === 0) { + result.sign = 1; + } //also this, for 0.layeradd10(-2) + + + if (result.layer < 0) { + result.layer = 0; + } + + return result.normalize(); + } + + if (result.layer >= 0) { + break; } } } + } - for (var i = 0; i < height; ++i) { - payload = this.tetrate(payload.toNumber()); //bail if we're NaN + while (result.layer < 0) { + result.layer++; + result.mag = Math.log10(result.mag); + } //bugfix: before we normalize: if we started with 0, we now need to manually fix a layer ourselves! - if (!isFinite(payload.layer) || !isFinite(payload.mag)) { - return payload.normalize(); - } //give up after 10 iterations if nothing is happening + if (result.sign === 0) { + result.sign = 1; - if (i > 10) { - return payload; - } + if (result.mag === 0 && result.layer >= 1) { + result.layer -= 1; + result.mag = 1; } + } - return payload; - } // trig functions! + result.normalize(); //layeradd10: like adding 'diff' to the number's slog(base) representation. Very similar to tetrate base 10 and iterated log base 10. Also equivalent to adding a fractional amount to the number's layer in its break_eternity.js representation. - }, { - key: "sin", - value: function sin() { - if (this.mag < 0) { - return this; + if (diff !== 0) { + return result.layeradd(diff, 10); //safe, only calls positive height 1 payload tetration, slog and log + } + + return result; + } //layeradd: like adding 'diff' to the number's slog(base) representation. Very similar to tetrate base 'base' and iterated log base 'base'. + + }, { + key: "layeradd", + value: function layeradd(diff, base) { + var slogthis = this.slog(base).toNumber(); + var slogdest = slogthis + diff; + + if (slogdest >= 0) { + return Decimal.tetrate(base, slogdest); + } else if (!Number.isFinite(slogdest)) { + return Decimal.dNaN; + } else if (slogdest >= -1) { + return Decimal.log(Decimal.tetrate(base, slogdest + 1), base); + } else { + return Decimal.log(Decimal.log(Decimal.tetrate(base, slogdest + 2), base), base); + } + } //The Lambert W function, also called the omega function or product logarithm, is the solution W(x) === x*e^x. + // https://en.wikipedia.org/wiki/Lambert_W_function + //Some special values, for testing: https://en.wikipedia.org/wiki/Lambert_W_function#Special_values + + }, { + key: "lambertw", + value: function lambertw() { + if (this.lt(-0.3678794411710499)) { + throw Error("lambertw is unimplemented for results less than -1, sorry!"); + } else if (this.mag < 0) { + return Decimal.fromNumber(f_lambertw(this.toNumber())); + } else if (this.layer === 0) { + return Decimal.fromNumber(f_lambertw(this.sign * this.mag)); + } else if (this.layer === 1) { + return d_lambertw(this); + } else if (this.layer === 2) { + return d_lambertw(this); + } + + if (this.layer >= 3) { + return FC_NN(this.sign, this.layer - 1, this.mag); + } + + throw "Unhandled behavior in lambertw()"; + } //The super square-root function - what number, tetrated to height 2, equals this? + //Other sroots are possible to calculate probably through guess and check methods, this one is easy though. + // https://en.wikipedia.org/wiki/Tetration#Super-root + + }, { + key: "ssqrt", + value: function ssqrt() { + if (this.sign == 1 && this.layer >= 3) { + return FC_NN(this.sign, this.layer - 1, this.mag); + } + + var lnx = this.ln(); + return lnx.div(lnx.lambertw()); + } //Pentation/pentate: The result of tetrating 'height' times in a row. An absurdly strong operator - Decimal.pentate(2, 4.28) and Decimal.pentate(10, 2.37) are already too huge for break_eternity.js! + // https://en.wikipedia.org/wiki/Pentation + + }, { + key: "pentate", + value: function pentate() { + var height = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2; + var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : FC_NN(1, 0, 1); + payload = D(payload); + var oldheight = height; + height = Math.trunc(height); + var fracheight = oldheight - height; //I have no idea if this is a meaningful approximation for pentation to continuous heights, but it is monotonic and continuous. + + if (fracheight !== 0) { + if (payload.eq(Decimal.dOne)) { + ++height; + payload = Decimal.fromNumber(fracheight); + } else { + if (this.eq(10)) { + payload = payload.layeradd10(fracheight); + } else { + payload = payload.layeradd(fracheight, this); + } } + } - if (this.layer === 0) { - return Decimal.fromNumber(Math.sin(this.sign * this.mag)); + for (var i = 0; i < height; ++i) { + payload = this.tetrate(payload.toNumber()); //bail if we're NaN + + if (!isFinite(payload.layer) || !isFinite(payload.mag)) { + return payload.normalize(); + } //give up after 10 iterations if nothing is happening + + + if (i > 10) { + return payload; } + } - return FC_NN(0, 0, 0); + return payload; + } // trig functions! + + }, { + key: "sin", + value: function sin() { + if (this.mag < 0) { + return this; } - }, { - key: "cos", - value: function cos() { - if (this.mag < 0) { - return Decimal.dOne; - } - if (this.layer === 0) { - return Decimal.fromNumber(Math.cos(this.sign * this.mag)); - } + if (this.layer === 0) { + return Decimal.fromNumber(Math.sin(this.sign * this.mag)); + } - return FC_NN(0, 0, 0); + return FC_NN(0, 0, 0); + } + }, { + key: "cos", + value: function cos() { + if (this.mag < 0) { + return Decimal.dOne; } - }, { - key: "tan", - value: function tan() { - if (this.mag < 0) { - return this; - } - if (this.layer === 0) { - return Decimal.fromNumber(Math.tan(this.sign * this.mag)); - } + if (this.layer === 0) { + return Decimal.fromNumber(Math.cos(this.sign * this.mag)); + } - return FC_NN(0, 0, 0); + return FC_NN(0, 0, 0); + } + }, { + key: "tan", + value: function tan() { + if (this.mag < 0) { + return this; } - }, { - key: "asin", - value: function asin() { - if (this.mag < 0) { - return this; - } - if (this.layer === 0) { - return Decimal.fromNumber(Math.asin(this.sign * this.mag)); - } + if (this.layer === 0) { + return Decimal.fromNumber(Math.tan(this.sign * this.mag)); + } - return FC_NN(Number.NaN, Number.NaN, Number.NaN); + return FC_NN(0, 0, 0); + } + }, { + key: "asin", + value: function asin() { + if (this.mag < 0) { + return this; } - }, { - key: "acos", - value: function acos() { - if (this.mag < 0) { - return Decimal.fromNumber(Math.acos(this.toNumber())); - } - if (this.layer === 0) { - return Decimal.fromNumber(Math.acos(this.sign * this.mag)); - } + if (this.layer === 0) { + return Decimal.fromNumber(Math.asin(this.sign * this.mag)); + } - return FC_NN(Number.NaN, Number.NaN, Number.NaN); + return FC_NN(Number.NaN, Number.NaN, Number.NaN); + } + }, { + key: "acos", + value: function acos() { + if (this.mag < 0) { + return Decimal.fromNumber(Math.acos(this.toNumber())); } - }, { - key: "atan", - value: function atan() { - if (this.mag < 0) { - return this; - } - if (this.layer === 0) { - return Decimal.fromNumber(Math.atan(this.sign * this.mag)); - } + if (this.layer === 0) { + return Decimal.fromNumber(Math.acos(this.sign * this.mag)); + } - return Decimal.fromNumber(Math.atan(this.sign * 1.8e308)); - } - }, { - key: "sinh", - value: function sinh() { - return this.exp().sub(this.negate().exp()).div(2); - } - }, { - key: "cosh", - value: function cosh() { - return this.exp().add(this.negate().exp()).div(2); - } - }, { - key: "tanh", - value: function tanh() { - return this.sinh().div(this.cosh()); - } - }, { - key: "asinh", - value: function asinh() { - return Decimal.ln(this.add(this.sqr().add(1).sqrt())); - } - }, { - key: "acosh", - value: function acosh() { - return Decimal.ln(this.add(this.sqr().sub(1).sqrt())); - } - }, { - key: "atanh", - value: function atanh() { - if (this.abs().gte(1)) { - return FC_NN(Number.NaN, Number.NaN, Number.NaN); - } + return FC_NN(Number.NaN, Number.NaN, Number.NaN); + } + }, { + key: "atan", + value: function atan() { + if (this.mag < 0) { + return this; + } - return Decimal.ln(this.add(1).div(Decimal.fromNumber(1).sub(this))).div(2); + if (this.layer === 0) { + return Decimal.fromNumber(Math.atan(this.sign * this.mag)); } - /** - * Joke function from Realm Grinder - */ - }, { - key: "ascensionPenalty", - value: function ascensionPenalty(ascensions) { - if (ascensions === 0) { - return this; - } + return Decimal.fromNumber(Math.atan(this.sign * 1.8e308)); + } + }, { + key: "sinh", + value: function sinh() { + return this.exp().sub(this.negate().exp()).div(2); + } + }, { + key: "cosh", + value: function cosh() { + return this.exp().add(this.negate().exp()).div(2); + } + }, { + key: "tanh", + value: function tanh() { + return this.sinh().div(this.cosh()); + } + }, { + key: "asinh", + value: function asinh() { + return Decimal.ln(this.add(this.sqr().add(1).sqrt())); + } + }, { + key: "acosh", + value: function acosh() { + return Decimal.ln(this.add(this.sqr().sub(1).sqrt())); + } + }, { + key: "atanh", + value: function atanh() { + if (this.abs().gte(1)) { + return FC_NN(Number.NaN, Number.NaN, Number.NaN); + } - return this.root(Decimal.pow(10, ascensions)); - } - /** - * Joke function from Cookie Clicker. It's 'egg' - */ - - }, { - key: "egg", - value: function egg() { - return this.add(9); - } - }, { - key: "lessThanOrEqualTo", - value: function lessThanOrEqualTo(other) { - return this.cmp(other) < 1; - } - }, { - key: "lessThan", - value: function lessThan(other) { - return this.cmp(other) < 0; - } - }, { - key: "greaterThanOrEqualTo", - value: function greaterThanOrEqualTo(other) { - return this.cmp(other) > -1; - } - }, { - key: "greaterThan", - value: function greaterThan(other) { - return this.cmp(other) > 0; - } - }], [{ - key: "fromComponents", - value: function fromComponents(sign, layer, mag) { - return new Decimal().fromComponents(sign, layer, mag); - } - }, { - key: "fromComponents_noNormalize", - value: function fromComponents_noNormalize(sign, layer, mag) { - return new Decimal().fromComponents_noNormalize(sign, layer, mag); - } - }, { - key: "fromMantissaExponent", - value: function fromMantissaExponent(mantissa, exponent) { - return new Decimal().fromMantissaExponent(mantissa, exponent); - } - }, { - key: "fromMantissaExponent_noNormalize", - value: function fromMantissaExponent_noNormalize(mantissa, exponent) { - return new Decimal().fromMantissaExponent_noNormalize(mantissa, exponent); - } - }, { - key: "fromDecimal", - value: function fromDecimal(value) { - return new Decimal().fromDecimal(value); - } - }, { - key: "fromNumber", - value: function fromNumber(value) { - return new Decimal().fromNumber(value); - } - }, { - key: "fromString", - value: function fromString(value) { - return new Decimal().fromString(value); - } - }, { - key: "fromValue", - value: function fromValue(value) { - return new Decimal().fromValue(value); - } - /** - * Converts a DecimalSource to a Decimal, without constructing a new Decimal - * if the provided value is already a Decimal. - * - * As the return value could be the provided value itself, this function - * returns a read-only Decimal to prevent accidental mutations of the value. - * Use `new Decimal(value)` to explicitly create a writeable copy if mutation - * is required. - */ - - }, { - key: "fromValue_noAlloc", - value: function fromValue_noAlloc(value) { - if (value instanceof Decimal) { - return value; - } else if (typeof value === "string") { - var cached = Decimal.fromStringCache.get(value); - - if (cached !== undefined) { - return cached; - } + return Decimal.ln(this.add(1).div(Decimal.fromNumber(1).sub(this))).div(2); + } + /** + * Joke function from Realm Grinder + */ - return Decimal.fromString(value); - } else if (typeof value === "number") { - return Decimal.fromNumber(value); - } else { - // This should never happen... but some users like Prestige Tree Rewritten - // pass undefined values in as DecimalSources, so we should handle this - // case to not break them. - return Decimal.dZero; - } + }, { + key: "ascensionPenalty", + value: function ascensionPenalty(ascensions) { + if (ascensions === 0) { + return this; } - }, { - key: "abs", - value: function abs(value) { - return D(value).abs(); - } - }, { - key: "neg", - value: function neg(value) { - return D(value).neg(); - } - }, { - key: "negate", - value: function negate(value) { - return D(value).neg(); - } - }, { - key: "negated", - value: function negated(value) { - return D(value).neg(); - } - }, { - key: "sign", - value: function sign(value) { - return D(value).sign; - } - }, { - key: "sgn", - value: function sgn(value) { - return D(value).sign; - } - }, { - key: "round", - value: function round(value) { - return D(value).round(); - } - }, { - key: "floor", - value: function floor(value) { - return D(value).floor(); - } - }, { - key: "ceil", - value: function ceil(value) { - return D(value).ceil(); - } - }, { - key: "trunc", - value: function trunc(value) { - return D(value).trunc(); - } - }, { - key: "add", - value: function add(value, other) { - return D(value).add(other); - } - }, { - key: "plus", - value: function plus(value, other) { - return D(value).add(other); - } - }, { - key: "sub", - value: function sub(value, other) { - return D(value).sub(other); - } - }, { - key: "subtract", - value: function subtract(value, other) { - return D(value).sub(other); - } - }, { - key: "minus", - value: function minus(value, other) { - return D(value).sub(other); - } - }, { - key: "mul", - value: function mul(value, other) { - return D(value).mul(other); - } - }, { - key: "multiply", - value: function multiply(value, other) { - return D(value).mul(other); - } - }, { - key: "times", - value: function times(value, other) { - return D(value).mul(other); - } - }, { - key: "div", - value: function div(value, other) { - return D(value).div(other); - } - }, { - key: "divide", - value: function divide(value, other) { - return D(value).div(other); - } - }, { - key: "recip", - value: function recip(value) { - return D(value).recip(); - } - }, { - key: "reciprocal", - value: function reciprocal(value) { - return D(value).recip(); - } - }, { - key: "reciprocate", - value: function reciprocate(value) { - return D(value).reciprocate(); - } - }, { - key: "cmp", - value: function cmp(value, other) { - return D(value).cmp(other); - } - }, { - key: "cmpabs", - value: function cmpabs(value, other) { - return D(value).cmpabs(other); - } - }, { - key: "compare", - value: function compare(value, other) { - return D(value).cmp(other); - } - }, { - key: "isNaN", - value: function (_isNaN) { - function isNaN(_x) { - return _isNaN.apply(this, arguments); - } - isNaN.toString = function () { - return _isNaN.toString(); - }; - - return isNaN; - }(function (value) { - value = D(value); - return isNaN(value.sign) || isNaN(value.layer) || isNaN(value.mag); - }) - }, { - key: "isFinite", - value: function (_isFinite) { - function isFinite(_x2) { - return _isFinite.apply(this, arguments); - } + return this.root(Decimal.pow(10, ascensions)); + } + /** + * Joke function from Cookie Clicker. It's 'egg' + */ - isFinite.toString = function () { - return _isFinite.toString(); - }; - - return isFinite; - }(function (value) { - value = D(value); - return isFinite(value.sign) && isFinite(value.layer) && isFinite(value.mag); - }) - }, { - key: "eq", - value: function eq(value, other) { - return D(value).eq(other); - } - }, { - key: "equals", - value: function equals(value, other) { - return D(value).eq(other); - } - }, { - key: "neq", - value: function neq(value, other) { - return D(value).neq(other); - } - }, { - key: "notEquals", - value: function notEquals(value, other) { - return D(value).notEquals(other); - } - }, { - key: "lt", - value: function lt(value, other) { - return D(value).lt(other); - } - }, { - key: "lte", - value: function lte(value, other) { - return D(value).lte(other); - } - }, { - key: "gt", - value: function gt(value, other) { - return D(value).gt(other); - } - }, { - key: "gte", - value: function gte(value, other) { - return D(value).gte(other); - } - }, { - key: "max", - value: function max(value, other) { - return D(value).max(other); - } - }, { - key: "min", - value: function min(value, other) { - return D(value).min(other); - } - }, { - key: "minabs", - value: function minabs(value, other) { - return D(value).minabs(other); - } - }, { - key: "maxabs", - value: function maxabs(value, other) { - return D(value).maxabs(other); - } - }, { - key: "clamp", - value: function clamp(value, min, max) { - return D(value).clamp(min, max); - } - }, { - key: "clampMin", - value: function clampMin(value, min) { - return D(value).clampMin(min); - } - }, { - key: "clampMax", - value: function clampMax(value, max) { - return D(value).clampMax(max); - } - }, { - key: "cmp_tolerance", - value: function cmp_tolerance(value, other, tolerance) { - return D(value).cmp_tolerance(other, tolerance); - } - }, { - key: "compare_tolerance", - value: function compare_tolerance(value, other, tolerance) { - return D(value).cmp_tolerance(other, tolerance); - } - }, { - key: "eq_tolerance", - value: function eq_tolerance(value, other, tolerance) { - return D(value).eq_tolerance(other, tolerance); - } - }, { - key: "equals_tolerance", - value: function equals_tolerance(value, other, tolerance) { - return D(value).eq_tolerance(other, tolerance); - } - }, { - key: "neq_tolerance", - value: function neq_tolerance(value, other, tolerance) { - return D(value).neq_tolerance(other, tolerance); - } - }, { - key: "notEquals_tolerance", - value: function notEquals_tolerance(value, other, tolerance) { - return D(value).notEquals_tolerance(other, tolerance); - } - }, { - key: "lt_tolerance", - value: function lt_tolerance(value, other, tolerance) { - return D(value).lt_tolerance(other, tolerance); - } - }, { - key: "lte_tolerance", - value: function lte_tolerance(value, other, tolerance) { - return D(value).lte_tolerance(other, tolerance); - } - }, { - key: "gt_tolerance", - value: function gt_tolerance(value, other, tolerance) { - return D(value).gt_tolerance(other, tolerance); - } - }, { - key: "gte_tolerance", - value: function gte_tolerance(value, other, tolerance) { - return D(value).gte_tolerance(other, tolerance); - } - }, { - key: "pLog10", - value: function pLog10(value) { - return D(value).pLog10(); - } - }, { - key: "absLog10", - value: function absLog10(value) { - return D(value).absLog10(); - } - }, { - key: "log10", - value: function log10(value) { - return D(value).log10(); - } - }, { - key: "log", - value: function log(value, base) { - return D(value).log(base); - } - }, { - key: "log2", - value: function log2(value) { - return D(value).log2(); - } - }, { - key: "ln", - value: function ln(value) { - return D(value).ln(); - } - }, { - key: "logarithm", - value: function logarithm(value, base) { - return D(value).logarithm(base); - } - }, { - key: "pow", - value: function pow(value, other) { - return D(value).pow(other); - } - }, { - key: "pow10", - value: function pow10(value) { - return D(value).pow10(); - } - }, { - key: "root", - value: function root(value, other) { - return D(value).root(other); - } - }, { - key: "factorial", - value: function factorial(value, _other) { - return D(value).factorial(); - } - }, { - key: "gamma", - value: function gamma(value, _other) { - return D(value).gamma(); - } - }, { - key: "lngamma", - value: function lngamma(value, _other) { - return D(value).lngamma(); - } - }, { - key: "exp", - value: function exp(value) { - return D(value).exp(); - } - }, { - key: "sqr", - value: function sqr(value) { - return D(value).sqr(); - } - }, { - key: "sqrt", - value: function sqrt(value) { - return D(value).sqrt(); - } - }, { - key: "cube", - value: function cube(value) { - return D(value).cube(); - } - }, { - key: "cbrt", - value: function cbrt(value) { - return D(value).cbrt(); - } - }, { - key: "tetrate", - value: function tetrate(value) { - var height = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; - var payload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : FC_NN(1, 0, 1); - return D(value).tetrate(height, payload); - } - }, { - key: "iteratedexp", - value: function iteratedexp(value) { - var height = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; - var payload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : FC_NN(1, 0, 1); - return D(value).iteratedexp(height, payload); - } - }, { - key: "iteratedlog", - value: function iteratedlog(value) { - var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10; - var times = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; - return D(value).iteratedlog(base, times); - } - }, { - key: "layeradd10", - value: function layeradd10(value, diff) { - return D(value).layeradd10(diff); - } - }, { - key: "layeradd", - value: function layeradd(value, diff) { - var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; - return D(value).layeradd(diff, base); - } - }, { - key: "slog", - value: function slog(value) { - var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10; - return D(value).slog(base); - } - }, { - key: "lambertw", - value: function lambertw(value) { - return D(value).lambertw(); - } - }, { - key: "ssqrt", - value: function ssqrt(value) { - return D(value).ssqrt(); - } - }, { - key: "pentate", - value: function pentate(value) { - var height = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; - var payload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : FC_NN(1, 0, 1); - return D(value).pentate(height, payload); - } - /** - * If you're willing to spend 'resourcesAvailable' and want to buy something - * with exponentially increasing cost each purchase (start at priceStart, - * multiply by priceRatio, already own currentOwned), how much of it can you buy? - * Adapted from Trimps source code. - */ - - }, { - key: "affordGeometricSeries", - value: function affordGeometricSeries(resourcesAvailable, priceStart, priceRatio, currentOwned) { - return this.affordGeometricSeries_core(D(resourcesAvailable), D(priceStart), D(priceRatio), currentOwned); - } - /** - * How much resource would it cost to buy (numItems) items if you already have currentOwned, - * the initial price is priceStart and it multiplies by priceRatio each purchase? - */ - - }, { - key: "sumGeometricSeries", - value: function sumGeometricSeries(numItems, priceStart, priceRatio, currentOwned) { - return this.sumGeometricSeries_core(numItems, D(priceStart), D(priceRatio), currentOwned); - } - /** - * If you're willing to spend 'resourcesAvailable' and want to buy something with additively - * increasing cost each purchase (start at priceStart, add by priceAdd, already own currentOwned), - * how much of it can you buy? - */ - - }, { - key: "affordArithmeticSeries", - value: function affordArithmeticSeries(resourcesAvailable, priceStart, priceAdd, currentOwned) { - return this.affordArithmeticSeries_core(D(resourcesAvailable), D(priceStart), D(priceAdd), D(currentOwned)); - } - /** - * How much resource would it cost to buy (numItems) items if you already have currentOwned, - * the initial price is priceStart and it adds priceAdd each purchase? - * Adapted from http://www.mathwords.com/a/arithmetic_series.htm - */ - - }, { - key: "sumArithmeticSeries", - value: function sumArithmeticSeries(numItems, priceStart, priceAdd, currentOwned) { - return this.sumArithmeticSeries_core(D(numItems), D(priceStart), D(priceAdd), D(currentOwned)); - } - /** - * When comparing two purchases that cost (resource) and increase your resource/sec by (deltaRpS), - * the lowest efficiency score is the better one to purchase. - * From Frozen Cookies: - * http://cookieclicker.wikia.com/wiki/Frozen_Cookies_(JavaScript_Add-on)#Efficiency.3F_What.27s_that.3F - */ - - }, { - key: "efficiencyOfPurchase", - value: function efficiencyOfPurchase(cost, currentRpS, deltaRpS) { - return this.efficiencyOfPurchase_core(D(cost), D(currentRpS), D(deltaRpS)); - } - }, { - key: "randomDecimalForTesting", - value: function randomDecimalForTesting(maxLayers) { - // NOTE: This doesn't follow any kind of sane random distribution, so use this for testing purposes only. - //5% of the time, return 0 - if (Math.random() * 20 < 1) { - return FC_NN(0, 0, 0); + }, { + key: "egg", + value: function egg() { + return this.add(9); + } + }, { + key: "lessThanOrEqualTo", + value: function lessThanOrEqualTo(other) { + return this.cmp(other) < 1; + } + }, { + key: "lessThan", + value: function lessThan(other) { + return this.cmp(other) < 0; + } + }, { + key: "greaterThanOrEqualTo", + value: function greaterThanOrEqualTo(other) { + return this.cmp(other) > -1; + } + }, { + key: "greaterThan", + value: function greaterThan(other) { + return this.cmp(other) > 0; + } + }], [{ + key: "fromComponents", + value: function fromComponents(sign, layer, mag) { + return new Decimal().fromComponents(sign, layer, mag); + } + }, { + key: "fromComponents_noNormalize", + value: function fromComponents_noNormalize(sign, layer, mag) { + return new Decimal().fromComponents_noNormalize(sign, layer, mag); + } + }, { + key: "fromMantissaExponent", + value: function fromMantissaExponent(mantissa, exponent) { + return new Decimal().fromMantissaExponent(mantissa, exponent); + } + }, { + key: "fromMantissaExponent_noNormalize", + value: function fromMantissaExponent_noNormalize(mantissa, exponent) { + return new Decimal().fromMantissaExponent_noNormalize(mantissa, exponent); + } + }, { + key: "fromDecimal", + value: function fromDecimal(value) { + return new Decimal().fromDecimal(value); + } + }, { + key: "fromNumber", + value: function fromNumber(value) { + return new Decimal().fromNumber(value); + } + }, { + key: "fromString", + value: function fromString(value) { + return new Decimal().fromString(value); + } + }, { + key: "fromValue", + value: function fromValue(value) { + return new Decimal().fromValue(value); + } + /** + * Converts a DecimalSource to a Decimal, without constructing a new Decimal + * if the provided value is already a Decimal. + * + * As the return value could be the provided value itself, this function + * returns a read-only Decimal to prevent accidental mutations of the value. + * Use `new Decimal(value)` to explicitly create a writeable copy if mutation + * is required. + */ + + }, { + key: "fromValue_noAlloc", + value: function fromValue_noAlloc(value) { + if (value instanceof Decimal) { + return value; + } else if (typeof value === "string") { + var cached = Decimal.fromStringCache.get(value); + + if (cached !== undefined) { + return cached; } - var randomsign = Math.random() > 0.5 ? 1 : -1; //5% of the time, return 1 or -1 + return Decimal.fromString(value); + } else if (typeof value === "number") { + return Decimal.fromNumber(value); + } else { + // This should never happen... but some users like Prestige Tree Rewritten + // pass undefined values in as DecimalSources, so we should handle this + // case to not break them. + return Decimal.dZero; + } + } + }, { + key: "abs", + value: function abs(value) { + return D(value).abs(); + } + }, { + key: "neg", + value: function neg(value) { + return D(value).neg(); + } + }, { + key: "negate", + value: function negate(value) { + return D(value).neg(); + } + }, { + key: "negated", + value: function negated(value) { + return D(value).neg(); + } + }, { + key: "sign", + value: function sign(value) { + return D(value).sign; + } + }, { + key: "sgn", + value: function sgn(value) { + return D(value).sign; + } + }, { + key: "round", + value: function round(value) { + return D(value).round(); + } + }, { + key: "floor", + value: function floor(value) { + return D(value).floor(); + } + }, { + key: "ceil", + value: function ceil(value) { + return D(value).ceil(); + } + }, { + key: "trunc", + value: function trunc(value) { + return D(value).trunc(); + } + }, { + key: "add", + value: function add(value, other) { + return D(value).add(other); + } + }, { + key: "plus", + value: function plus(value, other) { + return D(value).add(other); + } + }, { + key: "sub", + value: function sub(value, other) { + return D(value).sub(other); + } + }, { + key: "subtract", + value: function subtract(value, other) { + return D(value).sub(other); + } + }, { + key: "minus", + value: function minus(value, other) { + return D(value).sub(other); + } + }, { + key: "mul", + value: function mul(value, other) { + return D(value).mul(other); + } + }, { + key: "multiply", + value: function multiply(value, other) { + return D(value).mul(other); + } + }, { + key: "times", + value: function times(value, other) { + return D(value).mul(other); + } + }, { + key: "div", + value: function div(value, other) { + return D(value).div(other); + } + }, { + key: "divide", + value: function divide(value, other) { + return D(value).div(other); + } + }, { + key: "recip", + value: function recip(value) { + return D(value).recip(); + } + }, { + key: "reciprocal", + value: function reciprocal(value) { + return D(value).recip(); + } + }, { + key: "reciprocate", + value: function reciprocate(value) { + return D(value).reciprocate(); + } + }, { + key: "cmp", + value: function cmp(value, other) { + return D(value).cmp(other); + } + }, { + key: "cmpabs", + value: function cmpabs(value, other) { + return D(value).cmpabs(other); + } + }, { + key: "compare", + value: function compare(value, other) { + return D(value).cmp(other); + } + }, { + key: "isNaN", + value: function (_isNaN) { + function isNaN(_x) { + return _isNaN.apply(this, arguments); + } + + isNaN.toString = function () { + return _isNaN.toString(); + }; + + return isNaN; + }(function (value) { + value = D(value); + return isNaN(value.sign) || isNaN(value.layer) || isNaN(value.mag); + }) + }, { + key: "isFinite", + value: function (_isFinite) { + function isFinite(_x2) { + return _isFinite.apply(this, arguments); + } + + isFinite.toString = function () { + return _isFinite.toString(); + }; + + return isFinite; + }(function (value) { + value = D(value); + return isFinite(value.sign) && isFinite(value.layer) && isFinite(value.mag); + }) + }, { + key: "eq", + value: function eq(value, other) { + return D(value).eq(other); + } + }, { + key: "equals", + value: function equals(value, other) { + return D(value).eq(other); + } + }, { + key: "neq", + value: function neq(value, other) { + return D(value).neq(other); + } + }, { + key: "notEquals", + value: function notEquals(value, other) { + return D(value).notEquals(other); + } + }, { + key: "lt", + value: function lt(value, other) { + return D(value).lt(other); + } + }, { + key: "lte", + value: function lte(value, other) { + return D(value).lte(other); + } + }, { + key: "gt", + value: function gt(value, other) { + return D(value).gt(other); + } + }, { + key: "gte", + value: function gte(value, other) { + return D(value).gte(other); + } + }, { + key: "max", + value: function max(value, other) { + return D(value).max(other); + } + }, { + key: "min", + value: function min(value, other) { + return D(value).min(other); + } + }, { + key: "minabs", + value: function minabs(value, other) { + return D(value).minabs(other); + } + }, { + key: "maxabs", + value: function maxabs(value, other) { + return D(value).maxabs(other); + } + }, { + key: "clamp", + value: function clamp(value, min, max) { + return D(value).clamp(min, max); + } + }, { + key: "clampMin", + value: function clampMin(value, min) { + return D(value).clampMin(min); + } + }, { + key: "clampMax", + value: function clampMax(value, max) { + return D(value).clampMax(max); + } + }, { + key: "cmp_tolerance", + value: function cmp_tolerance(value, other, tolerance) { + return D(value).cmp_tolerance(other, tolerance); + } + }, { + key: "compare_tolerance", + value: function compare_tolerance(value, other, tolerance) { + return D(value).cmp_tolerance(other, tolerance); + } + }, { + key: "eq_tolerance", + value: function eq_tolerance(value, other, tolerance) { + return D(value).eq_tolerance(other, tolerance); + } + }, { + key: "equals_tolerance", + value: function equals_tolerance(value, other, tolerance) { + return D(value).eq_tolerance(other, tolerance); + } + }, { + key: "neq_tolerance", + value: function neq_tolerance(value, other, tolerance) { + return D(value).neq_tolerance(other, tolerance); + } + }, { + key: "notEquals_tolerance", + value: function notEquals_tolerance(value, other, tolerance) { + return D(value).notEquals_tolerance(other, tolerance); + } + }, { + key: "lt_tolerance", + value: function lt_tolerance(value, other, tolerance) { + return D(value).lt_tolerance(other, tolerance); + } + }, { + key: "lte_tolerance", + value: function lte_tolerance(value, other, tolerance) { + return D(value).lte_tolerance(other, tolerance); + } + }, { + key: "gt_tolerance", + value: function gt_tolerance(value, other, tolerance) { + return D(value).gt_tolerance(other, tolerance); + } + }, { + key: "gte_tolerance", + value: function gte_tolerance(value, other, tolerance) { + return D(value).gte_tolerance(other, tolerance); + } + }, { + key: "pLog10", + value: function pLog10(value) { + return D(value).pLog10(); + } + }, { + key: "absLog10", + value: function absLog10(value) { + return D(value).absLog10(); + } + }, { + key: "log10", + value: function log10(value) { + return D(value).log10(); + } + }, { + key: "log", + value: function log(value, base) { + return D(value).log(base); + } + }, { + key: "log2", + value: function log2(value) { + return D(value).log2(); + } + }, { + key: "ln", + value: function ln(value) { + return D(value).ln(); + } + }, { + key: "logarithm", + value: function logarithm(value, base) { + return D(value).logarithm(base); + } + }, { + key: "pow", + value: function pow(value, other) { + return D(value).pow(other); + } + }, { + key: "pow10", + value: function pow10(value) { + return D(value).pow10(); + } + }, { + key: "root", + value: function root(value, other) { + return D(value).root(other); + } + }, { + key: "factorial", + value: function factorial(value, _other) { + return D(value).factorial(); + } + }, { + key: "gamma", + value: function gamma(value, _other) { + return D(value).gamma(); + } + }, { + key: "lngamma", + value: function lngamma(value, _other) { + return D(value).lngamma(); + } + }, { + key: "exp", + value: function exp(value) { + return D(value).exp(); + } + }, { + key: "sqr", + value: function sqr(value) { + return D(value).sqr(); + } + }, { + key: "sqrt", + value: function sqrt(value) { + return D(value).sqrt(); + } + }, { + key: "cube", + value: function cube(value) { + return D(value).cube(); + } + }, { + key: "cbrt", + value: function cbrt(value) { + return D(value).cbrt(); + } + }, { + key: "tetrate", + value: function tetrate(value) { + var height = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; + var payload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : FC_NN(1, 0, 1); + return D(value).tetrate(height, payload); + } + }, { + key: "iteratedexp", + value: function iteratedexp(value) { + var height = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; + var payload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : FC_NN(1, 0, 1); + return D(value).iteratedexp(height, payload); + } + }, { + key: "iteratedlog", + value: function iteratedlog(value) { + var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10; + var times = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1; + return D(value).iteratedlog(base, times); + } + }, { + key: "layeradd10", + value: function layeradd10(value, diff) { + return D(value).layeradd10(diff); + } + }, { + key: "layeradd", + value: function layeradd(value, diff) { + var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10; + return D(value).layeradd(diff, base); + } + }, { + key: "slog", + value: function slog(value) { + var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10; + return D(value).slog(base); + } + }, { + key: "lambertw", + value: function lambertw(value) { + return D(value).lambertw(); + } + }, { + key: "ssqrt", + value: function ssqrt(value) { + return D(value).ssqrt(); + } + }, { + key: "pentate", + value: function pentate(value) { + var height = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; + var payload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : FC_NN(1, 0, 1); + return D(value).pentate(height, payload); + } + /** + * If you're willing to spend 'resourcesAvailable' and want to buy something + * with exponentially increasing cost each purchase (start at priceStart, + * multiply by priceRatio, already own currentOwned), how much of it can you buy? + * Adapted from Trimps source code. + */ - if (Math.random() * 20 < 1) { - return FC_NN(randomsign, 0, 1); - } //pick a random layer + }, { + key: "affordGeometricSeries", + value: function affordGeometricSeries(resourcesAvailable, priceStart, priceRatio, currentOwned) { + return this.affordGeometricSeries_core(D(resourcesAvailable), D(priceStart), D(priceRatio), currentOwned); + } + /** + * How much resource would it cost to buy (numItems) items if you already have currentOwned, + * the initial price is priceStart and it multiplies by priceRatio each purchase? + */ + + }, { + key: "sumGeometricSeries", + value: function sumGeometricSeries(numItems, priceStart, priceRatio, currentOwned) { + return this.sumGeometricSeries_core(numItems, D(priceStart), D(priceRatio), currentOwned); + } + /** + * If you're willing to spend 'resourcesAvailable' and want to buy something with additively + * increasing cost each purchase (start at priceStart, add by priceAdd, already own currentOwned), + * how much of it can you buy? + */ + }, { + key: "affordArithmeticSeries", + value: function affordArithmeticSeries(resourcesAvailable, priceStart, priceAdd, currentOwned) { + return this.affordArithmeticSeries_core(D(resourcesAvailable), D(priceStart), D(priceAdd), D(currentOwned)); + } + /** + * How much resource would it cost to buy (numItems) items if you already have currentOwned, + * the initial price is priceStart and it adds priceAdd each purchase? + * Adapted from http://www.mathwords.com/a/arithmetic_series.htm + */ - var layer = Math.floor(Math.random() * (maxLayers + 1)); - var randomexp = layer === 0 ? Math.random() * 616 - 308 : Math.random() * 16; //10% of the time, make it a simple power of 10 + }, { + key: "sumArithmeticSeries", + value: function sumArithmeticSeries(numItems, priceStart, priceAdd, currentOwned) { + return this.sumArithmeticSeries_core(D(numItems), D(priceStart), D(priceAdd), D(currentOwned)); + } + /** + * When comparing two purchases that cost (resource) and increase your resource/sec by (deltaRpS), + * the lowest efficiency score is the better one to purchase. + * From Frozen Cookies: + * http://cookieclicker.wikia.com/wiki/Frozen_Cookies_(JavaScript_Add-on)#Efficiency.3F_What.27s_that.3F + */ - if (Math.random() > 0.9) { - randomexp = Math.trunc(randomexp); - } + }, { + key: "efficiencyOfPurchase", + value: function efficiencyOfPurchase(cost, currentRpS, deltaRpS) { + return this.efficiencyOfPurchase_core(D(cost), D(currentRpS), D(deltaRpS)); + } + }, { + key: "randomDecimalForTesting", + value: function randomDecimalForTesting(maxLayers) { + // NOTE: This doesn't follow any kind of sane random distribution, so use this for testing purposes only. + //5% of the time, return 0 + if (Math.random() * 20 < 1) { + return FC_NN(0, 0, 0); + } - var randommag = Math.pow(10, randomexp); //10% of the time, trunc mag + var randomsign = Math.random() > 0.5 ? 1 : -1; //5% of the time, return 1 or -1 - if (Math.random() > 0.9) { - randommag = Math.trunc(randommag); - } + if (Math.random() * 20 < 1) { + return FC_NN(randomsign, 0, 1); + } //pick a random layer - return FC(randomsign, layer, randommag); - } - }, { - key: "affordGeometricSeries_core", - value: function affordGeometricSeries_core(resourcesAvailable, priceStart, priceRatio, currentOwned) { - var actualStart = priceStart.mul(priceRatio.pow(currentOwned)); - return Decimal.floor(resourcesAvailable.div(actualStart).mul(priceRatio.sub(1)).add(1).log10().div(priceRatio.log10())); - } - }, { - key: "sumGeometricSeries_core", - value: function sumGeometricSeries_core(numItems, priceStart, priceRatio, currentOwned) { - return priceStart.mul(priceRatio.pow(currentOwned)).mul(Decimal.sub(1, priceRatio.pow(numItems))).div(Decimal.sub(1, priceRatio)); - } - }, { - key: "affordArithmeticSeries_core", - value: function affordArithmeticSeries_core(resourcesAvailable, priceStart, priceAdd, currentOwned) { - // n = (-(a-d/2) + sqrt((a-d/2)^2+2dS))/d - // where a is actualStart, d is priceAdd and S is resourcesAvailable - // then floor it and you're done! - var actualStart = priceStart.add(currentOwned.mul(priceAdd)); - var b = actualStart.sub(priceAdd.div(2)); - var b2 = b.pow(2); - return b.neg().add(b2.add(priceAdd.mul(resourcesAvailable).mul(2)).sqrt()).div(priceAdd).floor(); - } - }, { - key: "sumArithmeticSeries_core", - value: function sumArithmeticSeries_core(numItems, priceStart, priceAdd, currentOwned) { - var actualStart = priceStart.add(currentOwned.mul(priceAdd)); // (n/2)*(2*a+(n-1)*d) - - return numItems.div(2).mul(actualStart.mul(2).plus(numItems.sub(1).mul(priceAdd))); - } - }, { - key: "efficiencyOfPurchase_core", - value: function efficiencyOfPurchase_core(cost, currentRpS, deltaRpS) { - return cost.div(currentRpS).add(cost.div(deltaRpS)); - } - }, { - key: "slog_critical", - value: function slog_critical(base, height) { - //TODO: for bases above 10, revert to old linear approximation until I can think of something better - if (base > 10) { - return height - 1; - } - return Decimal.critical_section(base, height, critical_slog_values); + var layer = Math.floor(Math.random() * (maxLayers + 1)); + var randomexp = layer === 0 ? Math.random() * 616 - 308 : Math.random() * 16; //10% of the time, make it a simple power of 10 + + if (Math.random() > 0.9) { + randomexp = Math.trunc(randomexp); } - }, { - key: "tetrate_critical", - value: function tetrate_critical(base, height) { - return Decimal.critical_section(base, height, critical_tetr_values); + + var randommag = Math.pow(10, randomexp); //10% of the time, trunc mag + + if (Math.random() > 0.9) { + randommag = Math.trunc(randommag); } - }, { - key: "critical_section", - value: function critical_section(base, height, grid) { - //this part is simple at least, since it's just 0.1 to 0.9 - height *= 10; - if (height < 0) { - height = 0; - } + return FC(randomsign, layer, randommag); + } + }, { + key: "affordGeometricSeries_core", + value: function affordGeometricSeries_core(resourcesAvailable, priceStart, priceRatio, currentOwned) { + var actualStart = priceStart.mul(priceRatio.pow(currentOwned)); + return Decimal.floor(resourcesAvailable.div(actualStart).mul(priceRatio.sub(1)).add(1).log10().div(priceRatio.log10())); + } + }, { + key: "sumGeometricSeries_core", + value: function sumGeometricSeries_core(numItems, priceStart, priceRatio, currentOwned) { + return priceStart.mul(priceRatio.pow(currentOwned)).mul(Decimal.sub(1, priceRatio.pow(numItems))).div(Decimal.sub(1, priceRatio)); + } + }, { + key: "affordArithmeticSeries_core", + value: function affordArithmeticSeries_core(resourcesAvailable, priceStart, priceAdd, currentOwned) { + // n = (-(a-d/2) + sqrt((a-d/2)^2+2dS))/d + // where a is actualStart, d is priceAdd and S is resourcesAvailable + // then floor it and you're done! + var actualStart = priceStart.add(currentOwned.mul(priceAdd)); + var b = actualStart.sub(priceAdd.div(2)); + var b2 = b.pow(2); + return b.neg().add(b2.add(priceAdd.mul(resourcesAvailable).mul(2)).sqrt()).div(priceAdd).floor(); + } + }, { + key: "sumArithmeticSeries_core", + value: function sumArithmeticSeries_core(numItems, priceStart, priceAdd, currentOwned) { + var actualStart = priceStart.add(currentOwned.mul(priceAdd)); // (n/2)*(2*a+(n-1)*d) - if (height > 10) { - height = 10; - } //have to do this complicated song and dance since one of the critical_headers is Math.E, and in the future I'd like 1.5 as well + return numItems.div(2).mul(actualStart.mul(2).plus(numItems.sub(1).mul(priceAdd))); + } + }, { + key: "efficiencyOfPurchase_core", + value: function efficiencyOfPurchase_core(cost, currentRpS, deltaRpS) { + return cost.div(currentRpS).add(cost.div(deltaRpS)); + } + }, { + key: "slog_critical", + value: function slog_critical(base, height) { + //TODO: for bases above 10, revert to old linear approximation until I can think of something better + if (base > 10) { + return height - 1; + } + return Decimal.critical_section(base, height, critical_slog_values); + } + }, { + key: "tetrate_critical", + value: function tetrate_critical(base, height) { + return Decimal.critical_section(base, height, critical_tetr_values); + } + }, { + key: "critical_section", + value: function critical_section(base, height, grid) { + //this part is simple at least, since it's just 0.1 to 0.9 + height *= 10; - if (base < 2) { - base = 2; - } + if (height < 0) { + height = 0; + } - if (base > 10) { - base = 10; - } + if (height > 10) { + height = 10; + } //have to do this complicated song and dance since one of the critical_headers is Math.E, and in the future I'd like 1.5 as well - var lower = 0; - var upper = 0; //basically, if we're between bases, we interpolate each bases' relevant values together - //then we interpolate based on what the fractional height is. - //accuracy could be improved by doing a non-linear interpolation (maybe), by adding more bases and heights (definitely) but this is AFAIK the best you can get without running some pari.gp or mathematica program to calculate exact values - //however, do note http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html can do it for arbitrary heights but not for arbitrary bases (2, e, 10 present) - - for (var i = 0; i < critical_headers.length; ++i) { - if (critical_headers[i] == base) { - // exact match - lower = grid[i][Math.floor(height)]; - upper = grid[i][Math.ceil(height)]; - break; - } else if (critical_headers[i] < base && critical_headers[i + 1] > base) { - // interpolate between this and the next - var basefrac = (base - critical_headers[i]) / (critical_headers[i + 1] - critical_headers[i]); - lower = grid[i][Math.floor(height)] * (1 - basefrac) + grid[i + 1][Math.floor(height)] * basefrac; - upper = grid[i][Math.ceil(height)] * (1 - basefrac) + grid[i + 1][Math.ceil(height)] * basefrac; - break; - } - } - var frac = height - Math.floor(height); //improvement - you get more accuracy (especially around 0.9-1.0) by doing log, then frac, then powing the result - //(we could pre-log the lookup table, but then fractional bases would get Weird) - //also, use old linear for slog (values 0 or less in critical section). maybe something else is better but haven't thought about what yet + if (base < 2) { + base = 2; + } - if (lower <= 0 || upper <= 0) { - return lower * (1 - frac) + upper * frac; - } else { - return Math.pow(base, Math.log(lower) / Math.log(base) * (1 - frac) + Math.log(upper) / Math.log(base) * frac); - } + if (base > 10) { + base = 10; } - }]); - return Decimal; - }(); - Decimal.dZero = FC_NN(0, 0, 0); - Decimal.dOne = FC_NN(1, 0, 1); - Decimal.dNegOne = FC_NN(-1, 0, 1); - Decimal.dTwo = FC_NN(1, 0, 2); - Decimal.dTen = FC_NN(1, 0, 10); - Decimal.dNaN = FC_NN(Number.NaN, Number.NaN, Number.NaN); - Decimal.dInf = FC_NN(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY); - Decimal.dNegInf = FC_NN(-1, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY); - Decimal.dNumberMax = FC(1, 0, Number.MAX_VALUE); - Decimal.dNumberMin = FC(1, 0, Number.MIN_VALUE); - Decimal.fromStringCache = new LRUCache(DEFAULT_FROM_STRING_CACHE_SIZE); // return Decimal; - // Optimise Decimal aliases. - // We can't do this optimisation before Decimal is assigned. + var lower = 0; + var upper = 0; //basically, if we're between bases, we interpolate each bases' relevant values together + //then we interpolate based on what the fractional height is. + //accuracy could be improved by doing a non-linear interpolation (maybe), by adding more bases and heights (definitely) but this is AFAIK the best you can get without running some pari.gp or mathematica program to calculate exact values + //however, do note http://myweb.astate.edu/wpaulsen/tetcalc/tetcalc.html can do it for arbitrary heights but not for arbitrary bases (2, e, 10 present) - D = Decimal.fromValue_noAlloc; - FC = Decimal.fromComponents; - FC_NN = Decimal.fromComponents_noNormalize; // eslint-disable-next-line @typescript-eslint/no-unused-vars + for (var i = 0; i < critical_headers.length; ++i) { + if (critical_headers[i] == base) { + // exact match + lower = grid[i][Math.floor(height)]; + upper = grid[i][Math.ceil(height)]; + break; + } else if (critical_headers[i] < base && critical_headers[i + 1] > base) { + // interpolate between this and the next + var basefrac = (base - critical_headers[i]) / (critical_headers[i + 1] - critical_headers[i]); + lower = grid[i][Math.floor(height)] * (1 - basefrac) + grid[i + 1][Math.floor(height)] * basefrac; + upper = grid[i][Math.ceil(height)] * (1 - basefrac) + grid[i + 1][Math.ceil(height)] * basefrac; + break; + } + } - Decimal.fromMantissaExponent; // eslint-disable-next-line @typescript-eslint/no-unused-vars + var frac = height - Math.floor(height); //improvement - you get more accuracy (especially around 0.9-1.0) by doing log, then frac, then powing the result + //(we could pre-log the lookup table, but then fractional bases would get Weird) + //also, use old linear for slog (values 0 or less in critical section). maybe something else is better but haven't thought about what yet - Decimal.fromMantissaExponent_noNormalize; + if (lower <= 0 || upper <= 0) { + return lower * (1 - frac) + upper * frac; + } else { + return Math.pow(base, Math.log(lower) / Math.log(base) * (1 - frac) + Math.log(upper) / Math.log(base) * frac); + } + } + }]); return Decimal; - -})); +}(); +Decimal.dZero = FC_NN(0, 0, 0); +Decimal.dOne = FC_NN(1, 0, 1); +Decimal.dNegOne = FC_NN(-1, 0, 1); +Decimal.dTwo = FC_NN(1, 0, 2); +Decimal.dTen = FC_NN(1, 0, 10); +Decimal.dNaN = FC_NN(Number.NaN, Number.NaN, Number.NaN); +Decimal.dInf = FC_NN(1, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY); +Decimal.dNegInf = FC_NN(-1, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY); +Decimal.dNumberMax = FC(1, 0, Number.MAX_VALUE); +Decimal.dNumberMin = FC(1, 0, Number.MIN_VALUE); +Decimal.fromStringCache = new LRUCache(DEFAULT_FROM_STRING_CACHE_SIZE); // return Decimal; +// Optimise Decimal aliases. +// We can't do this optimisation before Decimal is assigned. + +D = Decimal.fromValue_noAlloc; +FC = Decimal.fromComponents; +FC_NN = Decimal.fromComponents_noNormalize; // eslint-disable-next-line @typescript-eslint/no-unused-vars + +Decimal.fromMantissaExponent; // eslint-disable-next-line @typescript-eslint/no-unused-vars + +Decimal.fromMantissaExponent_noNormalize; + +module.exports = Decimal; diff --git a/break_eternity.min.js b/break_eternity.min.js index 4840df8..852e182 100644 --- a/break_eternity.min.js +++ b/break_eternity.min.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Decimal=e()}(this,(function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var i=0;ithis.maxSize;){var r=this.last;this.map.delete(r.key),this.last=r.prev,this.last.next=void 0}}}}]),e}(),n=i((function e(i,r){t(this,e),this.next=void 0,this.prev=void 0,this.key=i,this.value=r})),a=Math.log10(9e15),s=function(){for(var t=[],e=-323;e<=308;e++)t.push(Number("1e"+e));return function(e){return t[e+323]}}(),u=[2,Math.E,3,4,5,6,7,8,9,10],h=[[1,1.0891180521811203,1.1789767925673957,1.2701455431742086,1.3632090180450092,1.4587818160364217,1.5575237916251419,1.6601571006859253,1.767485818836978,1.8804192098842727,2],[1,1.1121114330934079,1.231038924931609,1.3583836963111375,1.4960519303993531,1.6463542337511945,1.8121385357018724,1.996971324618307,2.2053895545527546,2.4432574483385254,Math.E],[1,1.1187738849693603,1.2464963939368214,1.38527004705667,1.5376664685821402,1.7068895236551784,1.897001227148399,2.1132403089001035,2.362480153784171,2.6539010333870774,3],[1,1.1367350847096405,1.2889510672956703,1.4606478703324786,1.6570295196661111,1.8850062585672889,2.1539465047453485,2.476829779693097,2.872061932789197,3.3664204535587183,4],[1,1.1494592900767588,1.319708228183931,1.5166291280087583,1.748171114438024,2.0253263297298045,2.3636668498288547,2.7858359149579424,3.3257226212448145,4.035730287722532,5],[1,1.159225940787673,1.343712473580932,1.5611293155111927,1.8221199554561318,2.14183924486326,2.542468319282638,3.0574682501653316,3.7390572020926873,4.6719550537360774,6],[1,1.1670905356972596,1.3632807444991446,1.5979222279405536,1.8842640123816674,2.2416069644878687,2.69893426559423,3.3012632110403577,4.121250340630164,5.281493033448316,7],[1,1.1736630594087796,1.379783782386201,1.6292821855668218,1.9378971836180754,2.3289975651071977,2.8384347394720835,3.5232708454565906,4.478242031114584,5.868592169644505,8],[1,1.1793017514670474,1.394054150657457,1.65664127441059,1.985170999970283,2.4069682290577457,2.9647310119960752,3.7278665320924946,4.814462547283592,6.436522247411611,9],[1,1.1840100246247336,1.4061375836156955,1.6802272208863964,2.026757028388619,2.4770056063449646,3.080525271755482,3.9191964192627284,5.135152840833187,6.989961179534715,10]],o=[[-1,-.9194161097107025,-.8335625019330468,-.7425599821143978,-.6466611521029437,-.5462617907227869,-.4419033816638769,-.3342645487554494,-.224140440909962,-.11241087890006762,0],[-1,-.90603157029014,-.80786507256596,-.7064666939634,-.60294836853664,-.49849837513117,-.39430303318768,-.29147201034755,-.19097820800866,-.09361896280296,0],[-1,-.9021579584316141,-.8005762598234203,-.6964780623319391,-.5911906810998454,-.486050182576545,-.3823089430815083,-.28106046722897615,-.1831906535795894,-.08935809204418144,0],[-1,-.8917227442365535,-.781258746326964,-.6705130326902455,-.5612813129406509,-.4551067709033134,-.35319256652135966,-.2563741554088552,-.1651412821106526,-.0796919581982668,0],[-1,-.8843387974366064,-.7678744063886243,-.6529563724510552,-.5415870994657841,-.4352842206588936,-.33504449124791424,-.24138853420685147,-.15445285440944467,-.07409659641336663,0],[-1,-.8786709358426346,-.7577735191184886,-.6399546189952064,-.527284921869926,-.4211627631006314,-.3223479611761232,-.23107655627789858,-.1472057700818259,-.07035171210706326,0],[-1,-.8740862815291583,-.7497032990976209,-.6297119746181752,-.5161838335958787,-.41036238255751956,-.31277212146489963,-.2233976621705518,-.1418697367979619,-.06762117662323441,0],[-1,-.8702632331800649,-.7430366914122081,-.6213373075161548,-.5072025698095242,-.40171437727184167,-.30517930701410456,-.21736343968190863,-.137710238299109,-.06550774483471955,0],[-1,-.8670016295947213,-.7373984232432306,-.6143173985094293,-.49973884395492807,-.394584953527678,-.2989649949848695,-.21245647317021688,-.13434688362382652,-.0638072667348083,0],[-1,-.8641642839543857,-.732534623168535,-.6083127477059322,-.4934049257184696,-.3885773075899922,-.29376029055315767,-.2083678561173622,-.13155653399373268,-.062401588652553186,0]],l=function(t){return k.fromValue_noAlloc(t)},m=function(t,e,i){return k.fromComponents(t,e,i)},g=function(t,e,i){return k.fromComponents_noNormalize(t,e,i)},f=function(t,e){var i=e+1,r=Math.ceil(Math.log10(Math.abs(t))),n=Math.round(t*Math.pow(10,i-r))*Math.pow(10,r-i);return parseFloat(n.toFixed(Math.max(i-r,0)))},c=function(t){return Math.sign(t)*Math.log10(Math.abs(t))},y=.5671432904097838,v=function(t){var e,i,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1e-10;if(!Number.isFinite(t))return t;if(0===t)return t;if(1===t)return y;e=t<10?0:Math.log(t)-Math.log(Math.log(t));for(var n=0;n<100;++n){if(i=(t*Math.exp(-e)+e*e)/(e+1),Math.abs(i-e)1&&void 0!==arguments[1]?arguments[1]:1e-10;if(!Number.isFinite(t.mag))return t;if(t.eq(k.dZero))return t;if(t.eq(k.dOne))return k.fromNumber(y);e=k.ln(t);for(var s=0;s<100;++s){if(i=e.neg().exp(),r=e.sub(t.mul(i)),n=e.sub(r.div(e.add(1).sub(e.add(2).mul(r).div(k.mul(2,e).add(2))))),k.abs(n.sub(e)).lt(k.abs(n).mul(a)))return n;e=n}throw Error("Iteration failed to converge: ".concat(t.toString()))}var k=function(){function e(i){t(this,e),this.sign=0,this.mag=0,this.layer=0,i instanceof e?this.fromDecimal(i):"number"==typeof i?this.fromNumber(i):"string"==typeof i&&this.fromString(i)}return i(e,[{key:"m",get:function(){if(0===this.sign)return 0;if(0===this.layer){var t,e=Math.floor(Math.log10(this.mag));return t=5e-324===this.mag?5:this.mag/s(e),this.sign*t}if(1===this.layer){var i=this.mag-Math.floor(this.mag);return this.sign*Math.pow(10,i)}return this.sign},set:function(t){this.layer<=2?this.fromMantissaExponent(t,this.e):(this.sign=Math.sign(t),0===this.sign&&(this.layer=0,this.exponent=0))}},{key:"e",get:function(){return 0===this.sign?0:0===this.layer?Math.floor(Math.log10(this.mag)):1===this.layer?Math.floor(this.mag):2===this.layer?Math.floor(Math.sign(this.mag)*Math.pow(10,Math.abs(this.mag))):this.mag*Number.POSITIVE_INFINITY},set:function(t){this.fromMantissaExponent(this.m,t)}},{key:"s",get:function(){return this.sign},set:function(t){0===t?(this.sign=0,this.layer=0,this.mag=0):this.sign=t}},{key:"mantissa",get:function(){return this.m},set:function(t){this.m=t}},{key:"exponent",get:function(){return this.e},set:function(t){this.e=t}},{key:"normalize",value:function(){if(0===this.sign||0===this.mag&&0===this.layer)return this.sign=0,this.mag=0,this.layer=0,this;if(0===this.layer&&this.mag<0&&(this.mag=-this.mag,this.sign=-this.sign),0===this.layer&&this.mag<1/9e15)return this.layer+=1,this.mag=Math.log10(this.mag),this;var t=Math.abs(this.mag),e=Math.sign(this.mag);if(t>=9e15)return this.layer+=1,this.mag=e*Math.log10(t),this;for(;t0;)this.layer-=1,0===this.layer?this.mag=Math.pow(10,this.mag):(this.mag=e*Math.pow(10,t),t=Math.abs(this.mag),e=Math.sign(this.mag));return 0===this.layer&&(this.mag<0?(this.mag=-this.mag,this.sign=-this.sign):0===this.mag&&(this.sign=0)),this}},{key:"fromComponents",value:function(t,e,i){return this.sign=t,this.layer=e,this.mag=i,this.normalize(),this}},{key:"fromComponents_noNormalize",value:function(t,e,i){return this.sign=t,this.layer=e,this.mag=i,this}},{key:"fromMantissaExponent",value:function(t,e){return this.layer=1,this.sign=Math.sign(t),t=Math.abs(t),this.mag=e+Math.log10(t),this.normalize(),this}},{key:"fromMantissaExponent_noNormalize",value:function(t,e){return this.fromMantissaExponent(t,e),this}},{key:"fromDecimal",value:function(t){return this.sign=t.sign,this.layer=t.layer,this.mag=t.mag,this}},{key:"fromNumber",value:function(t){return this.mag=Math.abs(t),this.sign=Math.sign(t),this.layer=0,this.normalize(),this}},{key:"fromString",value:function(t){var i=t,r=e.fromStringCache.get(i);if(void 0!==r)return this.fromDecimal(r);var n=(t=t.replace(",","")).split("^^^");if(2===n.length){var a=parseFloat(n[0]),s=parseFloat(n[1]),u=n[1].split(";"),h=1;if(2===u.length&&(h=parseFloat(u[1]),isFinite(h)||(h=1)),isFinite(a)&&isFinite(s)){var o=e.pentate(a,s,h);return this.sign=o.sign,this.layer=o.layer,this.mag=o.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this}}var g=t.split("^^");if(2===g.length){var f=parseFloat(g[0]),y=parseFloat(g[1]),v=g[1].split(";"),d=1;if(2===v.length&&(d=parseFloat(v[1]),isFinite(d)||(d=1)),isFinite(f)&&isFinite(y)){var k=e.tetrate(f,y,d);return this.sign=k.sign,this.layer=k.layer,this.mag=k.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this}}var p,N,b=t.split("^");if(2===b.length){var M=parseFloat(b[0]),_=parseFloat(b[1]);if(isFinite(M)&&isFinite(_)){var x=e.pow(M,_);return this.sign=x.sign,this.layer=x.layer,this.mag=x.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this}}var F=(t=t.trim().toLowerCase()).split("pt");if(2===F.length){p=10,N=parseFloat(F[0]),F[1]=F[1].replace("(",""),F[1]=F[1].replace(")","");var S=parseFloat(F[1]);if(isFinite(S)||(S=1),isFinite(p)&&isFinite(N)){var w=e.tetrate(p,N,S);return this.sign=w.sign,this.layer=w.layer,this.mag=w.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this}}if(2===(F=t.split("p")).length){p=10,N=parseFloat(F[0]),F[1]=F[1].replace("(",""),F[1]=F[1].replace(")","");var q=parseFloat(F[1]);if(isFinite(q)||(q=1),isFinite(p)&&isFinite(N)){var I=e.tetrate(p,N,q);return this.sign=I.sign,this.layer=I.layer,this.mag=I.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this}}var E=t.split("e"),C=E.length-1;if(0===C){var z=parseFloat(t);if(isFinite(z))return this.fromNumber(z),e.fromStringCache.size>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this}else if(1===C){var T=parseFloat(t);if(isFinite(T)&&0!==T)return this.fromNumber(T),e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this}var O=t.split("e^");if(2===O.length){this.sign=1,"-"==O[0].charAt(0)&&(this.sign=-1);for(var D="",V=0;V=43&&A<=57||101===A))return this.layer=parseFloat(D),this.mag=parseFloat(O[1].substr(V+1)),this.normalize(),e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this;D+=O[1].charAt(V)}}if(C<1)return this.sign=0,this.layer=0,this.mag=0,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this;var P=parseFloat(E[0]);if(0===P)return this.sign=0,this.layer=0,this.mag=0,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this;var Z=parseFloat(E[E.length-1]);if(C>=2){var Y=parseFloat(E[E.length-2]);isFinite(Y)&&(Z*=Math.sign(Y),Z+=c(Y))}if(isFinite(P))if(1===C)this.sign=Math.sign(P),this.layer=1,this.mag=Z+Math.log10(Math.abs(P));else{if(this.sign=Math.sign(P),this.layer=C,2===C){var G=e.mul(m(1,2,Z),l(P));return this.sign=G.sign,this.layer=G.layer,this.mag=G.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this}this.mag=Z}else this.sign="-"===E[0]?-1:1,this.layer=C,this.mag=Z;return this.normalize(),e.fromStringCache.maxSize>=1&&e.fromStringCache.set(i,e.fromDecimal(this)),this}},{key:"fromValue",value:function(t){return t instanceof e?this.fromDecimal(t):"number"==typeof t?this.fromNumber(t):"string"==typeof t?this.fromString(t):(this.sign=0,this.layer=0,this.mag=0,this)}},{key:"toNumber",value:function(){return Number.isFinite(this.layer)?0===this.layer?this.sign*this.mag:1===this.layer?this.sign*Math.pow(10,this.mag):this.mag>0?this.sign>0?Number.POSITIVE_INFINITY:Number.NEGATIVE_INFINITY:0:Number.NaN}},{key:"mantissaWithDecimalPlaces",value:function(t){return isNaN(this.m)?Number.NaN:0===this.m?0:f(this.m,t)}},{key:"magnitudeWithDecimalPlaces",value:function(t){return isNaN(this.mag)?Number.NaN:0===this.mag?0:f(this.mag,t)}},{key:"toString",value:function(){return isNaN(this.layer)||isNaN(this.sign)||isNaN(this.mag)?"NaN":this.mag===Number.POSITIVE_INFINITY||this.layer===Number.POSITIVE_INFINITY?1===this.sign?"Infinity":"-Infinity":0===this.layer?this.mag<1e21&&this.mag>1e-7||0===this.mag?(this.sign*this.mag).toString():this.m+"e"+this.e:1===this.layer?this.m+"e"+this.e:this.layer<=5?(-1===this.sign?"-":"")+"e".repeat(this.layer)+this.mag:(-1===this.sign?"-":"")+"(e^"+this.layer+")"+this.mag}},{key:"toExponential",value:function(t){return 0===this.layer?(this.sign*this.mag).toExponential(t):this.toStringWithDecimalPlaces(t)}},{key:"toFixed",value:function(t){return 0===this.layer?(this.sign*this.mag).toFixed(t):this.toStringWithDecimalPlaces(t)}},{key:"toPrecision",value:function(t){return this.e<=-7?this.toExponential(t-1):t>this.e?this.toFixed(t-this.exponent-1):this.toExponential(t-1)}},{key:"valueOf",value:function(){return this.toString()}},{key:"toJSON",value:function(){return this.toString()}},{key:"toStringWithDecimalPlaces",value:function(t){return 0===this.layer?this.mag<1e21&&this.mag>1e-7||0===this.mag?(this.sign*this.mag).toFixed(t):f(this.m,t)+"e"+f(this.e,t):1===this.layer?f(this.m,t)+"e"+f(this.e,t):this.layer<=5?(-1===this.sign?"-":"")+"e".repeat(this.layer)+f(this.mag,t):(-1===this.sign?"-":"")+"(e^"+this.layer+")"+f(this.mag,t)}},{key:"abs",value:function(){return g(0===this.sign?0:1,this.layer,this.mag)}},{key:"neg",value:function(){return g(-this.sign,this.layer,this.mag)}},{key:"negate",value:function(){return this.neg()}},{key:"negated",value:function(){return this.neg()}},{key:"sgn",value:function(){return this.sign}},{key:"round",value:function(){return this.mag<0?e.dZero:0===this.layer?m(this.sign,0,Math.round(this.mag)):this}},{key:"floor",value:function(){return this.mag<0?e.dZero:0===this.layer?m(this.sign,0,Math.floor(this.mag)):this}},{key:"ceil",value:function(){return this.mag<0?e.dZero:0===this.layer?m(this.sign,0,Math.ceil(this.mag)):this}},{key:"trunc",value:function(){return this.mag<0?e.dZero:0===this.layer?m(this.sign,0,Math.trunc(this.mag)):this}},{key:"add",value:function(t){var i,r,n=l(t);if(!Number.isFinite(this.layer))return this;if(!Number.isFinite(n.layer))return n;if(0===this.sign)return n;if(0===n.sign)return this;if(this.sign===-n.sign&&this.layer===n.layer&&this.mag===n.mag)return g(0,0,0);if(this.layer>=2||n.layer>=2)return this.maxabs(n);if(e.cmpabs(this,n)>0?(i=this,r=n):(i=n,r=this),0===i.layer&&0===r.layer)return e.fromNumber(i.sign*i.mag+r.sign*r.mag);var a=i.layer*Math.sign(i.mag),s=r.layer*Math.sign(r.mag);if(a-s>=2)return i;if(0===a&&-1===s){if(Math.abs(r.mag-Math.log10(i.mag))>17)return i;var u=Math.pow(10,Math.log10(i.mag)-r.mag),h=r.sign+i.sign*u;return m(Math.sign(h),1,r.mag+Math.log10(Math.abs(h)))}if(1===a&&0===s){if(Math.abs(i.mag-Math.log10(r.mag))>17)return i;var o=Math.pow(10,i.mag-Math.log10(r.mag)),f=r.sign+i.sign*o;return m(Math.sign(f),1,Math.log10(r.mag)+Math.log10(Math.abs(f)))}if(Math.abs(i.mag-r.mag)>17)return i;var c=Math.pow(10,i.mag-r.mag),y=r.sign+i.sign*c;return m(Math.sign(y),1,r.mag+Math.log10(Math.abs(y)))}},{key:"plus",value:function(t){return this.add(t)}},{key:"sub",value:function(t){return this.add(l(t).neg())}},{key:"subtract",value:function(t){return this.sub(t)}},{key:"minus",value:function(t){return this.sub(t)}},{key:"mul",value:function(t){var i,r,n=l(t);if(!Number.isFinite(this.layer))return this;if(!Number.isFinite(n.layer))return n;if(0===this.sign||0===n.sign)return g(0,0,0);if(this.layer===n.layer&&this.mag===-n.mag)return g(this.sign*n.sign,0,1);if(this.layer>n.layer||this.layer==n.layer&&Math.abs(this.mag)>Math.abs(n.mag)?(i=this,r=n):(i=n,r=this),0===i.layer&&0===r.layer)return e.fromNumber(i.sign*r.sign*i.mag*r.mag);if(i.layer>=3||i.layer-r.layer>=2)return m(i.sign*r.sign,i.layer,i.mag);if(1===i.layer&&0===r.layer)return m(i.sign*r.sign,1,i.mag+Math.log10(r.mag));if(1===i.layer&&1===r.layer)return m(i.sign*r.sign,1,i.mag+r.mag);if(2===i.layer&&1===r.layer){var a=m(Math.sign(i.mag),i.layer-1,Math.abs(i.mag)).add(m(Math.sign(r.mag),r.layer-1,Math.abs(r.mag)));return m(i.sign*r.sign,a.layer+1,a.sign*a.mag)}if(2===i.layer&&2===r.layer){var s=m(Math.sign(i.mag),i.layer-1,Math.abs(i.mag)).add(m(Math.sign(r.mag),r.layer-1,Math.abs(r.mag)));return m(i.sign*r.sign,s.layer+1,s.sign*s.mag)}throw Error("Bad arguments to mul: "+this+", "+t)}},{key:"multiply",value:function(t){return this.mul(t)}},{key:"times",value:function(t){return this.mul(t)}},{key:"div",value:function(t){var e=l(t);return this.mul(e.recip())}},{key:"divide",value:function(t){return this.div(t)}},{key:"divideBy",value:function(t){return this.div(t)}},{key:"dividedBy",value:function(t){return this.div(t)}},{key:"recip",value:function(){return 0===this.mag?e.dNaN:0===this.layer?m(this.sign,0,1/this.mag):m(this.sign,this.layer,-this.mag)}},{key:"reciprocal",value:function(){return this.recip()}},{key:"reciprocate",value:function(){return this.recip()}},{key:"cmp",value:function(t){var e=l(t);return this.sign>e.sign?1:this.sign0?this.layer:-this.layer,r=e.mag>0?e.layer:-e.layer;return i>r?1:ie.mag?1:this.mag0?e:this}},{key:"clamp",value:function(t,e){return this.max(t).min(e)}},{key:"clampMin",value:function(t){return this.max(t)}},{key:"clampMax",value:function(t){return this.min(t)}},{key:"cmp_tolerance",value:function(t,e){var i=l(t);return this.eq_tolerance(i,e)?0:this.cmp(i)}},{key:"compare_tolerance",value:function(t,e){return this.cmp_tolerance(t,e)}},{key:"eq_tolerance",value:function(t,e){var i=l(t);if(null==e&&(e=1e-7),this.sign!==i.sign)return!1;if(Math.abs(this.layer-i.layer)>1)return!1;var r=this.mag,n=i.mag;return this.layer>i.layer&&(n=c(n)),this.layer0?m(Math.sign(this.mag),this.layer-1,Math.abs(this.mag)):m(1,0,Math.log10(this.mag))}},{key:"log10",value:function(){return this.sign<=0?e.dNaN:this.layer>0?m(Math.sign(this.mag),this.layer-1,Math.abs(this.mag)):m(this.sign,0,Math.log10(this.mag))}},{key:"log",value:function(t){return t=l(t),this.sign<=0||t.sign<=0||1===t.sign&&0===t.layer&&1===t.mag?e.dNaN:0===this.layer&&0===t.layer?m(this.sign,0,Math.log(this.mag)/Math.log(t.mag)):e.div(this.log10(),t.log10())}},{key:"log2",value:function(){return this.sign<=0?e.dNaN:0===this.layer?m(this.sign,0,Math.log2(this.mag)):1===this.layer?m(Math.sign(this.mag),0,3.321928094887362*Math.abs(this.mag)):2===this.layer?m(Math.sign(this.mag),1,Math.abs(this.mag)+.5213902276543247):m(Math.sign(this.mag),this.layer-1,Math.abs(this.mag))}},{key:"ln",value:function(){return this.sign<=0?e.dNaN:0===this.layer?m(this.sign,0,Math.log(this.mag)):1===this.layer?m(Math.sign(this.mag),0,2.302585092994046*Math.abs(this.mag)):2===this.layer?m(Math.sign(this.mag),1,Math.abs(this.mag)+.36221568869946325):m(Math.sign(this.mag),this.layer-1,Math.abs(this.mag))}},{key:"logarithm",value:function(t){return this.log(t)}},{key:"pow",value:function(t){var i=this,r=l(t);if(0===i.sign)return r.eq(0)?g(1,0,1):i;if(1===i.sign&&0===i.layer&&1===i.mag)return i;if(0===r.sign)return g(1,0,1);if(1===r.sign&&0===r.layer&&1===r.mag)return i;var n=i.absLog10().mul(r).pow10();return-1===this.sign?Math.abs(r.toNumber()%2)%2==1?n.neg():Math.abs(r.toNumber()%2)%2==0?n:e.dNaN:n}},{key:"pow10",value:function(){if(!Number.isFinite(this.layer)||!Number.isFinite(this.mag))return e.dNaN;var t=this;if(0===t.layer){var i=Math.pow(10,t.sign*t.mag);if(Number.isFinite(i)&&Math.abs(i)>=.1)return m(1,0,i);if(0===t.sign)return e.dOne;t=g(t.sign,t.layer+1,Math.log10(t.mag))}return t.sign>0&&t.mag>=0?m(t.sign,t.layer+1,t.mag):t.sign<0&&t.mag>=0?m(-t.sign,t.layer+1,-t.mag):e.dOne}},{key:"pow_base",value:function(t){return l(t).pow(this)}},{key:"root",value:function(t){var e=l(t);return this.pow(e.recip())}},{key:"factorial",value:function(){return this.mag<0||0===this.layer?this.add(1).gamma():1===this.layer?e.exp(e.mul(this,e.ln(this).sub(1))):e.exp(this)}},{key:"gamma",value:function(){if(this.mag<0)return this.recip();if(0===this.layer){if(this.lt(g(1,0,24)))return e.fromNumber(function(t){if(!isFinite(t))return t;if(t<-50)return t===Math.trunc(t)?Number.NEGATIVE_INFINITY:0;for(var e=1;t<10;)e*=t,++t;var i=.9189385332046727;i+=((t-=1)+.5)*Math.log(t),i-=t;var r=t*t,n=t;return i+=1/(12*n),i+=1/(360*(n*=r)),i+=1/(1260*(n*=r)),i+=1/(1680*(n*=r)),i+=1/(1188*(n*=r)),i+=691/(360360*(n*=r)),i+=7/(1092*(n*=r)),i+=3617/(122400*(n*=r)),Math.exp(i)/e}(this.sign*this.mag));var t=this.mag-1,i=.9189385332046727;i+=(t+.5)*Math.log(t);var r=t*t,n=t,a=12*n,s=1/a,u=(i-=t)+s;if(u===i)return e.exp(i);if((u=(i=u)-(s=1/(a=360*(n*=r))))===i)return e.exp(i);i=u;var h=1/(a=1260*(n*=r));return i+=h,i-=h=1/(a=1680*(n*=r)),e.exp(i)}return 1===this.layer?e.exp(e.mul(this,e.ln(this).sub(1))):e.exp(this)}},{key:"lngamma",value:function(){return this.gamma().ln()}},{key:"exp",value:function(){return this.mag<0?e.dOne:0===this.layer&&this.mag<=709.7?e.fromNumber(Math.exp(this.sign*this.mag)):0===this.layer?m(1,1,this.sign*Math.log10(Math.E)*this.mag):1===this.layer?m(1,2,this.sign*(Math.log10(.4342944819032518)+this.mag)):m(1,this.layer+1,this.sign*this.mag)}},{key:"sqr",value:function(){return this.pow(2)}},{key:"sqrt",value:function(){if(0===this.layer)return e.fromNumber(Math.sqrt(this.sign*this.mag));if(1===this.layer)return m(1,2,Math.log10(this.mag)-.3010299956639812);var t=e.div(g(this.sign,this.layer-1,this.mag),g(1,0,2));return t.layer+=1,t.normalize(),t}},{key:"cube",value:function(){return this.pow(3)}},{key:"cbrt",value:function(){return this.pow(1/3)}},{key:"tetrate",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:2,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:g(1,0,1);if(1===t)return e.pow(this,i);if(0===t)return new e(i);if(this.eq(e.dOne))return e.dOne;if(this.eq(-1))return e.pow(this,i);if(t===Number.POSITIVE_INFINITY){var r=this.toNumber();if(r<=1.444667861009766&&r>=.06598803584531254){if(r>1.444667861009099)return e.fromNumber(Math.E);var n=e.ln(this).neg();return n.lambertw().div(n)}return r>1.444667861009766?e.fromNumber(Number.POSITIVE_INFINITY):e.dNaN}if(this.eq(e.dZero)){var a=Math.abs((t+1)%2);return a>1&&(a=2-a),e.fromNumber(a)}if(t<0)return e.iteratedlog(i,this,-t);i=l(i);var s=t,u=s-(t=Math.trunc(t));if(this.gt(e.dZero)&&this.lte(1.444667861009766)){t=Math.min(1e4,t);for(var h=0;h3)return g(i.sign,i.layer+(t-f-1),i.mag);if(f>1e4)return i}return i}},{key:"iteratedexp",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:2,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:g(1,0,1);return this.tetrate(t,e)}},{key:"iteratedlog",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:10,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1;if(i<0)return e.tetrate(t,-i,this);t=l(t);var r=e.fromDecimal(this),n=i,a=n-(i=Math.trunc(i));if(r.layer-t.layer>3){var s=Math.min(i,r.layer-t.layer-3);i-=s,r.layer-=s}for(var u=0;u1e4)return r}return a>0&&a<1&&(r=t.eq(10)?r.layeradd10(-a):r.layeradd(-a,t)),r}},{key:"slog",value:function(){for(var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:10,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:100,r=.001,n=!1,a=!1,s=this.slog_internal(t).toNumber(),u=1;u1&&a!=o&&(n=!0),a=o,n?r/=2:r*=2,s+=r=Math.abs(r)*(o?-1:1),0===r)break}return e.fromNumber(s)}},{key:"slog_internal",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:10;if((t=l(t)).lte(e.dZero))return e.dNaN;if(t.eq(e.dOne))return e.dNaN;if(t.lt(e.dOne))return this.eq(e.dOne)?e.dZero:this.eq(e.dZero)?e.dNegOne:e.dNaN;if(this.mag<0||this.eq(e.dZero))return e.dNegOne;var i=0,r=e.fromDecimal(this);if(r.layer-t.layer>3){var n=r.layer-t.layer-3;i+=n,r.layer-=n}for(var a=0;a<100;++a)if(r.lt(e.dZero))r=e.pow(t,r),i-=1;else{if(r.lte(e.dOne))return e.fromNumber(i+e.slog_critical(t.toNumber(),r.toNumber()));i+=1,r=e.log(r,t)}return e.fromNumber(i)}},{key:"layeradd10",value:function(t){t=e.fromValue_noAlloc(t).toNumber();var i=e.fromDecimal(this);if(t>=1){i.mag<0&&i.layer>0?(i.sign=0,i.mag=0,i.layer=0):-1===i.sign&&0==i.layer&&(i.sign=1,i.mag=-i.mag);var r=Math.trunc(t);t-=r,i.layer+=r}if(t<=-1){var n=Math.trunc(t);if(t-=n,i.layer+=n,i.layer<0)for(var a=0;a<100;++a){if(i.layer++,i.mag=Math.log10(i.mag),!isFinite(i.mag))return 0===i.sign&&(i.sign=1),i.layer<0&&(i.layer=0),i.normalize();if(i.layer>=0)break}}for(;i.layer<0;)i.layer++,i.mag=Math.log10(i.mag);return 0===i.sign&&(i.sign=1,0===i.mag&&i.layer>=1&&(i.layer-=1,i.mag=1)),i.normalize(),0!==t?i.layeradd(t,10):i}},{key:"layeradd",value:function(t,i){var r=this.slog(i).toNumber()+t;return r>=0?e.tetrate(i,r):Number.isFinite(r)?r>=-1?e.log(e.tetrate(i,r+1),i):e.log(e.log(e.tetrate(i,r+2),i),i):e.dNaN}},{key:"lambertw",value:function(){if(this.lt(-.3678794411710499))throw Error("lambertw is unimplemented for results less than -1, sorry!");if(this.mag<0)return e.fromNumber(v(this.toNumber()));if(0===this.layer)return e.fromNumber(v(this.sign*this.mag));if(1===this.layer)return d(this);if(2===this.layer)return d(this);if(this.layer>=3)return g(this.sign,this.layer-1,this.mag);throw"Unhandled behavior in lambertw()"}},{key:"ssqrt",value:function(){if(1==this.sign&&this.layer>=3)return g(this.sign,this.layer-1,this.mag);var t=this.ln();return t.div(t.lambertw())}},{key:"pentate",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:2,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:g(1,0,1);i=l(i);var r=t,n=r-(t=Math.trunc(t));0!==n&&(i.eq(e.dOne)?(++t,i=e.fromNumber(n)):i=this.eq(10)?i.layeradd10(n):i.layeradd(n,this));for(var a=0;a10)return i}return i}},{key:"sin",value:function(){return this.mag<0?this:0===this.layer?e.fromNumber(Math.sin(this.sign*this.mag)):g(0,0,0)}},{key:"cos",value:function(){return this.mag<0?e.dOne:0===this.layer?e.fromNumber(Math.cos(this.sign*this.mag)):g(0,0,0)}},{key:"tan",value:function(){return this.mag<0?this:0===this.layer?e.fromNumber(Math.tan(this.sign*this.mag)):g(0,0,0)}},{key:"asin",value:function(){return this.mag<0?this:0===this.layer?e.fromNumber(Math.asin(this.sign*this.mag)):g(Number.NaN,Number.NaN,Number.NaN)}},{key:"acos",value:function(){return this.mag<0?e.fromNumber(Math.acos(this.toNumber())):0===this.layer?e.fromNumber(Math.acos(this.sign*this.mag)):g(Number.NaN,Number.NaN,Number.NaN)}},{key:"atan",value:function(){return this.mag<0?this:0===this.layer?e.fromNumber(Math.atan(this.sign*this.mag)):e.fromNumber(Math.atan(Infinity*this.sign))}},{key:"sinh",value:function(){return this.exp().sub(this.negate().exp()).div(2)}},{key:"cosh",value:function(){return this.exp().add(this.negate().exp()).div(2)}},{key:"tanh",value:function(){return this.sinh().div(this.cosh())}},{key:"asinh",value:function(){return e.ln(this.add(this.sqr().add(1).sqrt()))}},{key:"acosh",value:function(){return e.ln(this.add(this.sqr().sub(1).sqrt()))}},{key:"atanh",value:function(){return this.abs().gte(1)?g(Number.NaN,Number.NaN,Number.NaN):e.ln(this.add(1).div(e.fromNumber(1).sub(this))).div(2)}},{key:"ascensionPenalty",value:function(t){return 0===t?this:this.root(e.pow(10,t))}},{key:"egg",value:function(){return this.add(9)}},{key:"lessThanOrEqualTo",value:function(t){return this.cmp(t)<1}},{key:"lessThan",value:function(t){return this.cmp(t)<0}},{key:"greaterThanOrEqualTo",value:function(t){return this.cmp(t)>-1}},{key:"greaterThan",value:function(t){return this.cmp(t)>0}}],[{key:"fromComponents",value:function(t,i,r){return(new e).fromComponents(t,i,r)}},{key:"fromComponents_noNormalize",value:function(t,i,r){return(new e).fromComponents_noNormalize(t,i,r)}},{key:"fromMantissaExponent",value:function(t,i){return(new e).fromMantissaExponent(t,i)}},{key:"fromMantissaExponent_noNormalize",value:function(t,i){return(new e).fromMantissaExponent_noNormalize(t,i)}},{key:"fromDecimal",value:function(t){return(new e).fromDecimal(t)}},{key:"fromNumber",value:function(t){return(new e).fromNumber(t)}},{key:"fromString",value:function(t){return(new e).fromString(t)}},{key:"fromValue",value:function(t){return(new e).fromValue(t)}},{key:"fromValue_noAlloc",value:function(t){if(t instanceof e)return t;if("string"==typeof t){var i=e.fromStringCache.get(t);return void 0!==i?i:e.fromString(t)}return"number"==typeof t?e.fromNumber(t):e.dZero}},{key:"abs",value:function(t){return l(t).abs()}},{key:"neg",value:function(t){return l(t).neg()}},{key:"negate",value:function(t){return l(t).neg()}},{key:"negated",value:function(t){return l(t).neg()}},{key:"sign",value:function(t){return l(t).sign}},{key:"sgn",value:function(t){return l(t).sign}},{key:"round",value:function(t){return l(t).round()}},{key:"floor",value:function(t){return l(t).floor()}},{key:"ceil",value:function(t){return l(t).ceil()}},{key:"trunc",value:function(t){return l(t).trunc()}},{key:"add",value:function(t,e){return l(t).add(e)}},{key:"plus",value:function(t,e){return l(t).add(e)}},{key:"sub",value:function(t,e){return l(t).sub(e)}},{key:"subtract",value:function(t,e){return l(t).sub(e)}},{key:"minus",value:function(t,e){return l(t).sub(e)}},{key:"mul",value:function(t,e){return l(t).mul(e)}},{key:"multiply",value:function(t,e){return l(t).mul(e)}},{key:"times",value:function(t,e){return l(t).mul(e)}},{key:"div",value:function(t,e){return l(t).div(e)}},{key:"divide",value:function(t,e){return l(t).div(e)}},{key:"recip",value:function(t){return l(t).recip()}},{key:"reciprocal",value:function(t){return l(t).recip()}},{key:"reciprocate",value:function(t){return l(t).reciprocate()}},{key:"cmp",value:function(t,e){return l(t).cmp(e)}},{key:"cmpabs",value:function(t,e){return l(t).cmpabs(e)}},{key:"compare",value:function(t,e){return l(t).cmp(e)}},{key:"isNaN",value:function(t){function e(e){return t.apply(this,arguments)}return e.toString=function(){return t.toString()},e}((function(t){return t=l(t),isNaN(t.sign)||isNaN(t.layer)||isNaN(t.mag)}))},{key:"isFinite",value:function(t){function e(e){return t.apply(this,arguments)}return e.toString=function(){return t.toString()},e}((function(t){return t=l(t),isFinite(t.sign)&&isFinite(t.layer)&&isFinite(t.mag)}))},{key:"eq",value:function(t,e){return l(t).eq(e)}},{key:"equals",value:function(t,e){return l(t).eq(e)}},{key:"neq",value:function(t,e){return l(t).neq(e)}},{key:"notEquals",value:function(t,e){return l(t).notEquals(e)}},{key:"lt",value:function(t,e){return l(t).lt(e)}},{key:"lte",value:function(t,e){return l(t).lte(e)}},{key:"gt",value:function(t,e){return l(t).gt(e)}},{key:"gte",value:function(t,e){return l(t).gte(e)}},{key:"max",value:function(t,e){return l(t).max(e)}},{key:"min",value:function(t,e){return l(t).min(e)}},{key:"minabs",value:function(t,e){return l(t).minabs(e)}},{key:"maxabs",value:function(t,e){return l(t).maxabs(e)}},{key:"clamp",value:function(t,e,i){return l(t).clamp(e,i)}},{key:"clampMin",value:function(t,e){return l(t).clampMin(e)}},{key:"clampMax",value:function(t,e){return l(t).clampMax(e)}},{key:"cmp_tolerance",value:function(t,e,i){return l(t).cmp_tolerance(e,i)}},{key:"compare_tolerance",value:function(t,e,i){return l(t).cmp_tolerance(e,i)}},{key:"eq_tolerance",value:function(t,e,i){return l(t).eq_tolerance(e,i)}},{key:"equals_tolerance",value:function(t,e,i){return l(t).eq_tolerance(e,i)}},{key:"neq_tolerance",value:function(t,e,i){return l(t).neq_tolerance(e,i)}},{key:"notEquals_tolerance",value:function(t,e,i){return l(t).notEquals_tolerance(e,i)}},{key:"lt_tolerance",value:function(t,e,i){return l(t).lt_tolerance(e,i)}},{key:"lte_tolerance",value:function(t,e,i){return l(t).lte_tolerance(e,i)}},{key:"gt_tolerance",value:function(t,e,i){return l(t).gt_tolerance(e,i)}},{key:"gte_tolerance",value:function(t,e,i){return l(t).gte_tolerance(e,i)}},{key:"pLog10",value:function(t){return l(t).pLog10()}},{key:"absLog10",value:function(t){return l(t).absLog10()}},{key:"log10",value:function(t){return l(t).log10()}},{key:"log",value:function(t,e){return l(t).log(e)}},{key:"log2",value:function(t){return l(t).log2()}},{key:"ln",value:function(t){return l(t).ln()}},{key:"logarithm",value:function(t,e){return l(t).logarithm(e)}},{key:"pow",value:function(t,e){return l(t).pow(e)}},{key:"pow10",value:function(t){return l(t).pow10()}},{key:"root",value:function(t,e){return l(t).root(e)}},{key:"factorial",value:function(t,e){return l(t).factorial()}},{key:"gamma",value:function(t,e){return l(t).gamma()}},{key:"lngamma",value:function(t,e){return l(t).lngamma()}},{key:"exp",value:function(t){return l(t).exp()}},{key:"sqr",value:function(t){return l(t).sqr()}},{key:"sqrt",value:function(t){return l(t).sqrt()}},{key:"cube",value:function(t){return l(t).cube()}},{key:"cbrt",value:function(t){return l(t).cbrt()}},{key:"tetrate",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:g(1,0,1);return l(t).tetrate(e,i)}},{key:"iteratedexp",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:g(1,0,1);return l(t).iteratedexp(e,i)}},{key:"iteratedlog",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:10,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1;return l(t).iteratedlog(e,i)}},{key:"layeradd10",value:function(t,e){return l(t).layeradd10(e)}},{key:"layeradd",value:function(t,e){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:10;return l(t).layeradd(e,i)}},{key:"slog",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:10;return l(t).slog(e)}},{key:"lambertw",value:function(t){return l(t).lambertw()}},{key:"ssqrt",value:function(t){return l(t).ssqrt()}},{key:"pentate",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:g(1,0,1);return l(t).pentate(e,i)}},{key:"affordGeometricSeries",value:function(t,e,i,r){return this.affordGeometricSeries_core(l(t),l(e),l(i),r)}},{key:"sumGeometricSeries",value:function(t,e,i,r){return this.sumGeometricSeries_core(t,l(e),l(i),r)}},{key:"affordArithmeticSeries",value:function(t,e,i,r){return this.affordArithmeticSeries_core(l(t),l(e),l(i),l(r))}},{key:"sumArithmeticSeries",value:function(t,e,i,r){return this.sumArithmeticSeries_core(l(t),l(e),l(i),l(r))}},{key:"efficiencyOfPurchase",value:function(t,e,i){return this.efficiencyOfPurchase_core(l(t),l(e),l(i))}},{key:"randomDecimalForTesting",value:function(t){if(20*Math.random()<1)return g(0,0,0);var e=Math.random()>.5?1:-1;if(20*Math.random()<1)return g(e,0,1);var i=Math.floor(Math.random()*(t+1)),r=0===i?616*Math.random()-308:16*Math.random();Math.random()>.9&&(r=Math.trunc(r));var n=Math.pow(10,r);return Math.random()>.9&&(n=Math.trunc(n)),m(e,i,n)}},{key:"affordGeometricSeries_core",value:function(t,i,r,n){var a=i.mul(r.pow(n));return e.floor(t.div(a).mul(r.sub(1)).add(1).log10().div(r.log10()))}},{key:"sumGeometricSeries_core",value:function(t,i,r,n){return i.mul(r.pow(n)).mul(e.sub(1,r.pow(t))).div(e.sub(1,r))}},{key:"affordArithmeticSeries_core",value:function(t,e,i,r){var n=e.add(r.mul(i)).sub(i.div(2)),a=n.pow(2);return n.neg().add(a.add(i.mul(t).mul(2)).sqrt()).div(i).floor()}},{key:"sumArithmeticSeries_core",value:function(t,e,i,r){var n=e.add(r.mul(i));return t.div(2).mul(n.mul(2).plus(t.sub(1).mul(i)))}},{key:"efficiencyOfPurchase_core",value:function(t,e,i){return t.div(e).add(t.div(i))}},{key:"slog_critical",value:function(t,i){return t>10?i-1:e.critical_section(t,i,o)}},{key:"tetrate_critical",value:function(t,i){return e.critical_section(t,i,h)}},{key:"critical_section",value:function(t,e,i){(e*=10)<0&&(e=0),e>10&&(e=10),t<2&&(t=2),t>10&&(t=10);for(var r=0,n=0,a=0;at){var s=(t-u[a])/(u[a+1]-u[a]);r=i[a][Math.floor(e)]*(1-s)+i[a+1][Math.floor(e)]*s,n=i[a][Math.ceil(e)]*(1-s)+i[a+1][Math.ceil(e)]*s;break}}var h=e-Math.floor(e);return r<=0||n<=0?r*(1-h)+n*h:Math.pow(t,Math.log(r)/Math.log(t)*(1-h)+Math.log(n)/Math.log(t)*h)}}]),e}();return k.dZero=g(0,0,0),k.dOne=g(1,0,1),k.dNegOne=g(-1,0,1),k.dTwo=g(1,0,2),k.dTen=g(1,0,10),k.dNaN=g(Number.NaN,Number.NaN,Number.NaN),k.dInf=g(1,Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY),k.dNegInf=g(-1,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY),k.dNumberMax=m(1,0,Number.MAX_VALUE),k.dNumberMin=m(1,0,Number.MIN_VALUE),k.fromStringCache=new r(1023),l=k.fromValue_noAlloc,m=k.fromComponents,g=k.fromComponents_noNormalize,k.fromMantissaExponent,k.fromMantissaExponent_noNormalize,k})); +"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var i=0;ithis.maxSize;){var n=this.last;this.map.delete(n.key),this.last=n.prev,this.last.next=void 0}}}}]),e}(),ListNode=_createClass(function e(t,i){_classCallCheck(this,e),this.next=void 0,this.prev=void 0,this.key=t,this.value=i}),MAX_SIGNIFICANT_DIGITS=17,EXP_LIMIT=9e15,LAYER_DOWN=Math.log10(9e15),FIRST_NEG_LAYER=1/9e15,NUMBER_EXP_MAX=308,NUMBER_EXP_MIN=-324,MAX_ES_IN_A_ROW=5,DEFAULT_FROM_STRING_CACHE_SIZE=1023,powerOf10=function(){for(var e=[],t=NUMBER_EXP_MIN+1;t<=NUMBER_EXP_MAX;t++)e.push(Number("1e"+t));return function(t){return e[t+323]}}(),critical_headers=[2,Math.E,3,4,5,6,7,8,9,10],critical_tetr_values=[[1,1.0891180521811203,1.1789767925673957,1.2701455431742086,1.3632090180450092,1.4587818160364217,1.5575237916251419,1.6601571006859253,1.767485818836978,1.8804192098842727,2],[1,1.1121114330934079,1.231038924931609,1.3583836963111375,1.4960519303993531,1.6463542337511945,1.8121385357018724,1.996971324618307,2.2053895545527546,2.4432574483385254,Math.E],[1,1.1187738849693603,1.2464963939368214,1.38527004705667,1.5376664685821402,1.7068895236551784,1.897001227148399,2.1132403089001035,2.362480153784171,2.6539010333870774,3],[1,1.1367350847096405,1.2889510672956703,1.4606478703324786,1.6570295196661111,1.8850062585672889,2.1539465047453485,2.476829779693097,2.872061932789197,3.3664204535587183,4],[1,1.1494592900767588,1.319708228183931,1.5166291280087583,1.748171114438024,2.0253263297298045,2.3636668498288547,2.7858359149579424,3.3257226212448145,4.035730287722532,5],[1,1.159225940787673,1.343712473580932,1.5611293155111927,1.8221199554561318,2.14183924486326,2.542468319282638,3.0574682501653316,3.7390572020926873,4.6719550537360774,6],[1,1.1670905356972596,1.3632807444991446,1.5979222279405536,1.8842640123816674,2.2416069644878687,2.69893426559423,3.3012632110403577,4.121250340630164,5.281493033448316,7],[1,1.1736630594087796,1.379783782386201,1.6292821855668218,1.9378971836180754,2.3289975651071977,2.8384347394720835,3.5232708454565906,4.478242031114584,5.868592169644505,8],[1,1.1793017514670474,1.394054150657457,1.65664127441059,1.985170999970283,2.4069682290577457,2.9647310119960752,3.7278665320924946,4.814462547283592,6.436522247411611,9],[1,1.1840100246247336,1.4061375836156955,1.6802272208863964,2.026757028388619,2.4770056063449646,3.080525271755482,3.9191964192627284,5.135152840833187,6.989961179534715,10]],critical_slog_values=[[-1,-.9194161097107025,-.8335625019330468,-.7425599821143978,-.6466611521029437,-.5462617907227869,-.4419033816638769,-.3342645487554494,-.224140440909962,-.11241087890006762,0],[-1,-.90603157029014,-.80786507256596,-.7064666939634,-.60294836853664,-.49849837513117,-.39430303318768,-.29147201034755,-.19097820800866,-.09361896280296,0],[-1,-.9021579584316141,-.8005762598234203,-.6964780623319391,-.5911906810998454,-.486050182576545,-.3823089430815083,-.28106046722897615,-.1831906535795894,-.08935809204418144,0],[-1,-.8917227442365535,-.781258746326964,-.6705130326902455,-.5612813129406509,-.4551067709033134,-.35319256652135966,-.2563741554088552,-.1651412821106526,-.0796919581982668,0],[-1,-.8843387974366064,-.7678744063886243,-.6529563724510552,-.5415870994657841,-.4352842206588936,-.33504449124791424,-.24138853420685147,-.15445285440944467,-.07409659641336663,0],[-1,-.8786709358426346,-.7577735191184886,-.6399546189952064,-.527284921869926,-.4211627631006314,-.3223479611761232,-.23107655627789858,-.1472057700818259,-.07035171210706326,0],[-1,-.8740862815291583,-.7497032990976209,-.6297119746181752,-.5161838335958787,-.41036238255751956,-.31277212146489963,-.2233976621705518,-.1418697367979619,-.06762117662323441,0],[-1,-.8702632331800649,-.7430366914122081,-.6213373075161548,-.5072025698095242,-.40171437727184167,-.30517930701410456,-.21736343968190863,-.137710238299109,-.06550774483471955,0],[-1,-.8670016295947213,-.7373984232432306,-.6143173985094293,-.49973884395492807,-.394584953527678,-.2989649949848695,-.21245647317021688,-.13434688362382652,-.0638072667348083,0],[-1,-.8641642839543857,-.732534623168535,-.6083127477059322,-.4934049257184696,-.3885773075899922,-.29376029055315767,-.2083678561173622,-.13155653399373268,-.062401588652553186,0]],D=function e(t){return Decimal.fromValue_noAlloc(t)},FC=function e(t,i,r){return Decimal.fromComponents(t,i,r)},FC_NN=function e(t,i,r){return Decimal.fromComponents_noNormalize(t,i,r)},decimalPlaces=function e(t,i){var r=i+1,n=Math.ceil(Math.log10(Math.abs(t)));return parseFloat((Math.round(t*Math.pow(10,r-n))*Math.pow(10,n-r)).toFixed(Math.max(r-n,0)))},f_maglog10=function e(t){return Math.sign(t)*Math.log10(Math.abs(t))},f_gamma=function e(t){if(!isFinite(t))return t;if(t<-50)return t===Math.trunc(t)?Number.NEGATIVE_INFINITY:0;for(var i=1;t<10;)i*=t,++t;t-=1;var r=.9189385332046727;r+=(t+.5)*Math.log(t),r-=t;var n=t*t,a=t;return r+=1/(12*a),a*=n,r+=1/(360*a),a*=n,r+=1/(1260*a),a*=n,r+=1/(1680*a),a*=n,r+=1/(1188*a),a*=n,r+=691/(360360*a),a*=n,r+=7/(1092*a),a*=n,Math.exp(r+=3617/(122400*a))/i},OMEGA=.5671432904097838,f_lambertw=function e(t){var i,r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1e-10;if(!Number.isFinite(t)||0===t)return t;if(1===t)return OMEGA;i=t<10?0:Math.log(t)-Math.log(Math.log(t));for(var a=0;a<100;++a){if(Math.abs((r=(t*Math.exp(-i)+i*i)/(i+1))-i)1&&void 0!==arguments[1]?arguments[1]:1e-10;if(!Number.isFinite(e.mag)||e.eq(Decimal.dZero))return e;if(e.eq(Decimal.dOne))return Decimal.fromNumber(OMEGA);t=Decimal.ln(e);for(var s=0;s<100;++s){if(i=t.neg().exp(),r=t.sub(e.mul(i)),n=t.sub(r.div(t.add(1).sub(t.add(2).mul(r).div(Decimal.mul(2,t).add(2))))),Decimal.abs(n.sub(t)).lt(Decimal.abs(n).mul(a)))return n;t=n}throw Error("Iteration failed to converge: ".concat(e.toString()))}var Decimal=function(){function e(t){_classCallCheck(this,e),this.sign=0,this.mag=0,this.layer=0,t instanceof e?this.fromDecimal(t):"number"==typeof t?this.fromNumber(t):"string"==typeof t&&this.fromString(t)}return _createClass(e,[{key:"m",get:function e(){if(0===this.sign)return 0;if(0===this.layer){var t,i=Math.floor(Math.log10(this.mag));return t=5e-324===this.mag?5:this.mag/powerOf10(i),this.sign*t}if(1!==this.layer)return this.sign;var r=this.mag-Math.floor(this.mag);return this.sign*Math.pow(10,r)},set:function e(t){this.layer<=2?this.fromMantissaExponent(t,this.e):(this.sign=Math.sign(t),0===this.sign&&(this.layer=0,this.exponent=0))}},{key:"e",get:function e(){return 0===this.sign?0:0===this.layer?Math.floor(Math.log10(this.mag)):1===this.layer?Math.floor(this.mag):2===this.layer?Math.floor(Math.sign(this.mag)*Math.pow(10,Math.abs(this.mag))):this.mag*Number.POSITIVE_INFINITY},set:function e(t){this.fromMantissaExponent(this.m,t)}},{key:"s",get:function e(){return this.sign},set:function e(t){0===t?(this.sign=0,this.layer=0,this.mag=0):this.sign=t}},{key:"mantissa",get:function e(){return this.m},set:function e(t){this.m=t}},{key:"exponent",get:function e(){return this.e},set:function e(t){this.e=t}},{key:"normalize",value:function e(){if(0===this.sign||0===this.mag&&0===this.layer)return this.sign=0,this.mag=0,this.layer=0,this;if(0===this.layer&&this.mag<0&&(this.mag=-this.mag,this.sign=-this.sign),0===this.layer&&this.mag=EXP_LIMIT)this.layer+=1,this.mag=i*Math.log10(t);else{for(;t0;)this.layer-=1,0===this.layer?this.mag=Math.pow(10,this.mag):(this.mag=i*Math.pow(10,t),t=Math.abs(this.mag),i=Math.sign(this.mag));0===this.layer&&(this.mag<0?(this.mag=-this.mag,this.sign=-this.sign):0===this.mag&&(this.sign=0))}return this}},{key:"fromComponents",value:function e(t,i,r){return this.sign=t,this.layer=i,this.mag=r,this.normalize(),this}},{key:"fromComponents_noNormalize",value:function e(t,i,r){return this.sign=t,this.layer=i,this.mag=r,this}},{key:"fromMantissaExponent",value:function e(t,i){return this.layer=1,this.sign=Math.sign(t),t=Math.abs(t),this.mag=i+Math.log10(t),this.normalize(),this}},{key:"fromMantissaExponent_noNormalize",value:function e(t,i){return this.fromMantissaExponent(t,i),this}},{key:"fromDecimal",value:function e(t){return this.sign=t.sign,this.layer=t.layer,this.mag=t.mag,this}},{key:"fromNumber",value:function e(t){return this.mag=Math.abs(t),this.sign=Math.sign(t),this.layer=0,this.normalize(),this}},{key:"fromString",value:function t(i){var r,n,a=i,s=e.fromStringCache.get(a);if(void 0!==s)return this.fromDecimal(s);var u=(i=i.replace(",","")).split("^^^");if(2===u.length){var l=parseFloat(u[0]),o=parseFloat(u[1]),$=u[1].split(";"),_=1;if(2!==$.length||(_=parseFloat($[1]),isFinite(_)||(_=1)),isFinite(l)&&isFinite(o)){var h=e.pentate(l,o,_);return this.sign=h.sign,this.layer=h.layer,this.mag=h.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this}}var m=i.split("^^");if(2===m.length){var g=parseFloat(m[0]),c=parseFloat(m[1]),f=m[1].split(";"),y=1;if(2!==f.length||(y=parseFloat(f[1]),isFinite(y)||(y=1)),isFinite(g)&&isFinite(c)){var v=e.tetrate(g,c,y);return this.sign=v.sign,this.layer=v.layer,this.mag=v.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this}}var d=i.split("^");if(2===d.length){var N=parseFloat(d[0]),k=parseFloat(d[1]);if(isFinite(N)&&isFinite(k)){var p=e.pow(N,k);return this.sign=p.sign,this.layer=p.layer,this.mag=p.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this}}var b=(i=i.trim().toLowerCase()).split("pt");if(2===b.length){r=10,n=parseFloat(b[0]),b[1]=b[1].replace("(",""),b[1]=b[1].replace(")","");var C=parseFloat(b[1]);if(isFinite(C)||(C=1),isFinite(r)&&isFinite(n)){var F=e.tetrate(r,n,C);return this.sign=F.sign,this.layer=F.layer,this.mag=F.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this}}if(2===(b=i.split("p")).length){r=10,n=parseFloat(b[0]),b[1]=b[1].replace("(",""),b[1]=b[1].replace(")","");var S=parseFloat(b[1]);if(isFinite(S)||(S=1),isFinite(r)&&isFinite(n)){var I=e.tetrate(r,n,S);return this.sign=I.sign,this.layer=I.layer,this.mag=I.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this}}var x=i.split("e"),w=x.length-1;if(0===w){var E=parseFloat(i);if(isFinite(E))return this.fromNumber(E),e.fromStringCache.size>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this}else if(1===w){var q=parseFloat(i);if(isFinite(q)&&0!==q)return this.fromNumber(q),e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this}var T=i.split("e^");if(2===T.length){this.sign=1,"-"==T[0].charAt(0)&&(this.sign=-1);for(var A="",O=0;O=43)||!(M<=57))&&101!==M)return this.layer=parseFloat(A),this.mag=parseFloat(T[1].substr(O+1)),this.normalize(),e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this;A+=T[1].charAt(O)}}if(w<1)return this.sign=0,this.layer=0,this.mag=0,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this;var z=parseFloat(x[0]);if(0===z)return this.sign=0,this.layer=0,this.mag=0,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this;var P=parseFloat(x[x.length-1]);if(w>=2){var G=parseFloat(x[x.length-2]);isFinite(G)&&(P*=Math.sign(G),P+=f_maglog10(G))}if(isFinite(z)){if(1===w)this.sign=Math.sign(z),this.layer=1,this.mag=P+Math.log10(Math.abs(z));else{if(this.sign=Math.sign(z),this.layer=w,2===w){var L=e.mul(FC(1,2,P),D(z));return this.sign=L.sign,this.layer=L.layer,this.mag=L.mag,e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this}this.mag=P}}else this.sign="-"===x[0]?-1:1,this.layer=w,this.mag=P;return this.normalize(),e.fromStringCache.maxSize>=1&&e.fromStringCache.set(a,e.fromDecimal(this)),this}},{key:"fromValue",value:function t(i){return i instanceof e?this.fromDecimal(i):"number"==typeof i?this.fromNumber(i):"string"==typeof i?this.fromString(i):(this.sign=0,this.layer=0,this.mag=0,this)}},{key:"toNumber",value:function e(){return Number.isFinite(this.layer)?0===this.layer?this.sign*this.mag:1===this.layer?this.sign*Math.pow(10,this.mag):this.mag>0?this.sign>0?Number.POSITIVE_INFINITY:Number.NEGATIVE_INFINITY:0:Number.NaN}},{key:"mantissaWithDecimalPlaces",value:function e(t){return isNaN(this.m)?Number.NaN:0===this.m?0:decimalPlaces(this.m,t)}},{key:"magnitudeWithDecimalPlaces",value:function e(t){return isNaN(this.mag)?Number.NaN:0===this.mag?0:decimalPlaces(this.mag,t)}},{key:"toString",value:function e(){return isNaN(this.layer)||isNaN(this.sign)||isNaN(this.mag)?"NaN":this.mag===Number.POSITIVE_INFINITY||this.layer===Number.POSITIVE_INFINITY?1===this.sign?"Infinity":"-Infinity":0===this.layer?this.mag<1e21&&this.mag>1e-7||0===this.mag?(this.sign*this.mag).toString():this.m+"e"+this.e:1===this.layer?this.m+"e"+this.e:this.layer<=MAX_ES_IN_A_ROW?(-1===this.sign?"-":"")+"e".repeat(this.layer)+this.mag:(-1===this.sign?"-":"")+"(e^"+this.layer+")"+this.mag}},{key:"toExponential",value:function e(t){return 0===this.layer?(this.sign*this.mag).toExponential(t):this.toStringWithDecimalPlaces(t)}},{key:"toFixed",value:function e(t){return 0===this.layer?(this.sign*this.mag).toFixed(t):this.toStringWithDecimalPlaces(t)}},{key:"toPrecision",value:function e(t){return this.e<=-7?this.toExponential(t-1):t>this.e?this.toFixed(t-this.exponent-1):this.toExponential(t-1)}},{key:"valueOf",value:function e(){return this.toString()}},{key:"toJSON",value:function e(){return this.toString()}},{key:"toStringWithDecimalPlaces",value:function e(t){return 0===this.layer?this.mag<1e21&&this.mag>1e-7||0===this.mag?(this.sign*this.mag).toFixed(t):decimalPlaces(this.m,t)+"e"+decimalPlaces(this.e,t):1===this.layer?decimalPlaces(this.m,t)+"e"+decimalPlaces(this.e,t):this.layer<=MAX_ES_IN_A_ROW?(-1===this.sign?"-":"")+"e".repeat(this.layer)+decimalPlaces(this.mag,t):(-1===this.sign?"-":"")+"(e^"+this.layer+")"+decimalPlaces(this.mag,t)}},{key:"abs",value:function e(){return FC_NN(0===this.sign?0:1,this.layer,this.mag)}},{key:"neg",value:function e(){return FC_NN(-this.sign,this.layer,this.mag)}},{key:"negate",value:function e(){return this.neg()}},{key:"negated",value:function e(){return this.neg()}},{key:"sgn",value:function e(){return this.sign}},{key:"round",value:function t(){return this.mag<0?e.dZero:0===this.layer?FC(this.sign,0,Math.round(this.mag)):this}},{key:"floor",value:function t(){return this.mag<0?e.dZero:0===this.layer?FC(this.sign,0,Math.floor(this.mag)):this}},{key:"ceil",value:function t(){return this.mag<0?e.dZero:0===this.layer?FC(this.sign,0,Math.ceil(this.mag)):this}},{key:"trunc",value:function t(){return this.mag<0?e.dZero:0===this.layer?FC(this.sign,0,Math.trunc(this.mag)):this}},{key:"add",value:function t(i){var r,n,a=D(i);if(!Number.isFinite(this.layer))return this;if(!Number.isFinite(a.layer)||0===this.sign)return a;if(0===a.sign)return this;if(this.sign===-a.sign&&this.layer===a.layer&&this.mag===a.mag)return FC_NN(0,0,0);if(this.layer>=2||a.layer>=2)return this.maxabs(a);if(e.cmpabs(this,a)>0?(r=this,n=a):(r=a,n=this),0===r.layer&&0===n.layer)return e.fromNumber(r.sign*r.mag+n.sign*n.mag);var s=r.layer*Math.sign(r.mag),u=n.layer*Math.sign(n.mag);if(s-u>=2)return r;if(0===s&&-1===u){if(Math.abs(n.mag-Math.log10(r.mag))>MAX_SIGNIFICANT_DIGITS)return r;var l=Math.pow(10,Math.log10(r.mag)-n.mag),o=n.sign+r.sign*l;return FC(Math.sign(o),1,n.mag+Math.log10(Math.abs(o)))}if(1===s&&0===u){if(Math.abs(r.mag-Math.log10(n.mag))>MAX_SIGNIFICANT_DIGITS)return r;var $=Math.pow(10,r.mag-Math.log10(n.mag)),_=n.sign+r.sign*$;return FC(Math.sign(_),1,Math.log10(n.mag)+Math.log10(Math.abs(_)))}if(Math.abs(r.mag-n.mag)>MAX_SIGNIFICANT_DIGITS)return r;var h=Math.pow(10,r.mag-n.mag),m=n.sign+r.sign*h;return FC(Math.sign(m),1,n.mag+Math.log10(Math.abs(m)))}},{key:"plus",value:function e(t){return this.add(t)}},{key:"sub",value:function e(t){return this.add(D(t).neg())}},{key:"subtract",value:function e(t){return this.sub(t)}},{key:"minus",value:function e(t){return this.sub(t)}},{key:"mul",value:function t(i){var r,n,a=D(i);if(!Number.isFinite(this.layer))return this;if(!Number.isFinite(a.layer))return a;if(0===this.sign||0===a.sign)return FC_NN(0,0,0);if(this.layer===a.layer&&this.mag===-a.mag)return FC_NN(this.sign*a.sign,0,1);if(this.layer>a.layer||this.layer==a.layer&&Math.abs(this.mag)>Math.abs(a.mag)?(r=this,n=a):(r=a,n=this),0===r.layer&&0===n.layer)return e.fromNumber(r.sign*n.sign*r.mag*n.mag);if(r.layer>=3||r.layer-n.layer>=2)return FC(r.sign*n.sign,r.layer,r.mag);if(1===r.layer&&0===n.layer)return FC(r.sign*n.sign,1,r.mag+Math.log10(n.mag));if(1===r.layer&&1===n.layer)return FC(r.sign*n.sign,1,r.mag+n.mag);if(2===r.layer&&1===n.layer){var s=FC(Math.sign(r.mag),r.layer-1,Math.abs(r.mag)).add(FC(Math.sign(n.mag),n.layer-1,Math.abs(n.mag)));return FC(r.sign*n.sign,s.layer+1,s.sign*s.mag)}if(2===r.layer&&2===n.layer){var u=FC(Math.sign(r.mag),r.layer-1,Math.abs(r.mag)).add(FC(Math.sign(n.mag),n.layer-1,Math.abs(n.mag)));return FC(r.sign*n.sign,u.layer+1,u.sign*u.mag)}throw Error("Bad arguments to mul: "+this+", "+i)}},{key:"multiply",value:function e(t){return this.mul(t)}},{key:"times",value:function e(t){return this.mul(t)}},{key:"div",value:function e(t){var i=D(t);return this.mul(i.recip())}},{key:"divide",value:function e(t){return this.div(t)}},{key:"divideBy",value:function e(t){return this.div(t)}},{key:"dividedBy",value:function e(t){return this.div(t)}},{key:"recip",value:function t(){return 0===this.mag?e.dNaN:0===this.layer?FC(this.sign,0,1/this.mag):FC(this.sign,this.layer,-this.mag)}},{key:"reciprocal",value:function e(){return this.recip()}},{key:"reciprocate",value:function e(){return this.recip()}},{key:"cmp",value:function e(t){var i=D(t);return this.sign>i.sign?1:this.sign0?this.layer:-this.layer,n=i.mag>0?i.layer:-i.layer;return r>n?1:ri.mag?1:this.magthis.cmpabs(i)?i:this}},{key:"minabs",value:function e(t){var i=D(t);return this.cmpabs(i)>0?i:this}},{key:"clamp",value:function e(t,i){return this.max(t).min(i)}},{key:"clampMin",value:function e(t){return this.max(t)}},{key:"clampMax",value:function e(t){return this.min(t)}},{key:"cmp_tolerance",value:function e(t,i){var r=D(t);return this.eq_tolerance(r,i)?0:this.cmp(r)}},{key:"compare_tolerance",value:function e(t,i){return this.cmp_tolerance(t,i)}},{key:"eq_tolerance",value:function e(t,i){var r=D(t);if(null==i&&(i=1e-7),this.sign!==r.sign||Math.abs(this.layer-r.layer)>1)return!1;var n=this.mag,a=r.mag;return this.layer>r.layer&&(a=f_maglog10(a)),this.layer0?FC(Math.sign(this.mag),this.layer-1,Math.abs(this.mag)):FC(1,0,Math.log10(this.mag))}},{key:"log10",value:function t(){return this.sign<=0?e.dNaN:this.layer>0?FC(Math.sign(this.mag),this.layer-1,Math.abs(this.mag)):FC(this.sign,0,Math.log10(this.mag))}},{key:"log",value:function t(i){return(i=D(i),this.sign<=0||i.sign<=0)?e.dNaN:1===i.sign&&0===i.layer&&1===i.mag?e.dNaN:0===this.layer&&0===i.layer?FC(this.sign,0,Math.log(this.mag)/Math.log(i.mag)):e.div(this.log10(),i.log10())}},{key:"log2",value:function t(){return this.sign<=0?e.dNaN:0===this.layer?FC(this.sign,0,Math.log2(this.mag)):1===this.layer?FC(Math.sign(this.mag),0,3.321928094887362*Math.abs(this.mag)):2===this.layer?FC(Math.sign(this.mag),1,Math.abs(this.mag)+.5213902276543247):FC(Math.sign(this.mag),this.layer-1,Math.abs(this.mag))}},{key:"ln",value:function t(){return this.sign<=0?e.dNaN:0===this.layer?FC(this.sign,0,Math.log(this.mag)):1===this.layer?FC(Math.sign(this.mag),0,2.302585092994046*Math.abs(this.mag)):2===this.layer?FC(Math.sign(this.mag),1,Math.abs(this.mag)+.36221568869946325):FC(Math.sign(this.mag),this.layer-1,Math.abs(this.mag))}},{key:"logarithm",value:function e(t){return this.log(t)}},{key:"pow",value:function t(i){var r,n=D(i);if(0===this.sign)return n.eq(0)?FC_NN(1,0,1):this;if(1===this.sign&&0===this.layer&&1===this.mag)return this;if(0===n.sign)return FC_NN(1,0,1);if(1===n.sign&&0===n.layer&&1===n.mag)return this;var a=this.absLog10().mul(n).pow10();return -1===this.sign?Math.abs(n.toNumber()%2)%2==1?a.neg():Math.abs(n.toNumber()%2)%2==0?a:e.dNaN:a}},{key:"pow10",value:function t(){if(!Number.isFinite(this.layer)||!Number.isFinite(this.mag))return e.dNaN;var i=this;if(0===i.layer){var r=Math.pow(10,i.sign*i.mag);if(Number.isFinite(r)&&Math.abs(r)>=.1)return FC(1,0,r);if(0===i.sign)return e.dOne;i=FC_NN(i.sign,i.layer+1,Math.log10(i.mag))}return i.sign>0&&i.mag>=0?FC(i.sign,i.layer+1,i.mag):i.sign<0&&i.mag>=0?FC(-i.sign,i.layer+1,-i.mag):e.dOne}},{key:"pow_base",value:function e(t){return D(t).pow(this)}},{key:"root",value:function e(t){var i=D(t);return this.pow(i.recip())}},{key:"factorial",value:function t(){return this.mag<0||0===this.layer?this.add(1).gamma():1===this.layer?e.exp(e.mul(this,e.ln(this).sub(1))):e.exp(this)}},{key:"gamma",value:function t(){if(this.mag<0)return this.recip();if(0===this.layer){if(this.lt(FC_NN(1,0,24)))return e.fromNumber(f_gamma(this.sign*this.mag));var i=this.mag-1,r=.9189385332046727;r+=(i+.5)*Math.log(i),r-=i;var n=i*i,a=i,s=12*a,u=1/s,l=r+u;if(l===r||(r=l,a*=n,(l=r-(u=1/(s=360*a)))===r))return e.exp(r);r=l,a*=n;var o=1/(s=1260*a);return r+=o,a*=n,r-=o=1/(s=1680*a),e.exp(r)}return 1===this.layer?e.exp(e.mul(this,e.ln(this).sub(1))):e.exp(this)}},{key:"lngamma",value:function e(){return this.gamma().ln()}},{key:"exp",value:function t(){return this.mag<0?e.dOne:0===this.layer&&this.mag<=709.7?e.fromNumber(Math.exp(this.sign*this.mag)):0===this.layer?FC(1,1,this.sign*Math.log10(Math.E)*this.mag):1===this.layer?FC(1,2,this.sign*(Math.log10(.4342944819032518)+this.mag)):FC(1,this.layer+1,this.sign*this.mag)}},{key:"sqr",value:function e(){return this.pow(2)}},{key:"sqrt",value:function t(){if(0===this.layer)return e.fromNumber(Math.sqrt(this.sign*this.mag));if(1===this.layer)return FC(1,2,Math.log10(this.mag)-.3010299956639812);var i=e.div(FC_NN(this.sign,this.layer-1,this.mag),FC_NN(1,0,2));return i.layer+=1,i.normalize(),i}},{key:"cube",value:function e(){return this.pow(3)}},{key:"cbrt",value:function e(){return this.pow(1/3)}},{key:"tetrate",value:function t(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:2,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:FC_NN(1,0,1);if(1===i)return e.pow(this,r);if(0===i)return new e(r);if(this.eq(e.dOne))return e.dOne;if(this.eq(-1))return e.pow(this,r);if(i===Number.POSITIVE_INFINITY){var n=this.toNumber();if(n<=1.444667861009766&&n>=.06598803584531254){if(n>1.444667861009099)return e.fromNumber(Math.E);var a=e.ln(this).neg();return a.lambertw().div(a)}return n>1.444667861009766?e.fromNumber(Number.POSITIVE_INFINITY):(!1,e.dNaN)}if(this.eq(e.dZero)){var s=Math.abs((i+1)%2);return s>1&&(s=2-s),e.fromNumber(s)}if(i<0)return e.iteratedlog(r,this,-i);r=D(r);var u=i;i=Math.trunc(i);var l=u-i;if(this.gt(e.dZero)&&this.lte(1.444667861009766)){i=Math.min(1e4,i);for(var o=0;o3)return FC_NN(r.sign,r.layer+(i-h-1),r.mag);if(h>1e4)break}return r}},{key:"iteratedexp",value:function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:2,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:FC_NN(1,0,1);return this.tetrate(t,i)}},{key:"iteratedlog",value:function t(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:10,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1;if(r<0)return e.tetrate(i,-r,this);i=D(i);var n=e.fromDecimal(this),a=r;r=Math.trunc(r);var s=a-r;if(n.layer-i.layer>3){var u=Math.min(r,n.layer-i.layer-3);r-=u,n.layer-=u}for(var l=0;l1e4)return n}return s>0&&s<1&&(n=i.eq(10)?n.layeradd10(-s):n.layeradd(-s,i)),n}},{key:"slog",value:function t(){for(var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:10,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:100,n=.001,a=!1,s=!1,u=this.slog_internal(i).toNumber(),l=1;l1&&s!=o&&(a=!0),s=o,a?n/=2:n*=2,u+=n=Math.abs(n)*(o?-1:1),0===n)break}return e.fromNumber(u)}},{key:"slog_internal",value:function t(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:10;if((i=D(i)).lte(e.dZero)||i.eq(e.dOne))return e.dNaN;if(i.lt(e.dOne))return this.eq(e.dOne)?e.dZero:this.eq(e.dZero)?e.dNegOne:e.dNaN;if(this.mag<0||this.eq(e.dZero))return e.dNegOne;var r=0,n=e.fromDecimal(this);if(n.layer-i.layer>3){var a=n.layer-i.layer-3;r+=a,n.layer-=a}for(var s=0;s<100;++s)if(n.lt(e.dZero))n=e.pow(i,n),r-=1;else{if(n.lte(e.dOne))return e.fromNumber(r+e.slog_critical(i.toNumber(),n.toNumber()));r+=1,n=e.log(n,i)}return e.fromNumber(r)}},{key:"layeradd10",value:function t(i){i=e.fromValue_noAlloc(i).toNumber();var r=e.fromDecimal(this);if(i>=1){r.mag<0&&r.layer>0?(r.sign=0,r.mag=0,r.layer=0):-1===r.sign&&0==r.layer&&(r.sign=1,r.mag=-r.mag);var n=Math.trunc(i);i-=n,r.layer+=n}if(i<=-1){var a=Math.trunc(i);if(i-=a,r.layer+=a,r.layer<0)for(var s=0;s<100;++s){if(r.layer++,r.mag=Math.log10(r.mag),!isFinite(r.mag))return 0===r.sign&&(r.sign=1),r.layer<0&&(r.layer=0),r.normalize();if(r.layer>=0)break}}for(;r.layer<0;)r.layer++,r.mag=Math.log10(r.mag);return(0===r.sign&&(r.sign=1,0===r.mag&&r.layer>=1&&(r.layer-=1,r.mag=1)),r.normalize(),0!==i)?r.layeradd(i,10):r}},{key:"layeradd",value:function t(i,r){var n=this.slog(r).toNumber()+i;return n>=0?e.tetrate(r,n):Number.isFinite(n)?n>=-1?e.log(e.tetrate(r,n+1),r):e.log(e.log(e.tetrate(r,n+2),r),r):e.dNaN}},{key:"lambertw",value:function t(){if(this.lt(-.3678794411710499))throw Error("lambertw is unimplemented for results less than -1, sorry!");if(this.mag<0)return e.fromNumber(f_lambertw(this.toNumber()));if(0===this.layer)return e.fromNumber(f_lambertw(this.sign*this.mag));if(1===this.layer)return d_lambertw(this);if(2===this.layer)return d_lambertw(this);if(this.layer>=3)return FC_NN(this.sign,this.layer-1,this.mag);throw"Unhandled behavior in lambertw()"}},{key:"ssqrt",value:function e(){if(1==this.sign&&this.layer>=3)return FC_NN(this.sign,this.layer-1,this.mag);var t=this.ln();return t.div(t.lambertw())}},{key:"pentate",value:function t(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:2,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:FC_NN(1,0,1);r=D(r);var n=i;i=Math.trunc(i);var a=n-i;0!==a&&(r.eq(e.dOne)?(++i,r=e.fromNumber(a)):r=this.eq(10)?r.layeradd10(a):r.layeradd(a,this));for(var s=0;s10)break}return r}},{key:"sin",value:function t(){return this.mag<0?this:0===this.layer?e.fromNumber(Math.sin(this.sign*this.mag)):FC_NN(0,0,0)}},{key:"cos",value:function t(){return this.mag<0?e.dOne:0===this.layer?e.fromNumber(Math.cos(this.sign*this.mag)):FC_NN(0,0,0)}},{key:"tan",value:function t(){return this.mag<0?this:0===this.layer?e.fromNumber(Math.tan(this.sign*this.mag)):FC_NN(0,0,0)}},{key:"asin",value:function t(){return this.mag<0?this:0===this.layer?e.fromNumber(Math.asin(this.sign*this.mag)):FC_NN(Number.NaN,Number.NaN,Number.NaN)}},{key:"acos",value:function t(){return this.mag<0?e.fromNumber(Math.acos(this.toNumber())):0===this.layer?e.fromNumber(Math.acos(this.sign*this.mag)):FC_NN(Number.NaN,Number.NaN,Number.NaN)}},{key:"atan",value:function t(){return this.mag<0?this:0===this.layer?e.fromNumber(Math.atan(this.sign*this.mag)):e.fromNumber(Math.atan(Infinity*this.sign))}},{key:"sinh",value:function e(){return this.exp().sub(this.negate().exp()).div(2)}},{key:"cosh",value:function e(){return this.exp().add(this.negate().exp()).div(2)}},{key:"tanh",value:function e(){return this.sinh().div(this.cosh())}},{key:"asinh",value:function t(){return e.ln(this.add(this.sqr().add(1).sqrt()))}},{key:"acosh",value:function t(){return e.ln(this.add(this.sqr().sub(1).sqrt()))}},{key:"atanh",value:function t(){return this.abs().gte(1)?FC_NN(Number.NaN,Number.NaN,Number.NaN):e.ln(this.add(1).div(e.fromNumber(1).sub(this))).div(2)}},{key:"ascensionPenalty",value:function t(i){return 0===i?this:this.root(e.pow(10,i))}},{key:"egg",value:function e(){return this.add(9)}},{key:"lessThanOrEqualTo",value:function e(t){return 1>this.cmp(t)}},{key:"lessThan",value:function e(t){return 0>this.cmp(t)}},{key:"greaterThanOrEqualTo",value:function e(t){return this.cmp(t)>-1}},{key:"greaterThan",value:function e(t){return this.cmp(t)>0}}],[{key:"fromComponents",value:function t(i,r,n){return new e().fromComponents(i,r,n)}},{key:"fromComponents_noNormalize",value:function t(i,r,n){return new e().fromComponents_noNormalize(i,r,n)}},{key:"fromMantissaExponent",value:function t(i,r){return new e().fromMantissaExponent(i,r)}},{key:"fromMantissaExponent_noNormalize",value:function t(i,r){return new e().fromMantissaExponent_noNormalize(i,r)}},{key:"fromDecimal",value:function t(i){return new e().fromDecimal(i)}},{key:"fromNumber",value:function t(i){return new e().fromNumber(i)}},{key:"fromString",value:function t(i){return new e().fromString(i)}},{key:"fromValue",value:function t(i){return new e().fromValue(i)}},{key:"fromValue_noAlloc",value:function t(i){if(i instanceof e)return i;if("string"==typeof i){var r=e.fromStringCache.get(i);return void 0!==r?r:e.fromString(i)}return"number"==typeof i?e.fromNumber(i):(!1,e.dZero)}},{key:"abs",value:function e(t){return D(t).abs()}},{key:"neg",value:function e(t){return D(t).neg()}},{key:"negate",value:function e(t){return D(t).neg()}},{key:"negated",value:function e(t){return D(t).neg()}},{key:"sign",value:function e(t){return D(t).sign}},{key:"sgn",value:function e(t){return D(t).sign}},{key:"round",value:function e(t){return D(t).round()}},{key:"floor",value:function e(t){return D(t).floor()}},{key:"ceil",value:function e(t){return D(t).ceil()}},{key:"trunc",value:function e(t){return D(t).trunc()}},{key:"add",value:function e(t,i){return D(t).add(i)}},{key:"plus",value:function e(t,i){return D(t).add(i)}},{key:"sub",value:function e(t,i){return D(t).sub(i)}},{key:"subtract",value:function e(t,i){return D(t).sub(i)}},{key:"minus",value:function e(t,i){return D(t).sub(i)}},{key:"mul",value:function e(t,i){return D(t).mul(i)}},{key:"multiply",value:function e(t,i){return D(t).mul(i)}},{key:"times",value:function e(t,i){return D(t).mul(i)}},{key:"div",value:function e(t,i){return D(t).div(i)}},{key:"divide",value:function e(t,i){return D(t).div(i)}},{key:"recip",value:function e(t){return D(t).recip()}},{key:"reciprocal",value:function e(t){return D(t).recip()}},{key:"reciprocate",value:function e(t){return D(t).reciprocate()}},{key:"cmp",value:function e(t,i){return D(t).cmp(i)}},{key:"cmpabs",value:function e(t,i){return D(t).cmpabs(i)}},{key:"compare",value:function e(t,i){return D(t).cmp(i)}},{key:"isNaN",value:function(e){function t(t){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t}(function(e){return e=D(e),isNaN(e.sign)||isNaN(e.layer)||isNaN(e.mag)})},{key:"isFinite",value:function(e){function t(t){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t}(function(e){return e=D(e),isFinite(e.sign)&&isFinite(e.layer)&&isFinite(e.mag)})},{key:"eq",value:function e(t,i){return D(t).eq(i)}},{key:"equals",value:function e(t,i){return D(t).eq(i)}},{key:"neq",value:function e(t,i){return D(t).neq(i)}},{key:"notEquals",value:function e(t,i){return D(t).notEquals(i)}},{key:"lt",value:function e(t,i){return D(t).lt(i)}},{key:"lte",value:function e(t,i){return D(t).lte(i)}},{key:"gt",value:function e(t,i){return D(t).gt(i)}},{key:"gte",value:function e(t,i){return D(t).gte(i)}},{key:"max",value:function e(t,i){return D(t).max(i)}},{key:"min",value:function e(t,i){return D(t).min(i)}},{key:"minabs",value:function e(t,i){return D(t).minabs(i)}},{key:"maxabs",value:function e(t,i){return D(t).maxabs(i)}},{key:"clamp",value:function e(t,i,r){return D(t).clamp(i,r)}},{key:"clampMin",value:function e(t,i){return D(t).clampMin(i)}},{key:"clampMax",value:function e(t,i){return D(t).clampMax(i)}},{key:"cmp_tolerance",value:function e(t,i,r){return D(t).cmp_tolerance(i,r)}},{key:"compare_tolerance",value:function e(t,i,r){return D(t).cmp_tolerance(i,r)}},{key:"eq_tolerance",value:function e(t,i,r){return D(t).eq_tolerance(i,r)}},{key:"equals_tolerance",value:function e(t,i,r){return D(t).eq_tolerance(i,r)}},{key:"neq_tolerance",value:function e(t,i,r){return D(t).neq_tolerance(i,r)}},{key:"notEquals_tolerance",value:function e(t,i,r){return D(t).notEquals_tolerance(i,r)}},{key:"lt_tolerance",value:function e(t,i,r){return D(t).lt_tolerance(i,r)}},{key:"lte_tolerance",value:function e(t,i,r){return D(t).lte_tolerance(i,r)}},{key:"gt_tolerance",value:function e(t,i,r){return D(t).gt_tolerance(i,r)}},{key:"gte_tolerance",value:function e(t,i,r){return D(t).gte_tolerance(i,r)}},{key:"pLog10",value:function e(t){return D(t).pLog10()}},{key:"absLog10",value:function e(t){return D(t).absLog10()}},{key:"log10",value:function e(t){return D(t).log10()}},{key:"log",value:function e(t,i){return D(t).log(i)}},{key:"log2",value:function e(t){return D(t).log2()}},{key:"ln",value:function e(t){return D(t).ln()}},{key:"logarithm",value:function e(t,i){return D(t).logarithm(i)}},{key:"pow",value:function e(t,i){return D(t).pow(i)}},{key:"pow10",value:function e(t){return D(t).pow10()}},{key:"root",value:function e(t,i){return D(t).root(i)}},{key:"factorial",value:function e(t,i){return D(t).factorial()}},{key:"gamma",value:function e(t,i){return D(t).gamma()}},{key:"lngamma",value:function e(t,i){return D(t).lngamma()}},{key:"exp",value:function e(t){return D(t).exp()}},{key:"sqr",value:function e(t){return D(t).sqr()}},{key:"sqrt",value:function e(t){return D(t).sqrt()}},{key:"cube",value:function e(t){return D(t).cube()}},{key:"cbrt",value:function e(t){return D(t).cbrt()}},{key:"tetrate",value:function e(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:FC_NN(1,0,1);return D(t).tetrate(i,r)}},{key:"iteratedexp",value:function e(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:FC_NN(1,0,1);return D(t).iteratedexp(i,r)}},{key:"iteratedlog",value:function e(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:10,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1;return D(t).iteratedlog(i,r)}},{key:"layeradd10",value:function e(t,i){return D(t).layeradd10(i)}},{key:"layeradd",value:function e(t,i){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:10;return D(t).layeradd(i,r)}},{key:"slog",value:function e(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:10;return D(t).slog(i)}},{key:"lambertw",value:function e(t){return D(t).lambertw()}},{key:"ssqrt",value:function e(t){return D(t).ssqrt()}},{key:"pentate",value:function e(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:FC_NN(1,0,1);return D(t).pentate(i,r)}},{key:"affordGeometricSeries",value:function e(t,i,r,n){return this.affordGeometricSeries_core(D(t),D(i),D(r),n)}},{key:"sumGeometricSeries",value:function e(t,i,r,n){return this.sumGeometricSeries_core(t,D(i),D(r),n)}},{key:"affordArithmeticSeries",value:function e(t,i,r,n){return this.affordArithmeticSeries_core(D(t),D(i),D(r),D(n))}},{key:"sumArithmeticSeries",value:function e(t,i,r,n){return this.sumArithmeticSeries_core(D(t),D(i),D(r),D(n))}},{key:"efficiencyOfPurchase",value:function e(t,i,r){return this.efficiencyOfPurchase_core(D(t),D(i),D(r))}},{key:"randomDecimalForTesting",value:function e(t){if(20*Math.random()<1)return FC_NN(0,0,0);var i=Math.random()>.5?1:-1;if(20*Math.random()<1)return FC_NN(i,0,1);var r=Math.floor(Math.random()*(t+1)),n=0===r?616*Math.random()-308:16*Math.random();Math.random()>.9&&(n=Math.trunc(n));var a=Math.pow(10,n);return Math.random()>.9&&(a=Math.trunc(a)),FC(i,r,a)}},{key:"affordGeometricSeries_core",value:function t(i,r,n,a){var s=r.mul(n.pow(a));return e.floor(i.div(s).mul(n.sub(1)).add(1).log10().div(n.log10()))}},{key:"sumGeometricSeries_core",value:function t(i,r,n,a){return r.mul(n.pow(a)).mul(e.sub(1,n.pow(i))).div(e.sub(1,n))}},{key:"affordArithmeticSeries_core",value:function e(t,i,r,n){var a=i.add(n.mul(r)).sub(r.div(2)),s=a.pow(2);return a.neg().add(s.add(r.mul(t).mul(2)).sqrt()).div(r).floor()}},{key:"sumArithmeticSeries_core",value:function e(t,i,r,n){var a=i.add(n.mul(r));return t.div(2).mul(a.mul(2).plus(t.sub(1).mul(r)))}},{key:"efficiencyOfPurchase_core",value:function e(t,i,r){return t.div(i).add(t.div(r))}},{key:"slog_critical",value:function t(i,r){return i>10?r-1:e.critical_section(i,r,critical_slog_values)}},{key:"tetrate_critical",value:function t(i,r){return e.critical_section(i,r,critical_tetr_values)}},{key:"critical_section",value:function e(t,i,r){(i*=10)<0&&(i=0),i>10&&(i=10),t<2&&(t=2),t>10&&(t=10);for(var n=0,a=0,s=0;st){var u=(t-critical_headers[s])/(critical_headers[s+1]-critical_headers[s]);n=r[s][Math.floor(i)]*(1-u)+r[s+1][Math.floor(i)]*u,a=r[s][Math.ceil(i)]*(1-u)+r[s+1][Math.ceil(i)]*u;break}}var l=i-Math.floor(i);return n<=0||a<=0?n*(1-l)+a*l:Math.pow(t,Math.log(n)/Math.log(t)*(1-l)+Math.log(a)/Math.log(t)*l)}}]),e}();Decimal.dZero=FC_NN(0,0,0),Decimal.dOne=FC_NN(1,0,1),Decimal.dNegOne=FC_NN(-1,0,1),Decimal.dTwo=FC_NN(1,0,2),Decimal.dTen=FC_NN(1,0,10),Decimal.dNaN=FC_NN(Number.NaN,Number.NaN,Number.NaN),Decimal.dInf=FC_NN(1,Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY),Decimal.dNegInf=FC_NN(-1,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY),Decimal.dNumberMax=FC(1,0,Number.MAX_VALUE),Decimal.dNumberMin=FC(1,0,Number.MIN_VALUE),Decimal.fromStringCache=new LRUCache(DEFAULT_FROM_STRING_CACHE_SIZE),D=Decimal.fromValue_noAlloc,FC=Decimal.fromComponents,FC_NN=Decimal.fromComponents_noNormalize,Decimal.fromMantissaExponent,Decimal.fromMantissaExponent_noNormalize;