spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Formulas.LOG written and tested.
author
Ben Vogt <[email protected]>
date
2017-02-04 22:19:34
stats
2 file(s) changed, 55 insertions(+), 0 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 2index 3299bda..b2f5d6f 100644
 3--- a/src/RawFormulas/RawFormulas.ts
 4+++ b/src/RawFormulas/RawFormulas.ts
 5@@ -135,9 +135,35 @@ var FISHER = Formula["FISHER"];
 6 var FISHERINV = Formula["FISHERINV"];
 7 var IF = Formula["IF"];
 8 var LN = Formula["LN"];
 9-var LOG = Formula["LOG"];
10 var LOG10 = Formula["LOG10"];
11 
12+/**
13+ * Returns the the logarithm of a number given a base.
14+ * @param values[0] The value for which to calculate the logarithm given base.
15+ * @param values[1] The base to use for calculation of the logarithm. Defaults to 10.
16+ * @returns {number}
17+ * @constructor
18+ */
19+var LOG = function (...values) : number {
20+  checkArgumentsAtLeastLength(values, 1);
21+  var n = firstValueAsNumber(values[0]);
22+  var b = 10;
23+  if (values.length > 1) {
24+    b = firstValueAsNumber(values[1]);
25+    if (b < 1) {
26+      throw new CellError(ERRORS.NUM_ERROR, "Function LOG parameter 2 value is " + b + ". It should be greater than 0.");
27+    }
28+  }
29+  if (b < 2) {
30+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function LOG caused a divide by zero error.");
31+  }
32+  var ln = Math.log(n);
33+  var lb = Math.log(b);
34+  if (lb === 0) {
35+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function LOG caused a divide by zero error.");
36+  }
37+  return ln / lb;
38+};
39 
40 /**
41  * Returns true if any of the provided arguments are logically true, and false if all of the provided arguments are logically false.
42diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
43index 22288c7..16d2965 100644
44--- a/tests/FormulasTest.ts
45+++ b/tests/FormulasTest.ts
46@@ -636,7 +636,32 @@ catchAndAssertEquals(function() {
47 
48 assertEquals(LN(100), 4.605170185988092);
49 
50+
51+// Test LOG
52 assertEquals(LOG(256, 2), 8);
53+assertEquals(LOG(100), 2);
54+assertEquals(LOG(100), 2);
55+assertEquals(LOG(256, 10), 2.408239965311849);
56+assertEquals(LOG(256), 2.408239965311849);
57+assertEquals(LOG("100"), 2);
58+assertEquals(LOG(1, 2), 0);
59+catchAndAssertEquals(function() {
60+  LOG("str");
61+}, ERRORS.VALUE_ERROR);
62+catchAndAssertEquals(function() {
63+  LOG(256, 0);
64+}, ERRORS.NUM_ERROR);
65+catchAndAssertEquals(function() {
66+  LOG(256, 1);
67+}, ERRORS.DIV_ZERO_ERROR);
68+catchAndAssertEquals(function() {
69+  LOG(256, false);
70+}, ERRORS.NUM_ERROR);
71+catchAndAssertEquals(function() {
72+  LOG(256, true);
73+}, ERRORS.DIV_ZERO_ERROR);
74+
75+
76 
77 assertEquals(LOG10(100), 2);
78