spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[PERMUT] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-12 02:09:15
stats
8 file(s) changed, 103 insertions(+), 6 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Statistical.js
src/Formulas/AllFormulas.ts
src/Formulas/Statistical.ts
tests/Formulas/StatisticalTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index 970c2d1..5cbe883 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2019,6 +2019,16 @@
  6 @param values - Values in sample. 
  7 @constructor
  8 ```
  9+
 10+### PERMUT 
 11+
 12+```
 13+  Returns the number of permutations for a given number of objects. 
 14+@param total - The total number of objects 
 15+@param objects - The number of objects in each permutation. 
 16+@returns {number} 
 17+@constructor
 18+```
 19 ## Text
 20 
 21 
 22diff --git a/TODO.md b/TODO.md
 23index 94e569e..de1297d 100644
 24--- a/TODO.md
 25+++ b/TODO.md
 26@@ -66,7 +66,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 27 * LOGINV
 28 * LOGNORMDIST
 29 * MODE
 30-* PERMUT
 31 * PROB
 32 * RANK
 33 * RANK.AVG
 34diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 35index 9626897..d50175b 100644
 36--- a/dist/Formulas/AllFormulas.js
 37+++ b/dist/Formulas/AllFormulas.js
 38@@ -187,6 +187,7 @@ exports.VARPA = Statistical_1.VARPA;
 39 exports.VARP = Statistical_1.VARP;
 40 exports.VARA = Statistical_1.VARA;
 41 exports.VAR = Statistical_1.VAR;
 42+exports.PERMUT = Statistical_1.PERMUT;
 43 var Text_1 = require("./Text");
 44 exports.ARABIC = Text_1.ARABIC;
 45 exports.CHAR = Text_1.CHAR;
 46diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 47index 56e1fef..b27389b 100644
 48--- a/dist/Formulas/Statistical.js
 49+++ b/dist/Formulas/Statistical.js
 50@@ -1659,3 +1659,35 @@ var VAR = function () {
 51     return sigma / (count - 1);
 52 };
 53 exports.VAR = VAR;
 54+/**
 55+ * Returns the number of permutations for a given number of objects.
 56+ * @param total - The total number of objects
 57+ * @param objects - The number of objects in each permutation.
 58+ * @returns {number}
 59+ * @constructor
 60+ */
 61+var PERMUT = function (total, objects) {
 62+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "PERMUT");
 63+    total = TypeConverter_1.TypeConverter.firstValueAsNumber(total);
 64+    objects = TypeConverter_1.TypeConverter.firstValueAsNumber(objects);
 65+    if (total < objects) {
 66+        throw new Errors_1.NumError("Function PERMUT parameter 2 value is " + objects +
 67+            ", should be less than or equal to value of Function PERMUT parameter 1 of " + objects + ".");
 68+    }
 69+    var memoizeFact = [];
 70+    function _fact(value) {
 71+        var n = Math.floor(value);
 72+        if (n === 0 || n === 1) {
 73+            return 1;
 74+        }
 75+        else if (memoizeFact[n] > 0) {
 76+            return memoizeFact[n];
 77+        }
 78+        else {
 79+            memoizeFact[n] = _fact(n - 1) * n;
 80+            return memoizeFact[n];
 81+        }
 82+    }
 83+    return _fact(total) / _fact(total - objects);
 84+};
 85+exports.PERMUT = PERMUT;
 86diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 87index a5fc798..a56c3ab 100644
 88--- a/src/Formulas/AllFormulas.ts
 89+++ b/src/Formulas/AllFormulas.ts
 90@@ -192,7 +192,8 @@ import {
 91   VARPA,
 92   VARP,
 93   VARA,
 94-  VAR
 95+  VAR,
 96+  PERMUT
 97 } from "./Statistical";
 98 import {
 99   ARABIC,
100@@ -459,5 +460,6 @@ export {
101   VARPA,
102   VARP,
103   VARA,
104-  VAR
105+  VAR,
106+  PERMUT
107 }
108\ No newline at end of file
109diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
110index 0abb22b..871a693 100644
111--- a/src/Formulas/Statistical.ts
112+++ b/src/Formulas/Statistical.ts
113@@ -1643,6 +1643,37 @@ var VAR = function (...values) {
114 };
115 
116 
117+/**
118+ * Returns the number of permutations for a given number of objects.
119+ * @param total - The total number of objects
120+ * @param objects - The number of objects in each permutation.
121+ * @returns {number}
122+ * @constructor
123+ */
124+var PERMUT = function (total, objects) {
125+  ArgsChecker.checkLength(arguments, 2, "PERMUT");
126+  total = TypeConverter.firstValueAsNumber(total);
127+  objects = TypeConverter.firstValueAsNumber(objects);
128+  if (total < objects) {
129+    throw new NumError("Function PERMUT parameter 2 value is " + objects +
130+        ", should be less than or equal to value of Function PERMUT parameter 1 of " + objects + ".");
131+  }
132+  var memoizeFact = [];
133+  function _fact(value) {
134+    var n = Math.floor(value);
135+    if (n === 0 || n === 1) {
136+      return 1;
137+    } else if (memoizeFact[n] > 0) {
138+      return memoizeFact[n];
139+    } else {
140+      memoizeFact[n] = _fact(n - 1) * n;
141+      return memoizeFact[n];
142+    }
143+  }
144+  return _fact(total) / _fact(total - objects);
145+};
146+
147+
148 export {
149   AVERAGE,
150   AVERAGEA,
151@@ -1694,5 +1725,6 @@ export {
152   VARPA,
153   VARP,
154   VARA,
155-  VAR
156+  VAR,
157+  PERMUT
158 }
159\ No newline at end of file
160diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
161index ffba91a..04e2798 100644
162--- a/tests/Formulas/StatisticalTest.ts
163+++ b/tests/Formulas/StatisticalTest.ts
164@@ -49,7 +49,8 @@ import {
165   VARPA,
166   VARP,
167   VARA,
168-  VAR
169+  VAR,
170+  PERMUT
171 } from "../../src/Formulas/Statistical";
172 import * as ERRORS from "../../src/Errors";
173 import {
174@@ -1022,4 +1023,21 @@ test("VAR", function() {
175   catchAndAssertEquals(function() {
176     VAR.apply(this, []);
177   }, ERRORS.NA_ERROR);
178+});
179+
180+test("PERMUT", function() {
181+  assertEquals(PERMUT(4, 2), 12);
182+  assertEquals(PERMUT(44, 2), 1892);
183+  assertEquals(PERMUT(11, 1), 11);
184+  assertEquals(PERMUT(4, 0), 1);
185+  assertEquals(PERMUT(0, 0), 1);
186+  catchAndAssertEquals(function() {
187+    PERMUT(4, 20);
188+  }, ERRORS.NUM_ERROR);
189+  catchAndAssertEquals(function() {
190+    PERMUT.apply(this, [1]);
191+  }, ERRORS.NA_ERROR);
192+  catchAndAssertEquals(function() {
193+    PERMUT.apply(this, [1, 2, 3]);
194+  }, ERRORS.NA_ERROR);
195 });
196\ No newline at end of file
197diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
198index 139967b..4f2431f 100644
199--- a/tests/SheetFormulaTest.ts
200+++ b/tests/SheetFormulaTest.ts
201@@ -906,6 +906,10 @@ test("Sheet VAR", function(){
202   assertFormulaEquals('=VAR(1, 2, 3, 4, 5, 6, 7, 8)', 6);
203 });
204 
205+test("Sheet PERMUT", function(){
206+  assertFormulaEquals('=PERMUT(4, 2)', 12);
207+});
208+
209 
210 test("Sheet *", function(){
211   assertFormulaEquals('= 10 * 10', 100);