spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[HARMEAN] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-04 17:03:58
stats
8 file(s) changed, 85 insertions(+), 5 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 ecdc272..5ab0c03 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1801,6 +1801,15 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### HARMEAN 
 11+
 12+```
 13+  Returns the harmonic mean of a data set. 
 14+@param values - The numerical arguments or ranges that represent a sample. 
 15+@returns {number} 
 16+@constructor
 17+```
 18 ## Text
 19 
 20 
 21diff --git a/TODO.md b/TODO.md
 22index 32fd62d..edd14e7 100644
 23--- a/TODO.md
 24+++ b/TODO.md
 25@@ -62,7 +62,6 @@ For example 64 tbs to a qt.
 26 * COVAR
 27 * CRITBINOM
 28 * F.DIST.RT
 29-* HARMEAN
 30 * HYPGEOMDIST
 31 * LOGINV
 32 * LOGNORMDIST
 33diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 34index 7e4c7d3..6ba5e6b 100644
 35--- a/dist/Formulas/AllFormulas.js
 36+++ b/dist/Formulas/AllFormulas.js
 37@@ -164,6 +164,7 @@ exports.NORMDIST = Statistical_1.NORMDIST;
 38 exports.NORMINV = Statistical_1.NORMINV;
 39 exports.NEGBINOMDIST = Statistical_1.NEGBINOMDIST;
 40 exports.GEOMEAN = Statistical_1.GEOMEAN;
 41+exports.HARMEAN = Statistical_1.HARMEAN;
 42 var Text_1 = require("./Text");
 43 exports.ARABIC = Text_1.ARABIC;
 44 exports.CHAR = Text_1.CHAR;
 45diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 46index 2ce18c9..b9bf072 100644
 47--- a/dist/Formulas/Statistical.js
 48+++ b/dist/Formulas/Statistical.js
 49@@ -1307,3 +1307,29 @@ var GEOMEAN = function () {
 50     return Math.pow(_product(values), 1 / values.length);
 51 };
 52 exports.GEOMEAN = GEOMEAN;
 53+/**
 54+ * Returns the harmonic mean of a data set.
 55+ * @param values - The numerical arguments or ranges that represent a sample.
 56+ * @returns {number}
 57+ * @constructor
 58+ */
 59+var HARMEAN = function () {
 60+    var values = [];
 61+    for (var _i = 0; _i < arguments.length; _i++) {
 62+        values[_i] = arguments[_i];
 63+    }
 64+    ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 1, "HARMEAN");
 65+    var range = Filter_1.Filter.flattenAndThrow(values).map(TypeConverter_1.TypeConverter.valueToNumber).map(function (value) {
 66+        if (value <= 0) {
 67+            throw new Errors_1.NumError("HARMEAN requires inputs greater than 0, but one of the values entered is " + value + ".");
 68+        }
 69+        return value;
 70+    });
 71+    var n = range.length;
 72+    var den = 0;
 73+    for (var i = 0; i < n; i++) {
 74+        den += 1 / range[i];
 75+    }
 76+    return n / den;
 77+};
 78+exports.HARMEAN = HARMEAN;
 79diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 80index 0021441..80b6f5a 100644
 81--- a/src/Formulas/AllFormulas.ts
 82+++ b/src/Formulas/AllFormulas.ts
 83@@ -169,7 +169,8 @@ import {
 84   NORMDIST,
 85   NORMINV,
 86   NEGBINOMDIST,
 87-  GEOMEAN
 88+  GEOMEAN,
 89+  HARMEAN
 90 } from "./Statistical";
 91 import {
 92   ARABIC,
 93@@ -410,5 +411,6 @@ export {
 94   NORMDIST,
 95   NORMINV,
 96   NEGBINOMDIST,
 97-  GEOMEAN
 98+  GEOMEAN,
 99+  HARMEAN
100 }
101\ No newline at end of file
102diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
103index 034b596..3efacfe 100644
104--- a/src/Formulas/Statistical.ts
105+++ b/src/Formulas/Statistical.ts
106@@ -1307,6 +1307,29 @@ var GEOMEAN  = function (...values) {
107 };
108 
109 
110+/**
111+ * Returns the harmonic mean of a data set.
112+ * @param values - The numerical arguments or ranges that represent a sample.
113+ * @returns {number}
114+ * @constructor
115+ */
116+var HARMEAN = function (...values) {
117+  ArgsChecker.checkAtLeastLength(arguments, 1, "HARMEAN");
118+  var range = Filter.flattenAndThrow(values).map(TypeConverter.valueToNumber).map(function (value) {
119+    if (value <=0) {
120+      throw new NumError("HARMEAN requires inputs greater than 0, but one of the values entered is " + value + ".");
121+    }
122+    return value;
123+  });
124+  var n = range.length;
125+  var den = 0;
126+  for (var i = 0; i < n; i++) {
127+    den += 1 / range[i];
128+  }
129+  return n / den;
130+};
131+
132+
133 export {
134   AVERAGE,
135   AVERAGEA,
136@@ -1349,5 +1372,6 @@ export {
137   NORMDIST,
138   NORMINV,
139   NEGBINOMDIST,
140-  GEOMEAN
141+  GEOMEAN,
142+  HARMEAN
143 }
144\ No newline at end of file
145diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
146index 39710a4..cc6fb5a 100644
147--- a/tests/Formulas/StatisticalTest.ts
148+++ b/tests/Formulas/StatisticalTest.ts
149@@ -40,7 +40,8 @@ import {
150   NORMDIST,
151   NORMINV,
152   NEGBINOMDIST,
153-  GEOMEAN
154+  GEOMEAN,
155+  HARMEAN
156 } from "../../src/Formulas/Statistical";
157 import * as ERRORS from "../../src/Errors";
158 import {
159@@ -857,4 +858,17 @@ test("GEOMEAN", function() {
160   catchAndAssertEquals(function() {
161     GEOMEAN.apply(this, []);
162   }, ERRORS.NA_ERROR);
163+});
164+
165+
166+test("HARMEAN", function() {
167+  assertEquals(HARMEAN(10, 4, 6, 3, 6, 7, 1, 1), 2.532027128862095);
168+  assertEquals(HARMEAN(10, 4, 6, [3, 6, [7]], 1, 1), 2.532027128862095);
169+  assertEquals(GEOMEAN(10), 10);
170+  catchAndAssertEquals(function() {
171+    HARMEAN(10, 2, 4, 5, 2, 0);
172+  }, ERRORS.NUM_ERROR);
173+  catchAndAssertEquals(function() {
174+    HARMEAN.apply(this, []);
175+  }, ERRORS.NA_ERROR);
176 });
177\ No newline at end of file
178diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
179index 31af94d..78d8f24 100644
180--- a/tests/SheetFormulaTest.ts
181+++ b/tests/SheetFormulaTest.ts
182@@ -801,6 +801,10 @@ test("Sheet GEOMEAN", function(){
183   assertFormulaEquals('=GEOMEAN(10, 4, 6, 3, 6, 7, 1, 1)', 3.6313885790189477);
184 });
185 
186+test("Sheet HARMEAN", function(){
187+  assertFormulaEquals('=HARMEAN(10, 4, 6, 3, 6, 7, 1, 1)', 2.532027128862095);
188+});
189+
190 test("Sheet *", function(){
191   assertFormulaEquals('= 10 * 10', 100);
192   assertFormulaEquals('= 10 * 0', 0);