summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneyfinancialcalculator.cpp
blob: 33d5855db20f669daa289bfdee870f173b292ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/***************************************************************************
                          mymoneyfinancialcalculator.cpp  -  description
                             -------------------
    begin                : Tue Oct 21 2003
    copyright            : (C) 2000-2003 by Michael Edwardes
    email                : mte@users.sourceforge.net
                           Javier Campos Morales <javi_c@users.sourceforge.net>
                           Felix Rodriguez <frodriguez@users.sourceforge.net>
                           John C <thetacoturtle@users.sourceforge.net>
                           Thomas Baumgart <ipwizard@users.sourceforge.net>
                           Kevin Tambascio <ktambascio@users.sourceforge.net>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <math.h>
#include <stdio.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// ----------------------------------------------------------------------------
// QT Includes

// ----------------------------------------------------------------------------
// KDE Includes

// ----------------------------------------------------------------------------
// Project Includes


#include "mymoneyfinancialcalculator.h"
#include "mymoneyexception.h"

// #ifndef HAVE_ROUND
// #undef roundl
// #define roundl(a)  rnd(a)

FCALC_DOUBLE MyMoneyFinancialCalculator::rnd(const FCALC_DOUBLE x) const
{
  FCALC_DOUBLE r,f;

  if(m_prec > 0) {
#ifdef HAVE_ROUND
    f = powl(10.0, m_prec);
    r = roundl(x * f)/f;
#else
    char  buf[50];
#if HAVE_LONG_DOUBLE
    sprintf (buf, "%.*Lf", m_prec, x);
    sscanf (buf, "%Lf", &r);
#else
    sprintf (buf, "%.*f", m_prec, x);
    sscanf (buf, "%lf", &r);
#endif
#endif
  } else
    r = roundl(x);
  return r;
}
// #endif

static inline FCALC_DOUBLE dabs(const FCALC_DOUBLE x)
{
  return (x >= 0.0) ? x : -x;
}

MyMoneyFinancialCalculator::MyMoneyFinancialCalculator()
{
  setPrec();
  setPF();
  setCF();
  setBep();
  setDisc();

  setNpp(0.0);
  setIr(0.0);
  setPv(0.0);
  setPmt(0.0);
  setFv(0.0);

  // clear the tqmask
  m_tqmask = 0;
}

MyMoneyFinancialCalculator::~MyMoneyFinancialCalculator()
{
}

void MyMoneyFinancialCalculator::setPrec(const unsigned short prec)
{
  m_prec = prec;
}

void MyMoneyFinancialCalculator::setPF(const unsigned short PF)
{
  m_PF = PF;
}

void MyMoneyFinancialCalculator::setCF(const unsigned short CF)
{
  m_CF = CF;
}

void MyMoneyFinancialCalculator::setBep(const bool bep)
{
  m_bep = bep;
}

void MyMoneyFinancialCalculator::setDisc(const bool disc)
{
  m_disc = disc;
}

void MyMoneyFinancialCalculator::setIr(const FCALC_DOUBLE ir)
{
  m_ir = ir;
  m_tqmask |= IR_SET;
}

void MyMoneyFinancialCalculator::setPv(const FCALC_DOUBLE pv)
{
  m_pv = pv;
  m_tqmask |= PV_SET;
}

void MyMoneyFinancialCalculator::setPmt(const FCALC_DOUBLE pmt)
{
  m_pmt = pmt;
  m_tqmask |= PMT_SET;
}

void MyMoneyFinancialCalculator::setNpp(const FCALC_DOUBLE npp)
{
  m_npp = npp;
  m_tqmask |= NPP_SET;
}

void MyMoneyFinancialCalculator::setFv(const FCALC_DOUBLE fv)
{
  m_fv = fv;
  m_tqmask |= FV_SET;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::numPayments(void)
{
  const unsigned short tqmask = PV_SET | IR_SET | PMT_SET | FV_SET;

  if((m_tqmask & tqmask) != tqmask)
    throw new MYMONEYEXCEPTION("Not all parameters set for calculation of numPayments");

  FCALC_DOUBLE eint = eff_int();
  FCALC_DOUBLE CC = _Cx(eint);

  CC = (CC - m_fv) / (CC + m_pv);
  m_npp = (CC > 0.0) ? logl (CC) / logl (eint +1.0) : 0.0;

  m_tqmask |= NPP_SET;
  return m_npp;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::payment(void)
{
  const unsigned short tqmask = PV_SET | IR_SET | NPP_SET | FV_SET;

  if((m_tqmask & tqmask) != tqmask)
    throw new MYMONEYEXCEPTION("Not all parameters set for calculation of payment");

  FCALC_DOUBLE eint = eff_int();
  FCALC_DOUBLE AA = _Ax(eint);
  FCALC_DOUBLE BB = _Bx(eint);

  m_pmt = -rnd((m_fv + m_pv * (AA + 1.0)) / (AA * BB));
  //m_pmt = -floorl((m_fv + m_pv * (AA + 1.0)) / (AA * BB));

  m_tqmask |= PMT_SET;
  return m_pmt;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::presentValue(void)
{
  const unsigned short tqmask = PMT_SET | IR_SET | NPP_SET | FV_SET;

  if((m_tqmask & tqmask) != tqmask)
    throw new MYMONEYEXCEPTION("Not all parameters set for calculation of payment");

  FCALC_DOUBLE eint = eff_int();
  FCALC_DOUBLE AA = _Ax(eint);
  FCALC_DOUBLE CC = _Cx(eint);

  m_pv = rnd(-(m_fv + (AA * CC)) / (AA + 1.0));

  m_tqmask |= PV_SET;
  return m_pv;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::futureValue(void)
{
  const unsigned short tqmask = PMT_SET | IR_SET | NPP_SET | PV_SET;

  if((m_tqmask & tqmask) != tqmask)
    throw new MYMONEYEXCEPTION("Not all parameters set for calculation of payment");

  FCALC_DOUBLE eint = eff_int();
  FCALC_DOUBLE AA = _Ax(eint);
  FCALC_DOUBLE CC = _Cx(eint);
  m_fv = rnd(-(m_pv + AA * (m_pv + CC)));

  m_tqmask |= FV_SET;
  return m_fv;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::interestRate(void)
{
  FCALC_DOUBLE eint = 0.0;
  FCALC_DOUBLE a = 0.0;
  FCALC_DOUBLE dik = 0.0;

  const FCALC_DOUBLE ratio = 1e4;
  int ri;

  if (m_pmt == 0.0) {
    eint = powl ((dabs (m_fv) / dabs (m_pv)), (1.0 / m_npp)) - 1.0;
  } else {
    if ((m_pmt * m_fv) < 0.0) {
      if(m_pv)
        a = -1.0;
      else
        a = 1.0;
      eint =
        dabs ((m_fv + a * m_npp * m_pmt) /
              (3.0 *
               ((m_npp - 1.0) * (m_npp - 1.0) * m_pmt + m_pv -
                m_fv)));
    } else {
      if ((m_pv * m_pmt) < 0.0) {
        eint = dabs ((m_npp * m_pmt + m_pv + m_fv) / (m_npp * m_pv));
      } else {
        a = dabs (m_pmt / (dabs(m_pv) + dabs(m_fv)));
        eint = a + 1.0 / (a * m_npp * m_npp * m_npp);
      }
    }
    do {
      try {
        dik = _fi(eint) / _fip(eint);
        eint -= dik;
      } catch(MyMoneyException *e) {
        delete e;
        eint = 0;
      }
      (void) modfl(ratio * (dik / eint), &a);
      ri = static_cast<unsigned> (a);
    }
    while (ri);
  }
  m_tqmask |= IR_SET;
  m_ir = rnd(nom_int(eint) * 100.0);
  return m_ir;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::_fi(const FCALC_DOUBLE eint) const
{
  return _Ax(eint) * (m_pv + _Cx(eint)) + m_pv + m_fv;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::_fip(const FCALC_DOUBLE eint) const
{
  double AA = _Ax(eint);
  double CC = _Cx(eint);
  double D = (AA + 1.0) / (eint + 1.0);

  return m_npp *(m_pv + CC) * D - (AA * CC) / eint;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::_Ax(const FCALC_DOUBLE eint) const
{
  return powl ((eint + 1.0), m_npp) - 1.0;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::_Bx(const FCALC_DOUBLE eint) const
{
  if(eint == 0.0)
    throw new MYMONEYEXCEPTION("Zero interest");

  if(m_bep == false)
    return static_cast<FCALC_DOUBLE>(1.0) / eint;

  return (eint + 1.0) / eint;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::_Cx(const FCALC_DOUBLE eint) const
{
  return m_pmt * _Bx(eint);
}

FCALC_DOUBLE MyMoneyFinancialCalculator::eff_int(void) const
{
  FCALC_DOUBLE nint = m_ir / 100.0;
  FCALC_DOUBLE eint;

  if(m_disc) {              // periodically compound?
    if(m_CF == m_PF) {      // same frequency?
      eint = nint / static_cast<FCALC_DOUBLE>(m_CF);

    } else {
      eint = powl((static_cast<FCALC_DOUBLE>(1.0) + (nint / static_cast<FCALC_DOUBLE>(m_CF))),
                  (static_cast<FCALC_DOUBLE>(m_CF) / static_cast<FCALC_DOUBLE>(m_PF))) - 1.0;

    }

  } else {
    eint = expl(nint / static_cast<FCALC_DOUBLE>(m_PF)) - 1.0;
  }

  return eint;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::nom_int(const FCALC_DOUBLE eint) const
{
  FCALC_DOUBLE nint;

  if(m_disc) {
    if(m_CF == m_PF) {
      nint = m_CF * eint;

    } else {
      nint = m_CF * (powl ((eint + 1.0), (static_cast<FCALC_DOUBLE>(m_PF) / static_cast<FCALC_DOUBLE>(m_CF))) - 1.0);
    }
  } else
    nint = logl (powl (eint + 1.0, m_PF));

  return nint;
}

FCALC_DOUBLE MyMoneyFinancialCalculator::interestDue(void) const
{
  FCALC_DOUBLE eint = eff_int();

  return (m_pv + (m_bep ? m_pmt : 0.0)) * eint;
}