commit
message
Formulas.ATAN2 written and tested.
author
Ben Vogt <[email protected]>
date
2017-01-17 00:04:13
stats
2 file(s) changed,
57 insertions(+),
0 deletions(-)
files
src/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas.ts b/src/RawFormulas.ts
2index 6d42607..78f1356 100644
3--- a/src/RawFormulas.ts
4+++ b/src/RawFormulas.ts
5@@ -258,10 +258,42 @@ var ASINH = function (value?) {
6 };
7
8
9-var ATAN = Formula["ATAN"];
10+/**
11+ * Returns the inverse tangent of a value, in radians.
12+ * @param value The value for which to calculate the inverse tangent.
13+ * @returns {number} inverse tangent of input value
14+ * @constructor
15+ */
16+var ATAN = function (value?) {
17+ checkArgumentsLength(arguments, 1);
18+ value = valueToNumber(value);
19+ if (value === -1) {
20+ return Math.PI;
21+ } else if (value > 1 || value < -1) {
22+ throw new CellError(ERRORS.NUM_ERROR, "Function ____ parameter 1 value is " + value + ". Valid values are between -1 and 1 inclusive.");
23+ }
24+ return Math.atan(value);
25+};
26+
27+
28+/**
29+ * Returns the angle between the x-axis and a line segment from the origin (0,0) to specified coordinate pair (x,y), in radians.
30+ * @param x The x coordinate of the endpoint of the line segment for which to calculate the angle from the x-axis.
31+ * @param y The y coordinate of the endpoint of the line segment for which to calculate the angle from the x-axis.
32+ * @returns {number} angle in radians
33+ * @constructor
34+ */
35 var ATAN2 = function (x, y) {
36+ checkArgumentsLength(arguments, 2);
37+ x = valueToNumber(x);
38+ y = valueToNumber(y);
39+ if (x === 0 && y === 0) {
40+ throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function ATAN2 caused a divide by zero error.");
41+ }
42 return Math.atan2(y, x);
43 };
44+
45+
46 var ATANH = Formula["ATANH"];
47 var AVEDEV = Formula["AVEDEV"];
48 var AVERAGE = Formula["AVERAGE"];
49diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
50index bc9e2fe..f620e00 100644
51--- a/tests/FormulasTest.ts
52+++ b/tests/FormulasTest.ts
53@@ -145,9 +145,30 @@ catchAndAssertEquals(function() {
54 }, ERRORS.VALUE_ERROR);
55
56
57-assertEquals(ATAN(0.1), 0.09966865249116204);
58+// Test ATAN
59+assertEquals(ATAN(1), 0.7853981633974483);
60+assertEquals(ATAN(0), 0);
61+assertEquals(ATAN("1"), 0.7853981633974483);
62+assertEquals(ATAN(false), 0);
63+assertEquals(ATAN(true), 0.7853981633974483);
64+catchAndAssertEquals(function() {
65+ return ASINH("str");
66+}, ERRORS.VALUE_ERROR);
67
68+// Test ATAN2
69 assertEquals(ATAN2(4, 3), 0.6435011087932844);
70+assertEquals(ATAN2(-1, -1), -2.356194490192345);
71+catchAndAssertEquals(function() {
72+ return ATAN2(0, 0);
73+}, ERRORS.DIV_ZERO_ERROR);
74+assertEquals(ATAN2(1, 0), 0);
75+assertEquals(ATAN2(0, 1), 1.5707963267948966);
76+assertEquals(ATAN2(-1, "-1"), -2.356194490192345);
77+assertEquals(ATAN2(true, false), 0);
78+assertEquals(ATAN2(true, true), 0.7853981633974483);
79+catchAndAssertEquals(function() {
80+ return ATAN2("str", false);
81+}, ERRORS.VALUE_ERROR);
82
83 assertEquals(ATANH(0.44), 0.47223080442042564);
84