UNPKG

hyperformula-dc

Version:

HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas

1,112 lines (978 loc) 33.9 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } import "core-js/modules/es.math.trunc.js"; import "core-js/modules/es.object.get-prototype-of.js"; import "core-js/modules/es.reflect.construct.js"; import "core-js/modules/es.array.slice.js"; import "core-js/modules/es.object.to-string.js"; import "core-js/modules/es.function.name.js"; import "core-js/modules/es.array.from.js"; import "core-js/modules/es.string.iterator.js"; import "core-js/modules/es.symbol.js"; import "core-js/modules/es.symbol.description.js"; import "core-js/modules/es.symbol.iterator.js"; import "core-js/modules/es.array.iterator.js"; import "core-js/modules/web.dom-collections.iterator.js"; 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 _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /** * @license * Copyright (c) 2021 Handsoncode. All rights reserved. */ import { CellError, ErrorType } from '../../Cell'; import { ErrorMessage } from '../../error-message'; import { EmptyValue, getRawValue, isExtendedNumber, NumberType } from '../InterpreterValue'; import { ArgumentTypes, FunctionPlugin } from './FunctionPlugin'; export var FinancialPlugin = /*#__PURE__*/function (_FunctionPlugin) { _inherits(FinancialPlugin, _FunctionPlugin); var _super = _createSuper(FinancialPlugin); function FinancialPlugin() { _classCallCheck(this, FinancialPlugin); return _super.apply(this, arguments); } _createClass(FinancialPlugin, [{ key: "pmt", value: function pmt(ast, state) { return this.runFunction(ast.args, state, this.metadata('PMT'), pmtCore); } }, { key: "ipmt", value: function ipmt(ast, state) { return this.runFunction(ast.args, state, this.metadata('IPMT'), ipmtCore); } }, { key: "ppmt", value: function ppmt(ast, state) { return this.runFunction(ast.args, state, this.metadata('PPMT'), ppmtCore); } }, { key: "fv", value: function fv(ast, state) { return this.runFunction(ast.args, state, this.metadata('FV'), fvCore); } }, { key: "cumipmt", value: function cumipmt(ast, state) { return this.runFunction(ast.args, state, this.metadata('CUMIPMT'), function (rate, periods, value, start, end, type) { if (start > end) { return new CellError(ErrorType.NUM, ErrorMessage.EndStartPeriod); } var acc = 0; for (var i = start; i <= end; i++) { acc += ipmtCore(rate, i, periods, value, 0, type); } return acc; }); } }, { key: "cumprinc", value: function cumprinc(ast, state) { return this.runFunction(ast.args, state, this.metadata('CUMPRINC'), function (rate, periods, value, start, end, type) { if (start > end) { return new CellError(ErrorType.NUM, ErrorMessage.EndStartPeriod); } var acc = 0; for (var i = start; i <= end; i++) { acc += ppmtCore(rate, i, periods, value, 0, type); } return acc; }); } }, { key: "db", value: function db(ast, state) { return this.runFunction(ast.args, state, this.metadata('DB'), function (cost, salvage, life, period, month) { if (month === 12 && period > life || period > life + 1) { return new CellError(ErrorType.NUM, ErrorMessage.PeriodLong); } if (salvage >= cost) { return 0; } var rate = Math.round((1 - Math.pow(salvage / cost, 1 / life)) * 1000) / 1000; var initial = cost * rate * month / 12; if (period === 1) { return initial; } var total = initial; for (var i = 0; i < period - 2; i++) { total += (cost - total) * rate; } if (period === life + 1) { return (cost - total) * rate * (12 - month) / 12; } return (cost - total) * rate; }); } }, { key: "ddb", value: function ddb(ast, state) { return this.runFunction(ast.args, state, this.metadata('DDB'), function (cost, salvage, life, period, factor) { if (period > life) { return new CellError(ErrorType.NUM); } var rate = factor / life; var oldValue; if (rate >= 1) { rate = 1; if (period === 1) { oldValue = cost; } else { oldValue = 0; } } else { oldValue = cost * Math.pow(1 - rate, period - 1); } var newValue = cost * Math.pow(1 - rate, period); return Math.max(oldValue - Math.max(salvage, newValue), 0); }); } }, { key: "dollarde", value: function dollarde(ast, state) { return this.runFunction(ast.args, state, this.metadata('DOLLARDE'), function (dollar, fraction) { if (fraction < 1) { return new CellError(ErrorType.DIV_BY_ZERO); } fraction = Math.trunc(fraction); while (fraction > 10) { fraction /= 10; } return Math.trunc(dollar) + (dollar - Math.trunc(dollar)) * 10 / fraction; }); } }, { key: "dollarfr", value: function dollarfr(ast, state) { return this.runFunction(ast.args, state, this.metadata('DOLLARFR'), function (dollar, fraction) { if (fraction < 1) { return new CellError(ErrorType.DIV_BY_ZERO); } fraction = Math.trunc(fraction); while (fraction > 10) { fraction /= 10; } return Math.trunc(dollar) + (dollar - Math.trunc(dollar)) * fraction / 10; }); } }, { key: "effect", value: function effect(ast, state) { return this.runFunction(ast.args, state, this.metadata('EFFECT'), function (rate, periods) { periods = Math.trunc(periods); return Math.pow(1 + rate / periods, periods) - 1; }); } }, { key: "ispmt", value: function ispmt(ast, state) { return this.runFunction(ast.args, state, this.metadata('ISPMT'), function (rate, period, periods, value) { if (periods === 0) { return new CellError(ErrorType.DIV_BY_ZERO); } return value * rate * (period / periods - 1); }); } }, { key: "nominal", value: function nominal(ast, state) { return this.runFunction(ast.args, state, this.metadata('NOMINAL'), function (rate, periods) { periods = Math.trunc(periods); return (Math.pow(rate + 1, 1 / periods) - 1) * periods; }); } }, { key: "nper", value: function nper(ast, state) { return this.runFunction(ast.args, state, this.metadata('NPER'), function (rate, payment, present, future, type) { if (rate === 0) { if (payment === 0) { return new CellError(ErrorType.DIV_BY_ZERO); } return (-present - future) / payment; } if (type) { payment *= 1 + rate; } return Math.log((payment - future * rate) / (present * rate + payment)) / Math.log(1 + rate); }); } }, { key: "rate", value: function rate(ast, state) { return this.runFunction(ast.args, state, this.metadata('RATE'), function (periods, payment, present, future, type, guess) { if (guess <= -1) { return new CellError(ErrorType.VALUE); } var epsMax = 1e-10; var iterMax = 20; var rate = guess; type = type ? 1 : 0; for (var i = 0; i < iterMax; i++) { if (rate <= -1) { return new CellError(ErrorType.NUM); } var y = void 0; if (Math.abs(rate) < epsMax) { y = present * (1 + periods * rate) + payment * (1 + rate * type) * periods + future; } else { var f = Math.pow(1 + rate, periods); y = present * f + payment * (1 / rate + type) * (f - 1) + future; } if (Math.abs(y) < epsMax) { return rate; } var dy = void 0; if (Math.abs(rate) < epsMax) { dy = present * periods + payment * type * periods; } else { var _f = Math.pow(1 + rate, periods); var df = periods * Math.pow(1 + rate, periods - 1); dy = present * df + payment * (1 / rate + type) * df + payment * (-1 / (rate * rate)) * (_f - 1); } rate -= y / dy; } return new CellError(ErrorType.NUM); }); } }, { key: "pv", value: function pv(ast, state) { return this.runFunction(ast.args, state, this.metadata('PV'), function (rate, periods, payment, future, type) { type = type ? 1 : 0; if (rate === -1) { if (periods === 0) { return new CellError(ErrorType.NUM); } else { return new CellError(ErrorType.DIV_BY_ZERO); } } if (rate === 0) { return -payment * periods - future; } else { return ((1 - Math.pow(1 + rate, periods)) * payment * (1 + rate * type) / rate - future) / Math.pow(1 + rate, periods); } }); } }, { key: "rri", value: function rri(ast, state) { return this.runFunction(ast.args, state, this.metadata('RRI'), function (periods, present, future) { if (present === 0 || future < 0 && present > 0 || future > 0 && present < 0) { return new CellError(ErrorType.NUM); } return Math.pow(future / present, 1 / periods) - 1; }); } }, { key: "sln", value: function sln(ast, state) { return this.runFunction(ast.args, state, this.metadata('SLN'), function (cost, salvage, life) { if (life === 0) { return new CellError(ErrorType.DIV_BY_ZERO); } return (cost - salvage) / life; }); } }, { key: "syd", value: function syd(ast, state) { return this.runFunction(ast.args, state, this.metadata('SYD'), function (cost, salvage, life, period) { if (period > life) { return new CellError(ErrorType.NUM); } return (cost - salvage) * (life - period + 1) * 2 / (life * (life + 1)); }); } }, { key: "tbilleq", value: function tbilleq(ast, state) { var _this = this; return this.runFunction(ast.args, state, this.metadata('TBILLEQ'), function (settlement, maturity, discount) { settlement = Math.round(settlement); maturity = Math.round(maturity); if (settlement >= maturity) { return new CellError(ErrorType.NUM); } var startDate = _this.dateTimeHelper.numberToSimpleDate(settlement); var endDate = _this.dateTimeHelper.numberToSimpleDate(maturity); if (endDate.year > startDate.year + 1 || endDate.year === startDate.year + 1 && (endDate.month > startDate.month || endDate.month === startDate.month && endDate.day > startDate.day)) { return new CellError(ErrorType.NUM); } var denom = 360 - discount * (maturity - settlement); if (denom === 0) { return 0; } if (denom < 0) { return new CellError(ErrorType.NUM); } return 365 * discount / denom; }); } }, { key: "tbillprice", value: function tbillprice(ast, state) { var _this2 = this; return this.runFunction(ast.args, state, this.metadata('TBILLPRICE'), function (settlement, maturity, discount) { settlement = Math.round(settlement); maturity = Math.round(maturity); if (settlement >= maturity) { return new CellError(ErrorType.NUM); } var startDate = _this2.dateTimeHelper.numberToSimpleDate(settlement); var endDate = _this2.dateTimeHelper.numberToSimpleDate(maturity); if (endDate.year > startDate.year + 1 || endDate.year === startDate.year + 1 && (endDate.month > startDate.month || endDate.month === startDate.month && endDate.day > startDate.day)) { return new CellError(ErrorType.NUM); } var denom = 360 - discount * (maturity - settlement); if (denom === 0) { return 0; } if (denom < 0) { return new CellError(ErrorType.NUM); } return 100 * (1 - discount * (maturity - settlement) / 360); }); } }, { key: "tbillyield", value: function tbillyield(ast, state) { var _this3 = this; return this.runFunction(ast.args, state, this.metadata('TBILLYIELD'), function (settlement, maturity, price) { settlement = Math.round(settlement); maturity = Math.round(maturity); if (settlement >= maturity) { return new CellError(ErrorType.NUM); } var startDate = _this3.dateTimeHelper.numberToSimpleDate(settlement); var endDate = _this3.dateTimeHelper.numberToSimpleDate(maturity); if (endDate.year > startDate.year + 1 || endDate.year === startDate.year + 1 && (endDate.month > startDate.month || endDate.month === startDate.month && endDate.day > startDate.day)) { return new CellError(ErrorType.NUM); } return (100 - price) * 360 / (price * (maturity - settlement)); }); } }, { key: "fvschedule", value: function fvschedule(ast, state) { return this.runFunction(ast.args, state, this.metadata('FVSCHEDULE'), function (value, ratios) { var vals = ratios.valuesFromTopLeftCorner(); var _iterator = _createForOfIteratorHelper(vals), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var val = _step.value; if (val instanceof CellError) { return val; } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } var _iterator2 = _createForOfIteratorHelper(vals), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var _val = _step2.value; if (isExtendedNumber(_val)) { value *= 1 + getRawValue(_val); } else if (_val !== EmptyValue) { return new CellError(ErrorType.VALUE, ErrorMessage.NumberExpected); } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } return value; }); } }, { key: "npv", value: function npv(ast, state) { var _this4 = this; return this.runFunction(ast.args, state, this.metadata('NPV'), function (rate) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } var coerced = _this4.arithmeticHelper.coerceNumbersExactRanges(args); if (coerced instanceof CellError) { return coerced; } return npvCore(rate, coerced); }); } }, { key: "mirr", value: function mirr(ast, state) { var _this5 = this; return this.runFunction(ast.args, state, this.metadata('MIRR'), function (range, frate, rrate) { var vals = _this5.arithmeticHelper.manyToExactNumbers(range.valuesFromTopLeftCorner()); if (vals instanceof CellError) { return vals; } var posFlag = false; var negFlag = false; var posValues = []; var negValues = []; var _iterator3 = _createForOfIteratorHelper(vals), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var val = _step3.value; if (val > 0) { posFlag = true; posValues.push(val); negValues.push(0); } else if (val < 0) { negFlag = true; negValues.push(val); posValues.push(0); } else { negValues.push(0); posValues.push(0); } } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } if (!posFlag || !negFlag) { return new CellError(ErrorType.DIV_BY_ZERO); } var n = vals.length; var nom = npvCore(rrate, posValues); if (nom instanceof CellError) { return nom; } var denom = npvCore(frate, negValues); if (denom instanceof CellError) { return denom; } return Math.pow(-nom * Math.pow(1 + rrate, n) / denom / (1 + frate), 1 / (n - 1)) - 1; }); } }, { key: "pduration", value: function pduration(ast, state) { return this.runFunction(ast.args, state, this.metadata('PDURATION'), function (rate, pv, fv) { return (Math.log(fv) - Math.log(pv)) / Math.log(1 + rate); }); } }, { key: "xnpv", value: function xnpv(ast, state) { return this.runFunction(ast.args, state, this.metadata('XNPV'), function (rate, values, dates) { var valArr = values.valuesFromTopLeftCorner(); var _iterator4 = _createForOfIteratorHelper(valArr), _step4; try { for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { var val = _step4.value; if (typeof val !== 'number') { return new CellError(ErrorType.VALUE, ErrorMessage.NumberExpected); } } } catch (err) { _iterator4.e(err); } finally { _iterator4.f(); } var valArrNum = valArr; var dateArr = dates.valuesFromTopLeftCorner(); var _iterator5 = _createForOfIteratorHelper(dateArr), _step5; try { for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) { var date = _step5.value; if (typeof date !== 'number') { return new CellError(ErrorType.VALUE, ErrorMessage.NumberExpected); } } } catch (err) { _iterator5.e(err); } finally { _iterator5.f(); } var dateArrNum = dateArr; if (dateArrNum.length !== valArrNum.length) { return new CellError(ErrorType.NUM, ErrorMessage.EqualLength); } var n = dateArrNum.length; var ret = 0; if (dateArrNum[0] < 0) { return new CellError(ErrorType.NUM, ErrorMessage.ValueSmall); } for (var i = 0; i < n; i++) { dateArrNum[i] = Math.floor(dateArrNum[i]); if (dateArrNum[i] < dateArrNum[0]) { return new CellError(ErrorType.NUM, ErrorMessage.ValueSmall); } ret += valArrNum[i] / Math.pow(1 + rate, (dateArrNum[i] - dateArrNum[0]) / 365); } return ret; }); } }]); return FinancialPlugin; }(FunctionPlugin); FinancialPlugin.implementedFunctions = { 'PMT': { method: 'pmt', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'IPMT': { method: 'ipmt', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'PPMT': { method: 'ppmt', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'FV': { method: 'fv', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'CUMIPMT': { method: 'cumipmt', parameters: [{ argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.INTEGER, minValue: 1 }, { argumentType: ArgumentTypes.INTEGER, minValue: 1 }, { argumentType: ArgumentTypes.INTEGER, minValue: 0, maxValue: 1 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'CUMPRINC': { method: 'cumprinc', parameters: [{ argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.INTEGER, minValue: 1 }, { argumentType: ArgumentTypes.INTEGER, minValue: 1 }, { argumentType: ArgumentTypes.INTEGER, minValue: 0, maxValue: 1 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'DB': { method: 'db', parameters: [{ argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.INTEGER, minValue: 0 }, { argumentType: ArgumentTypes.INTEGER, minValue: 0 }, { argumentType: ArgumentTypes.INTEGER, minValue: 1, maxValue: 12, defaultValue: 12 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'DDB': { method: 'ddb', parameters: [{ argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.INTEGER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0, defaultValue: 2 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'DOLLARDE': { method: 'dollarde', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, minValue: 0 }] }, 'DOLLARFR': { method: 'dollarfr', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, minValue: 0 }] }, 'EFFECT': { method: 'effect', parameters: [{ argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, minValue: 1 }], returnNumberType: NumberType.NUMBER_PERCENT }, 'ISPMT': { method: 'ispmt', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }] }, 'NOMINAL': { method: 'nominal', parameters: [{ argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, minValue: 1 }], returnNumberType: NumberType.NUMBER_PERCENT }, 'NPER': { method: 'nper', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }] }, 'PV': { method: 'pv', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'RATE': { method: 'rate', parameters: [{ argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0 }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 0.1 }], returnNumberType: NumberType.NUMBER_PERCENT }, 'RRI': { method: 'rri', parameters: [{ argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }], returnNumberType: NumberType.NUMBER_PERCENT }, 'SLN': { method: 'sln', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'SYD': { method: 'syd', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'TBILLEQ': { method: 'tbilleq', parameters: [{ argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }], returnNumberType: NumberType.NUMBER_PERCENT }, 'TBILLPRICE': { method: 'tbillprice', parameters: [{ argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'TBILLYIELD': { method: 'tbillyield', parameters: [{ argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, minValue: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }], returnNumberType: NumberType.NUMBER_PERCENT }, 'FVSCHEDULE': { method: 'fvschedule', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.RANGE }], returnNumberType: NumberType.NUMBER_CURRENCY }, 'NPV': { method: 'npv', parameters: [{ argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.ANY }], repeatLastArgs: 1, returnNumberType: NumberType.NUMBER_CURRENCY }, 'MIRR': { method: 'mirr', parameters: [{ argumentType: ArgumentTypes.RANGE }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.NUMBER }], returnNumberType: NumberType.NUMBER_PERCENT }, 'PDURATION': { method: 'pduration', parameters: [{ argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }, { argumentType: ArgumentTypes.NUMBER, greaterThan: 0 }] }, 'XNPV': { method: 'xnpv', parameters: [{ argumentType: ArgumentTypes.NUMBER, greaterThan: -1 }, { argumentType: ArgumentTypes.RANGE }, { argumentType: ArgumentTypes.RANGE }] } }; function pmtCore(rate, periods, present, future, type) { if (rate === 0) { return (-present - future) / periods; } else { var term = Math.pow(1 + rate, periods); return (future * rate + present * rate * term) * (type ? 1 / (1 + rate) : 1) / (1 - term); } } function ipmtCore(rate, period, periods, present, future, type) { var payment = pmtCore(rate, periods, present, future, type); if (period === 1) { return rate * (type ? 0 : -present); } else { return rate * (type ? fvCore(rate, period - 2, payment, present, type) - payment : fvCore(rate, period - 1, payment, present, type)); } } function fvCore(rate, periods, payment, value, type) { if (rate === 0) { return -value - payment * periods; } else { var term = Math.pow(1 + rate, periods); return payment * (type ? 1 + rate : 1) * (1 - term) / rate - value * term; } } function ppmtCore(rate, period, periods, present, future, type) { return pmtCore(rate, periods, present, future, type) - ipmtCore(rate, period, periods, present, future, type); } function npvCore(rate, args) { var acc = 0; for (var i = args.length - 1; i >= 0; i--) { acc += args[i]; if (rate === -1) { if (acc === 0) { continue; } else { return new CellError(ErrorType.DIV_BY_ZERO); } } acc /= 1 + rate; } return acc; }