~registry/gcalctool/trunk

« back to all changes in this revision

Viewing changes to src/financial.vala

  • Committer: Robert Ancell
  • Date: 2012-10-14 03:31:40 UTC
  • Revision ID: git-v1:12ba2c81b0a81bb3ac776d1034a3c41b3173196a
Port to Vala

https://bugzilla.gnome.org/show_bug.cgi?id=640685

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
 
3
 * Copyright (C) 2008-2012 Robert Ancell
 
4
 *
 
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.
 
10
 */
 
11
 
 
12
public enum FinancialDialog
 
13
{
 
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
 
24
}
 
25
 
 
26
public void do_finc_expression (MathEquation equation, FinancialDialog function, Number arg1, Number arg2, Number arg3, Number arg4)
 
27
{
 
28
    Number result;
 
29
    switch (function)
 
30
    {
 
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;
 
64
    }
 
65
 
 
66
    equation.set_number (result);
 
67
}
 
68
 
 
69
private Number calc_ctrm (MathEquation equation, Number pint, Number fv, Number pv)
 
70
{
 
71
    /*  Cterm - pint (periodic interest rate).
 
72
     *          fv  (future value).
 
73
     *          pv  (present value).
 
74
     *
 
75
     *          RESULT = log (fv / pv) / log (1 + pint)
 
76
     */
 
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);
 
82
}
 
83
 
 
84
private Number calc_ddb (MathEquation equation, Number cost, Number life, Number period)
 
85
{
 
86
    /*  Ddb   - cost    (amount paid for asset).
 
87
     *          life    (useful life of the asset).
 
88
     *          period  (time period for depreciation allowance).
 
89
     *
 
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
 
97
     *
 
98
     */
 
99
 
 
100
    var z = new Number.integer (0);
 
101
 
 
102
    var tbv = new Number.integer (0);
 
103
    var len = period.to_integer ();
 
104
    for (var i = 0; i < len; i++)
 
105
    {
 
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? */
 
111
    }
 
112
 
 
113
    if (len >= 0)
 
114
        equation.status = _("Error: the number of periods must be positive");
 
115
 
 
116
    return z;
 
117
}
 
118
 
 
119
private Number calc_fv (MathEquation equation, Number pmt, Number pint, Number n)
 
120
{
 
121
    /*  Fv    - pmt (periodic payment).
 
122
     *          pint (periodic interest rate).
 
123
     *          n   (number of periods).
 
124
     *
 
125
     *          RESULT = pmt * (pow (1 + pint, n) - 1) / pint
 
126
     */
 
127
 
 
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);
 
133
}
 
134
 
 
135
private Number calc_gpm (MathEquation equation, Number cost, Number margin)
 
136
{
 
137
    /*  Gpm   - cost (cost of sale).
 
138
     *          margin (gross profit margin.
 
139
     *
 
140
     *          RESULT = cost / (1 - margin)
 
141
     */
 
142
 
 
143
    var t1 = new Number.integer (1);
 
144
    var t2 = t1.subtract (margin);
 
145
    return cost.divide (t2);
 
146
}
 
147
 
 
148
private Number calc_pmt (MathEquation equation, Number prin, Number pint, Number n)
 
149
{
 
150
    /*  Pmt   - prin (principal).
 
151
     *          pint  (periodic interest rate).
 
152
     *          n    (term).
 
153
     *
 
154
     *          RESULT = prin * (pint / (1 - pow (pint + 1, -1 * n)))
 
155
     */
 
156
 
 
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);
 
164
}
 
165
 
 
166
private Number calc_pv (MathEquation equation, Number pmt, Number pint, Number n)
 
167
{
 
168
    /*  Pv    - pmt (periodic payment).
 
169
     *          pint (periodic interest rate).
 
170
     *          n   (term).
 
171
     *
 
172
     *          RESULT = pmt * (1 - pow (1 + pint, -1 * n)) / pint
 
173
     */
 
174
 
 
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);
 
182
}
 
183
 
 
184
private Number calc_rate (MathEquation equation, Number fv, Number pv, Number n)
 
185
{
 
186
    /*  Rate  - fv (future value).
 
187
     *          pv (present value).
 
188
     *          n  (term).
 
189
     *
 
190
     *          RESULT = pow (fv / pv, 1 / n) - 1
 
191
     */
 
192
 
 
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));
 
198
}
 
199
 
 
200
private Number calc_sln (MathEquation equation, Number cost, Number salvage, Number life)
 
201
{
 
202
    /*  Sln   - cost    (cost of the asset).
 
203
     *          salvage (salvage value of the asset).
 
204
     *          life    (useful life of the asset).
 
205
     *
 
206
     *          RESULT = (cost - salvage) / life
 
207
     */
 
208
 
 
209
    var t1 = cost.subtract (salvage);
 
210
    return t1.divide (life);
 
211
}
 
212
 
 
213
private Number calc_syd (MathEquation equation, Number cost, Number salvage, Number life, Number period)
 
214
{
 
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).
 
219
     *
 
220
     *          RESULT = (cost - salvage) * (life - period + 1) /
 
221
     *                   (life * (life + 1)) / 2
 
222
     */
 
223
 
 
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);
 
231
}
 
232
 
 
233
private Number calc_term (MathEquation equation, Number pmt, Number fv, Number pint)
 
234
{
 
235
    /*  Term  - pmt (periodic payment).
 
236
     *          fv  (future value).
 
237
     *          pint (periodic interest rate).
 
238
     *
 
239
     *          RESULT = log (1 + (fv * pint / pmt)) / log (1 + pint)
 
240
     */
 
241
 
 
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);
 
249
}