spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[GCD] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-05-30 02:02:09
stats
8 file(s) changed, 74 insertions(+), 12 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Math.js
docs.sh
src/Formulas/AllFormulas.ts
src/Formulas/Math.ts
tests/Formulas/MathTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index 6ac27ce..bae3d13 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1,7 +1,5 @@
  6 # Documentation
  7 
  8-## AllFormulas
  9-
 10 ## Date
 11 
 12 
 13@@ -525,6 +523,15 @@
 14 ## Math
 15 
 16 
 17+### GCD 
 18+
 19+```
 20+  Returns the greatest common divisor of one or more integers. 
 21+@param values - The values or ranges whose factors to consider in a calculation to find the greatest common divisor. 
 22+@returns {number} greatest common divisor. 
 23+@constructor
 24+```
 25+
 26 ### ABS 
 27 
 28 ```
 29diff --git a/TODO.md b/TODO.md
 30index 20859ba..2d2efd2 100644
 31--- a/TODO.md
 32+++ b/TODO.md
 33@@ -54,7 +54,6 @@ For example 64 tbs to a qt.
 34 * COUNTBLANK
 35 * FACTDOUBLE
 36 * GAMMALN
 37-* GCD
 38 * LCM
 39 * MROUND
 40 * MULTINOMIAL
 41@@ -185,5 +184,3 @@ For example 64 tbs to a qt.
 42 * SLN
 43 * SYD
 44 * YIELD
 45-
 46-
 47diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 48index 0992648..0c33a7a 100644
 49--- a/dist/Formulas/AllFormulas.js
 50+++ b/dist/Formulas/AllFormulas.js
 51@@ -64,6 +64,7 @@ exports.GTE = Math_1.GTE;
 52 exports.LT = Math_1.LT;
 53 exports.LTE = Math_1.LTE;
 54 exports.NE = Math_1.NE;
 55+exports.GCD = Math_1.GCD;
 56 var Info_1 = require("./Info");
 57 exports.NA = Info_1.NA;
 58 var Lookup_1 = require("./Lookup");
 59diff --git a/dist/Formulas/Math.js b/dist/Formulas/Math.js
 60index 2df80ef..88dee02 100644
 61--- a/dist/Formulas/Math.js
 62+++ b/dist/Formulas/Math.js
 63@@ -7,6 +7,27 @@ var Serializer_1 = require("../Utilities/Serializer");
 64 var CriteriaFunctionFactory_1 = require("../Utilities/CriteriaFunctionFactory");
 65 var Errors_1 = require("../Errors");
 66 var MathHelpers_1 = require("../Utilities/MathHelpers");
 67+/**
 68+ * Returns the greatest common divisor of one or more integers.
 69+ * @param values - The values or ranges whose factors to consider in a calculation to find the greatest common divisor.
 70+ * @returns {number} greatest common divisor.
 71+ * @constructor
 72+ */
 73+var GCD = function () {
 74+    var values = [];
 75+    for (var _i = 0; _i < arguments.length; _i++) {
 76+        values[_i] = arguments[_i];
 77+    }
 78+    ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 1, "ABS");
 79+    // Credits: Andrew Pociu
 80+    for (var r, a, i = values.length - 1, result = values[i]; i;) {
 81+        for (a = values[--i]; (r = a % result); a = result, result = r) {
 82+            //empty
 83+        }
 84+    }
 85+    return result;
 86+};
 87+exports.GCD = GCD;
 88 /**
 89  * Returns the absolute value of a number.
 90  * @param value to get the absolute value of.
 91diff --git a/docs.sh b/docs.sh
 92index 5423868..18506ae 100755
 93--- a/docs.sh
 94+++ b/docs.sh
 95@@ -84,7 +84,7 @@ function parse_file() {
 96 # Write the header of the documentation
 97 printf "# Documentation\n\n" > $DOCUMENTATION_FILE
 98 
 99-for f in $(ls $SRC_DIRECTORY/*ts)
100+for f in $(ls $SRC_DIRECTORY/*.ts | grep -v AllFormulas)
101 do
102 	parse_file "$f"
103 done
104diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
105index f35efc1..b3d82ce 100644
106--- a/src/Formulas/AllFormulas.ts
107+++ b/src/Formulas/AllFormulas.ts
108@@ -61,7 +61,8 @@ import {
109   GTE,
110   LT,
111   LTE,
112-  NE
113+  NE,
114+  GCD
115 } from "./Math";
116 import {
117   NA
118@@ -299,5 +300,6 @@ export {
119   LTE,
120   NE,
121   NA,
122-  CHOOSE
123+  CHOOSE,
124+  GCD
125 }
126\ No newline at end of file
127diff --git a/src/Formulas/Math.ts b/src/Formulas/Math.ts
128index 2d52704..ae5e7ba 100644
129--- a/src/Formulas/Math.ts
130+++ b/src/Formulas/Math.ts
131@@ -2,7 +2,7 @@ import {
132   ArgsChecker
133 } from "../Utilities/ArgsChecker";
134 import {
135-  TypeConverter, checkForDevideByZero
136+  TypeConverter
137 } from "../Utilities/TypeConverter";
138 import {
139   Filter
140@@ -24,6 +24,25 @@ import {
141   erf
142 } from "../Utilities/MathHelpers";
143 
144+
145+/**
146+ * Returns the greatest common divisor of one or more integers.
147+ * @param values - The values or ranges whose factors to consider in a calculation to find the greatest common divisor.
148+ * @returns {number} greatest common divisor.
149+ * @constructor
150+ */
151+var GCD = function (...values) {
152+  ArgsChecker.checkAtLeastLength(arguments, 1, "ABS");
153+  // Credits: Andrew Pociu
154+  for (var r, a, i = values.length - 1, result = values[i]; i;) {
155+    for (a = values[--i]; (r = a % result); a = result, result = r) {
156+      //empty
157+    }
158+  }
159+  return result;
160+};
161+
162+
163 /**
164  * Returns the absolute value of a number.
165  * @param value to get the absolute value of.
166@@ -1240,5 +1259,6 @@ export {
167   GTE,
168   LT,
169   LTE,
170-  NE
171+  NE,
172+  GCD
173 }
174\ No newline at end of file
175diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
176index 7ef12dd..3b150d6 100644
177--- a/tests/Formulas/MathTest.ts
178+++ b/tests/Formulas/MathTest.ts
179@@ -61,7 +61,8 @@ import {
180   GTE,
181   LT,
182   LTE,
183-  NE
184+  NE,
185+  GCD
186 } from "../../src/Formulas/Math";
187 import * as ERRORS from "../../src/Errors";
188 import {
189@@ -71,6 +72,18 @@ import {
190 } from "../Utils/Asserts";
191 
192 
193+test("GCD", function(){
194+  assertEquals(GCD(10, 100), 10);
195+  assertEquals(GCD(22, 44), 22);
196+  assertEquals(GCD(18, 24), 6);
197+  assertEquals(GCD(7, 9), 1);
198+  assertEquals(GCD(14, 21, 42), 7);
199+  catchAndAssertEquals(function() {
200+    GCD.apply(this, []);
201+  }, ERRORS.NA_ERROR);
202+});
203+
204+
205 test("ABS", function(){
206   assertEquals(ABS(-10), 10);
207   assertEquals(ABS(-10.111), 10.111);