spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[SYD] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-27 02:09:47
stats
8 file(s) changed, 106 insertions(+), 4 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Financial.js
src/Formulas/AllFormulas.ts
src/Formulas/Financial.ts
tests/Formulas/FinancialTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index f6eb8c2..7725e02 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -436,6 +436,18 @@
  6 @returns {number} 
  7 @constructor TODO: This function is based off of the open-source versions I was able to dig up online. We should implement a TODO:     second version that is closer to what MSExcel does and is named something like `ACCRINT.MS`.
  8 ```
  9+
 10+### SYD 
 11+
 12+```
 13+  Returns the arithmetic-declining depreciation rate. Use this function to calculate the depreciation amount for one period of the total depreciation span of an object. Arithmetic declining depreciation reduces the depreciation amount from period to period by a fixed sum. 
 14+@param cost - The initial cost of an asset. 
 15+@param salvage - the value of an asset after depreciation. 
 16+@param life - The period fixing the time span over which an asset is depreciated. 
 17+@param period - The period for which the depreciation is to be calculated. 
 18+@returns {number} 
 19+@constructor
 20+```
 21 ## Info
 22 
 23 
 24diff --git a/TODO.md b/TODO.md
 25index ddfe4bc..d572dd7 100644
 26--- a/TODO.md
 27+++ b/TODO.md
 28@@ -154,5 +154,4 @@ For example 64 tbs to a qt.
 29 * RATE
 30 * RECEIVED
 31 * SLN
 32-* SYD
 33 * YIELD
 34diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 35index a6d8af0..0b47e18 100644
 36--- a/dist/Formulas/AllFormulas.js
 37+++ b/dist/Formulas/AllFormulas.js
 38@@ -110,6 +110,7 @@ exports.DOLLAR = Financial_1.DOLLAR;
 39 exports.DOLLARDE = Financial_1.DOLLARDE;
 40 exports.DOLLARFR = Financial_1.DOLLARFR;
 41 exports.EFFECT = Financial_1.EFFECT;
 42+exports.SYD = Financial_1.SYD;
 43 var Statistical_1 = require("./Statistical");
 44 exports.AVERAGE = Statistical_1.AVERAGE;
 45 exports.AVERAGEA = Statistical_1.AVERAGEA;
 46diff --git a/dist/Formulas/Financial.js b/dist/Formulas/Financial.js
 47index c633f5c..ce37ff2 100644
 48--- a/dist/Formulas/Financial.js
 49+++ b/dist/Formulas/Financial.js
 50@@ -414,3 +414,32 @@ var ACCRINT = function (issue, firstPayment, settlement, rate, redemption, frequ
 51     return redemption * rate * factor;
 52 };
 53 exports.ACCRINT = ACCRINT;
 54+/**
 55+ * Returns the arithmetic-declining depreciation rate. Use this function to calculate the depreciation amount for one
 56+ * period of the total depreciation span of an object. Arithmetic declining depreciation reduces the depreciation amount
 57+ * from period to period by a fixed sum.
 58+ * @param cost - The initial cost of an asset.
 59+ * @param salvage - the value of an asset after depreciation.
 60+ * @param life - The period fixing the time span over which an asset is depreciated.
 61+ * @param period - The period for which the depreciation is to be calculated.
 62+ * @returns {number}
 63+ * @constructor
 64+ */
 65+var SYD = function (cost, salvage, life, period) {
 66+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 4, "SYD");
 67+    cost = TypeConverter_1.TypeConverter.firstValueAsNumber(cost);
 68+    salvage = TypeConverter_1.TypeConverter.firstValueAsNumber(salvage);
 69+    life = TypeConverter_1.TypeConverter.firstValueAsNumber(life);
 70+    period = TypeConverter_1.TypeConverter.firstValueAsNumber(period);
 71+    // Return error if period is lower than 1 or greater than life
 72+    if (period > life) {
 73+        throw new Errors_1.NumError("Function SYD parameter 4 value is " + period +
 74+            ". It should be less than or equal to value of Function SYD parameter 3 with " + life + ".");
 75+    }
 76+    if (period < 1) {
 77+        throw new Errors_1.NumError("Function SYD parameter 4 value is " + period + ". It should be greater than 0.");
 78+    }
 79+    period = Math.floor(period);
 80+    return (cost - salvage) * (life - period + 1) * 2 / (life * (life + 1));
 81+};
 82+exports.SYD = SYD;
 83diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 84index 18a08c3..a7fd134 100644
 85--- a/src/Formulas/AllFormulas.ts
 86+++ b/src/Formulas/AllFormulas.ts
 87@@ -113,7 +113,8 @@ import {
 88   DOLLAR,
 89   DOLLARDE,
 90   DOLLARFR,
 91-  EFFECT
 92+  EFFECT,
 93+  SYD
 94 } from "./Financial";
 95 import {
 96   AVERAGE,
 97@@ -365,5 +366,6 @@ export {
 98   LARGE,
 99   KURT,
100   INTERCEPT,
101-  FORECAST
102+  FORECAST,
103+  SYD
104 }
105\ No newline at end of file
106diff --git a/src/Formulas/Financial.ts b/src/Formulas/Financial.ts
107index 7fa8031..ec27be0 100644
108--- a/src/Formulas/Financial.ts
109+++ b/src/Formulas/Financial.ts
110@@ -424,6 +424,37 @@ var ACCRINT = function (issue, firstPayment, settlement, rate, redemption, frequ
111   return redemption * rate * factor;
112 };
113 
114+
115+/**
116+ * Returns the arithmetic-declining depreciation rate. Use this function to calculate the depreciation amount for one
117+ * period of the total depreciation span of an object. Arithmetic declining depreciation reduces the depreciation amount
118+ * from period to period by a fixed sum.
119+ * @param cost - The initial cost of an asset.
120+ * @param salvage - the value of an asset after depreciation.
121+ * @param life - The period fixing the time span over which an asset is depreciated.
122+ * @param period - The period for which the depreciation is to be calculated.
123+ * @returns {number}
124+ * @constructor
125+ */
126+var SYD = function (cost, salvage, life, period) {
127+  ArgsChecker.checkLength(arguments, 4, "SYD");
128+  cost = TypeConverter.firstValueAsNumber(cost);
129+  salvage = TypeConverter.firstValueAsNumber(salvage);
130+  life = TypeConverter.firstValueAsNumber(life);
131+  period = TypeConverter.firstValueAsNumber(period);
132+  // Return error if period is lower than 1 or greater than life
133+  if (period > life) {
134+    throw new NumError("Function SYD parameter 4 value is " + period +
135+        ". It should be less than or equal to value of Function SYD parameter 3 with " + life + ".");
136+  }
137+  if (period < 1) {
138+    throw new NumError("Function SYD parameter 4 value is " + period + ". It should be greater than 0.");
139+  }
140+  period = Math.floor(period);
141+
142+  return (cost - salvage) * (life - period + 1) * 2 / (life * (life + 1));
143+};
144+
145 export {
146   ACCRINT,
147   CUMPRINC,
148@@ -434,5 +465,6 @@ export {
149   DOLLARDE,
150   DOLLARFR,
151   EFFECT,
152-  PMT
153+  PMT,
154+  SYD
155 }
156\ No newline at end of file
157diff --git a/tests/Formulas/FinancialTest.ts b/tests/Formulas/FinancialTest.ts
158index c0cec49..a4f90cc 100644
159--- a/tests/Formulas/FinancialTest.ts
160+++ b/tests/Formulas/FinancialTest.ts
161@@ -8,7 +8,8 @@ import {
162   DOLLARDE,
163   DOLLARFR,
164   EFFECT,
165-  PMT
166+  PMT,
167+  SYD
168 } from "../../src/Formulas/Financial";
169 import {
170   DATE
171@@ -268,4 +269,23 @@ test("PMT", function() {
172   catchAndAssertEquals(function() {
173     PMT.apply(this, [0.05/12, 30*12, 100000, 10000, 1, "nope"]);
174   }, ERRORS.NA_ERROR);
175+});
176+
177+
178+test("SYD", function() {
179+  assertEquals(SYD(100, 22, 10, 3), 11.345454545454546);
180+  assertEquals(SYD(33.99, 22, 10, 3), 1.7440000000000002);
181+  assertEquals(SYD(39, 22, 1000, 300), 0.02381018981018981);
182+  catchAndAssertEquals(function() {
183+    SYD(39, 22, 10, 300);
184+  }, ERRORS.NUM_ERROR);
185+  catchAndAssertEquals(function() {
186+    SYD(39, 22, 10, -3);
187+  }, ERRORS.NUM_ERROR);
188+  catchAndAssertEquals(function() {
189+    SYD.apply(this, [10, 10, 10]);
190+  }, ERRORS.NA_ERROR);
191+  catchAndAssertEquals(function() {
192+    SYD.apply(this, [10, 10, 10, 10, 10]);
193+  }, ERRORS.NA_ERROR);
194 });
195\ No newline at end of file
196diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
197index ef16a66..301b23d 100644
198--- a/tests/SheetFormulaTest.ts
199+++ b/tests/SheetFormulaTest.ts
200@@ -723,6 +723,11 @@ test("Sheet FORECAST", function(){
201   assertFormulaEquals('=FORECAST([0], [1, 2, 3, 4], [10, 20, 33, 44])', 0.1791776688042246);
202 });
203 
204+test("Sheet SYD", function(){
205+  assertFormulaEquals('=SYD(100, 22, 10, 3)', 11.345454545454546);
206+});
207+
208+
209 test("Sheet *", function(){
210   assertFormulaEquals('= 10 * 10', 100);
211   assertFormulaEquals('= 10 * 0', 0);