commit
message
Added Formulas.EFFECT
author
Ben Vogt <[email protected]>
date
2017-02-19 21:29:30
stats
4 file(s) changed,
52 insertions(+),
5 deletions(-)
files
README.md
src/RawFormulas/Math.ts
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/README.md b/README.md
2index dbe8ff8..5b82e97 100644
3--- a/README.md
4+++ b/README.md
5@@ -32,4 +32,6 @@ arbitrary javascript is executed in the client machine.
6
7 ### Criteria evaluations should escape reg-ex characters: http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
8
9-### Refactor `Util.ts`
10+### License for all code used.
11+
12+### Document the functions pulled in from jStat.
13\ No newline at end of file
14diff --git a/src/RawFormulas/Math.ts b/src/RawFormulas/Math.ts
15index daf72f2..1933bb9 100644
16--- a/src/RawFormulas/Math.ts
17+++ b/src/RawFormulas/Math.ts
18@@ -1385,6 +1385,7 @@ function erf(x) {
19 * @param values[2] deg_freedom2 - Required. The denominator degrees of freedom.
20 * @returns {number} inverse of the (right-tailed) F probability distribution
21 * @constructor
22+ * TODO: This function needs to be tested more thuroughly.
23 */
24 var FINV = function (...values) : number {
25 function betacf(x, a, b) {
26@@ -1796,6 +1797,27 @@ var FISHERINV = function (...values) : number {
27 return (e2y - 1) / (e2y + 1);
28 };
29
30+/**
31+ * Calculates the annual effective interest rate given the nominal rate and number of compounding periods per year.
32+ * @param values[0] nominal_rate - The nominal interest rate per year.
33+ * @param values[1] periods_per_year - The number of compounding periods per year.
34+ * @returns {number} annual effective interest rate
35+ * @constructor
36+ */
37+var EFFECT = function (...values) : number {
38+ ArgsChecker.checkLength(values, 2);
39+ var rate = TypeCaster.firstValueAsNumber(values[0]);
40+ var periods = TypeCaster.firstValueAsNumber(values[1]);
41+ if (rate <= 0) {
42+ throw new CellError(ERRORS.NUM_ERROR, "Function EFFECT parameter 1 value is " + rate + ". It should be greater than to 0");
43+ }
44+ if (periods < 1) {
45+ throw new CellError(ERRORS.NUM_ERROR, "Function EFFECT parameter 2 value is " + periods + ". It should be greater than or equal to 1");
46+ }
47+ periods = Math.floor(periods);
48+ return Math.pow(1 + rate / periods, periods) - 1;
49+};
50+
51
52 export {
53 ABS,
54@@ -1819,6 +1841,7 @@ export {
55 DEVSQ,
56 DB,
57 DDB,
58+ EFFECT,
59 EVEN,
60 ERF,
61 ERFC,
62diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
63index 34d5201..1a513cd 100644
64--- a/src/RawFormulas/RawFormulas.ts
65+++ b/src/RawFormulas/RawFormulas.ts
66@@ -23,6 +23,7 @@ import {
67 DEVSQ,
68 DB,
69 DDB,
70+ EFFECT,
71 EVEN,
72 ERF,
73 ERFC,
74@@ -125,7 +126,6 @@ var DOLLARFR = Formula["DOLLARFR"];
75 var EDATE = function (start_date: Date, months) {
76 return moment(start_date).add(months, 'months').toDate();
77 };
78-var EFFECT = Formula["EFFECT"];
79 var EOMONTH = function (start_date, months) {
80 var edate = moment(start_date).add(months, 'months');
81 return new Date(edate.year(), edate.month(), edate.daysInMonth());
82@@ -136,8 +136,6 @@ var __COMPLEX = {
83 };
84 var YEARFRAC = Formula["YEARFRAC"];
85
86-
87-
88 export {
89 __COMPLEX,
90
91diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
92index 6698913..6df35ea 100644
93--- a/tests/FormulasTest.ts
94+++ b/tests/FormulasTest.ts
95@@ -880,7 +880,31 @@ assertEquals(AND(10), true);
96
97 assertEqualsDates(EDATE(DATE(1992, 6, 24), 1), new Date('7/24/1992'));
98
99+
100+// Test EFFECT
101 assertEquals(EFFECT(0.99, 12), 1.5890167507927795);
102+assertEquals(EFFECT(0.99, 12.111), 1.5890167507927795);
103+assertEquals(EFFECT(0.99, 12.999), 1.5890167507927795);
104+assertEquals(EFFECT("100000", 12.999), 1.123182670038387e+47);
105+assertEquals(EFFECT([100000], [12.999]), 1.123182670038387e+47);
106+catchAndAssertEquals(function() {
107+ EFFECT();
108+}, ERRORS.NA_ERROR);
109+catchAndAssertEquals(function() {
110+ EFFECT(0.99);
111+}, ERRORS.NA_ERROR);
112+catchAndAssertEquals(function() {
113+ EFFECT(-0.99, 12);
114+}, ERRORS.NUM_ERROR);
115+catchAndAssertEquals(function() {
116+ EFFECT(0.99, 0);
117+}, ERRORS.NUM_ERROR);
118+catchAndAssertEquals(function() {
119+ EFFECT(0.99, "str");
120+}, ERRORS.VALUE_ERROR);
121+catchAndAssertEquals(function() {
122+ EFFECT(0.99, []);
123+}, ERRORS.REF_ERROR);
124
125 assertEqualsDates(EOMONTH(DATE(1992, 6, 24), 1), new Date('7/31/1992'));
126