commit
message
[DIVIDE] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-05-25 03:41:13
stats
8 file(s) changed,
80 insertions(+),
8 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Math.js
src/Formulas/AllFormulas.ts
src/Formulas/Math.ts
tests/Formulas/MathTest.ts
tests/SheetFormulaTest.ts
1diff --git a/DOCS.md b/DOCS.md
2index a6a0087..72ddb0a 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -908,6 +908,16 @@
6 @constructor
7 ```
8
9+### DIVIDE
10+
11+```
12+ Returns one number divided by another. Equivalent to the `` operator.
13+@param dividend - The number to be divided.
14+@param divisor - The number to divide by, cannot be 0.
15+@returns {number} result of dividend divisor.
16+@constructor
17+```
18+
19 ### RAND
20
21 ```
22diff --git a/TODO.md b/TODO.md
23index 5ff5974..ffe3d3b 100644
24--- a/TODO.md
25+++ b/TODO.md
26@@ -56,7 +56,6 @@ For example 64 tbs to a qt.
27 * QUOTIENT
28 * SERIESSUM
29 * SUBTOTAL
30-* DIVIDE
31 * EQ
32 * GT
33 * GTE
34diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
35index 5c1fb7c..e1bf23b 100644
36--- a/dist/Formulas/AllFormulas.js
37+++ b/dist/Formulas/AllFormulas.js
38@@ -57,6 +57,7 @@ exports.MINUS = Math_1.MINUS;
39 exports.RAND = Math_1.RAND;
40 exports.RANDBETWEEN = Math_1.RANDBETWEEN;
41 exports.SIGN = Math_1.SIGN;
42+exports.DIVIDE = Math_1.DIVIDE;
43 var Logical_1 = require("./Logical");
44 exports.AND = Logical_1.AND;
45 exports.EXACT = Logical_1.EXACT;
46diff --git a/dist/Formulas/Math.js b/dist/Formulas/Math.js
47index 126229b..d5df9dd 100644
48--- a/dist/Formulas/Math.js
49+++ b/dist/Formulas/Math.js
50@@ -768,6 +768,23 @@ var MINUS = function (one, two) {
51 return x - y;
52 };
53 exports.MINUS = MINUS;
54+/**
55+ * Returns one number divided by another. Equivalent to the `/` operator.
56+ * @param dividend - The number to be divided.
57+ * @param divisor - The number to divide by, cannot be 0.
58+ * @returns {number} result of dividend / divisor.
59+ * @constructor
60+ */
61+var DIVIDE = function (dividend, divisor) {
62+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "DIVIDE");
63+ var x = TypeConverter_1.TypeConverter.firstValueAsNumber(dividend);
64+ var y = TypeConverter_1.TypeConverter.firstValueAsNumber(divisor);
65+ if (y < 0) {
66+ throw new Errors_1.DivZeroError("Function DIVIDE parameter 2 cannot be zero.");
67+ }
68+ return x / y;
69+};
70+exports.DIVIDE = DIVIDE;
71 /**
72 * Returns a random number between 0 inclusive and 1 exclusive.
73 * @returns {number}
74diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
75index a3fd20f..560838e 100644
76--- a/src/Formulas/AllFormulas.ts
77+++ b/src/Formulas/AllFormulas.ts
78@@ -54,7 +54,8 @@ import {
79 MINUS,
80 RAND,
81 RANDBETWEEN,
82- SIGN
83+ SIGN,
84+ DIVIDE
85 } from "./Math";
86 import {
87 AND,
88@@ -277,5 +278,6 @@ export {
89 MINUS,
90 RAND,
91 RANDBETWEEN,
92- SIGN
93+ SIGN,
94+ DIVIDE
95 }
96\ No newline at end of file
97diff --git a/src/Formulas/Math.ts b/src/Formulas/Math.ts
98index 9ebe3ca..d179daa 100644
99--- a/src/Formulas/Math.ts
100+++ b/src/Formulas/Math.ts
101@@ -2,7 +2,7 @@ import {
102 ArgsChecker
103 } from "../Utilities/ArgsChecker";
104 import {
105- TypeConverter
106+ TypeConverter, checkForDevideByZero
107 } from "../Utilities/TypeConverter";
108 import {
109 Filter
110@@ -775,6 +775,25 @@ var MINUS = function (one, two) {
111 return x - y;
112 };
113
114+
115+/**
116+ * Returns one number divided by another. Equivalent to the `/` operator.
117+ * @param dividend - The number to be divided.
118+ * @param divisor - The number to divide by, cannot be 0.
119+ * @returns {number} result of dividend / divisor.
120+ * @constructor
121+ */
122+var DIVIDE = function (dividend, divisor) {
123+ ArgsChecker.checkLength(arguments, 2, "DIVIDE");
124+ var x = TypeConverter.firstValueAsNumber(dividend);
125+ var y = TypeConverter.firstValueAsNumber(divisor);
126+ if (y < 0) {
127+ throw new DivZeroError("Function DIVIDE parameter 2 cannot be zero.");
128+ }
129+ return x / y;
130+};
131+
132+
133 /**
134 * Returns a random number between 0 inclusive and 1 exclusive.
135 * @returns {number}
136@@ -1121,5 +1140,6 @@ export {
137 COMBIN,
138 RAND,
139 RANDBETWEEN,
140- SIGN
141+ SIGN,
142+ DIVIDE
143 }
144\ No newline at end of file
145diff --git a/tests/Formulas/MathTest.ts b/tests/Formulas/MathTest.ts
146index f07fe27..bfad2d2 100644
147--- a/tests/Formulas/MathTest.ts
148+++ b/tests/Formulas/MathTest.ts
149@@ -54,7 +54,7 @@ import {
150 MINUS,
151 RAND,
152 RANDBETWEEN,
153- SIGN
154+ SIGN, DIVIDE
155 } from "../../src/Formulas/Math";
156 import * as ERRORS from "../../src/Errors";
157 import {
158@@ -1186,6 +1186,23 @@ test("MINUS", function(){
159 });
160
161
162+test("DIVIDE", function(){
163+ assertEquals(DIVIDE(2, 2), 1);
164+ assertEquals(DIVIDE(10, 2), 5);
165+ assertEquals(DIVIDE(2, "12"), 0.16666666666666666);
166+ assertEquals(DIVIDE([4, []], ["2"]), 2);
167+ catchAndAssertEquals(function() {
168+ DIVIDE.apply(this, [3.1, 1, 1]);
169+ }, ERRORS.NA_ERROR);
170+ catchAndAssertEquals(function() {
171+ DIVIDE.apply(this, [1]);
172+ }, ERRORS.NA_ERROR);
173+ catchAndAssertEquals(function() {
174+ DIVIDE.apply(this, [1, 1, 1]);
175+ }, ERRORS.NA_ERROR);
176+});
177+
178+
179 test("RAND", function(){
180 catchAndAssertEquals(function() {
181 RAND.apply(this, [3]);
182diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
183index b4e1cbf..7183548 100644
184--- a/tests/SheetFormulaTest.ts
185+++ b/tests/SheetFormulaTest.ts
186@@ -255,7 +255,11 @@ test("Sheet MULTIPLY", function(){
187 });
188
189 test("Sheet MULTIPLY", function(){
190- assertFormulaEquals('=MINUS(22, 1)', 21);
191+ assertFormulaEquals('=MULTIPLY(2, 2)', 4);
192+});
193+
194+test("Sheet DIVIDE", function(){
195+ assertFormulaEquals('=DIVIDE(22, 11)', 2);
196 });
197
198 test("Sheet SIGN", function(){