Updated kn translations
[gcalctool.git] / src / financial.vala
blobc7c758f7cf2f099d57102ece5dc845163af6dcb2
1 /*
2 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (C) 2008-2012 Robert Ancell
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 2 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9 * license.
12 public enum FinancialDialog
14 CTRM_DIALOG,
15 DDB_DIALOG,
16 FV_DIALOG,
17 GPM_DIALOG,
18 PMT_DIALOG,
19 PV_DIALOG,
20 RATE_DIALOG,
21 SLN_DIALOG,
22 SYD_DIALOG,
23 TERM_DIALOG
26 public void do_finc_expression (MathEquation equation, FinancialDialog function, Number arg1, Number arg2, Number arg3, Number arg4)
28 Number result;
29 switch (function)
31 case FinancialDialog.CTRM_DIALOG:
32 result = calc_ctrm (equation, arg1, arg2, arg3);
33 break;
34 case FinancialDialog.DDB_DIALOG:
35 result = calc_ddb (equation, arg1, arg2, arg3);
36 break;
37 case FinancialDialog.FV_DIALOG:
38 result = calc_fv (equation, arg1, arg2, arg3);
39 break;
40 case FinancialDialog.GPM_DIALOG:
41 result = calc_gpm (equation, arg1, arg2);
42 break;
43 case FinancialDialog.PMT_DIALOG:
44 result = calc_pmt (equation, arg1, arg2, arg3);
45 break;
46 case FinancialDialog.PV_DIALOG:
47 result = calc_pv (equation, arg1, arg2, arg3);
48 break;
49 case FinancialDialog.RATE_DIALOG:
50 result = calc_rate (equation, arg1, arg2, arg3);
51 break;
52 case FinancialDialog.SLN_DIALOG:
53 result = calc_sln (equation, arg1, arg2, arg3);
54 break;
55 case FinancialDialog.SYD_DIALOG:
56 result = calc_syd (equation, arg1, arg2, arg3, arg4);
57 break;
58 case FinancialDialog.TERM_DIALOG:
59 result = calc_term (equation, arg1, arg2, arg3);
60 break;
61 default:
62 result = new Number.integer (0);
63 break;
66 equation.set_number (result);
69 private Number calc_ctrm (MathEquation equation, Number pint, Number fv, Number pv)
71 /* Cterm - pint (periodic interest rate).
72 * fv (future value).
73 * pv (present value).
75 * RESULT = log (fv / pv) / log (1 + pint)
77 var t1 = fv.divide (pv);
78 var t2 = t1.ln ();
79 var t3 = pint.add (new Number.integer (1));
80 var t4 = t3.ln ();
81 return t2.divide (t4);
84 private Number calc_ddb (MathEquation equation, Number cost, Number life, Number period)
86 /* Ddb - cost (amount paid for asset).
87 * life (useful life of the asset).
88 * period (time period for depreciation allowance).
90 * bv = 0.0;
91 * for (i = 0; i < life; i++)
92 * {
93 * VAL = ((cost - bv) * 2) / life
94 * bv += VAL
95 * }
96 * RESULT = VAL
100 var z = new Number.integer (0);
102 var tbv = new Number.integer (0);
103 var len = period.to_integer ();
104 for (var i = 0; i < len; i++)
106 var t1 = cost.subtract (tbv);
107 var t2 = t1.multiply_integer (2);
108 z = t2.divide (life);
109 t1 = tbv;
110 tbv = t1.add (z); /* TODO: why result is tbv, for next loop? */
113 if (len >= 0)
114 equation.status = _("Error: the number of periods must be positive");
116 return z;
119 private Number calc_fv (MathEquation equation, Number pmt, Number pint, Number n)
121 /* Fv - pmt (periodic payment).
122 * pint (periodic interest rate).
123 * n (number of periods).
125 * RESULT = pmt * (pow (1 + pint, n) - 1) / pint
128 var t1 = pint.add (new Number.integer (1));
129 var t2 = t1.xpowy (n);
130 var t3 = t2.add (new Number.integer (-1));
131 var t4 = pmt.multiply (t3);
132 return t4.divide (pint);
135 private Number calc_gpm (MathEquation equation, Number cost, Number margin)
137 /* Gpm - cost (cost of sale).
138 * margin (gross profit margin.
140 * RESULT = cost / (1 - margin)
143 var t1 = new Number.integer (1);
144 var t2 = t1.subtract (margin);
145 return cost.divide (t2);
148 private Number calc_pmt (MathEquation equation, Number prin, Number pint, Number n)
150 /* Pmt - prin (principal).
151 * pint (periodic interest rate).
152 * n (term).
154 * RESULT = prin * (pint / (1 - pow (pint + 1, -1 * n)))
157 var t1 = pint.add (new Number.integer (1));
158 var t2 = n.multiply_integer (-1);
159 var t3 = t1.xpowy (t2);
160 var t4 = t3.multiply_integer (-1);
161 t1 = t4.add (new Number.integer (1));
162 t2 = pint.divide (t1);
163 return prin.multiply (t2);
166 private Number calc_pv (MathEquation equation, Number pmt, Number pint, Number n)
168 /* Pv - pmt (periodic payment).
169 * pint (periodic interest rate).
170 * n (term).
172 * RESULT = pmt * (1 - pow (1 + pint, -1 * n)) / pint
175 var t1 = pint.add (new Number.integer (1));
176 var t2 = n.multiply_integer (-1);
177 var t3 = t1.xpowy (t2);
178 var t4 = t3.multiply_integer (-1);
179 t1 = t4.add (new Number.integer (1));
180 t2 = t1.divide (pint);
181 return pmt.multiply (t2);
184 private Number calc_rate (MathEquation equation, Number fv, Number pv, Number n)
186 /* Rate - fv (future value).
187 * pv (present value).
188 * n (term).
190 * RESULT = pow (fv / pv, 1 / n) - 1
193 var t1 = fv.divide (pv);
194 var t2 = new Number.integer (1);
195 var t3 = t2.divide (n);
196 var t4 = t1.xpowy (t3);
197 return t4.add (new Number.integer (-1));
200 private Number calc_sln (MathEquation equation, Number cost, Number salvage, Number life)
202 /* Sln - cost (cost of the asset).
203 * salvage (salvage value of the asset).
204 * life (useful life of the asset).
206 * RESULT = (cost - salvage) / life
209 var t1 = cost.subtract (salvage);
210 return t1.divide (life);
213 private Number calc_syd (MathEquation equation, Number cost, Number salvage, Number life, Number period)
215 /* Syd - cost (cost of the asset).
216 * salvage (salvage value of the asset).
217 * life (useful life of the asset).
218 * period (period for which depreciation is computed).
220 * RESULT = (cost - salvage) * (life - period + 1) /
221 * (life * (life + 1)) / 2
224 var t3 = life.subtract (period).add (new Number.integer (1));
225 var t2 = life.add (new Number.integer (1));
226 var t4 = life.multiply (t2);
227 var t1 = t4.divide (new Number.integer (2));
228 t2 = t3.divide (t1);
229 t1 = cost.subtract (salvage);
230 return t1.multiply (t2);
233 private Number calc_term (MathEquation equation, Number pmt, Number fv, Number pint)
235 /* Term - pmt (periodic payment).
236 * fv (future value).
237 * pint (periodic interest rate).
239 * RESULT = log (1 + (fv * pint / pmt)) / log (1 + pint)
242 var t1 = pint.add (new Number.integer (1));
243 var t2 = t1.ln ();
244 t1 = fv.multiply (pint);
245 var t3 = t1.divide (pmt);
246 var t4 = t3.add (new Number.integer (1));
247 t1 = t4.ln ();
248 return t1.divide (t2);