1 /*
2  * Copyright 2003-2012  Thomas Baumgart <tbaumgart@kde.org>
3  * Copyright 2017-2018  Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef MYMONEYFINANCIALCALCULATOR_P_H
20 #define MYMONEYFINANCIALCALCULATOR_P_H
21 
22 #include "mymoneyfinancialcalculator.h"
23 
24 #include <qglobal.h>
25 
26 // ----------------------------------------------------------------------------
27 // QT Includes
28 
29 #include <QString>
30 
31 // ----------------------------------------------------------------------------
32 // KDE Includes
33 
34 // ----------------------------------------------------------------------------
35 // Project Includes
36 
37 #include "mymoneyexception.h"
38 
39 class MyMoneyFinancialCalculatorPrivate
40 {
Q_DISABLE_COPY(MyMoneyFinancialCalculatorPrivate)41   Q_DISABLE_COPY(MyMoneyFinancialCalculatorPrivate)
42 
43 public:
44 
45   MyMoneyFinancialCalculatorPrivate() :
46     m_ir(0.0),
47     m_pv(0.0),
48     m_pmt(0.0),
49     m_fv(0.0),
50     m_npp(0.0),
51     m_CF(0),
52     m_PF(0),
53     m_prec(2),
54     m_bep(false),
55     m_disc(false),
56     m_mask(0)
57   {
58   }
59 
_fi(const double eint)60   double _fi(const double eint) const
61   {
62     return _Ax(eint) *(m_pv + _Cx(eint)) + m_pv + m_fv;
63   }
64 
_fip(const double eint)65   double _fip(const double eint) const
66   {
67     double AA = _Ax(eint);
68     double CC = _Cx(eint);
69     double D = (AA + 1.0) / (eint + 1.0);
70 
71     return m_npp *(m_pv + CC) * D - (AA * CC) / eint;
72   }
73 
_Ax(const double eint)74   double _Ax(const double eint) const
75   {
76     return pow((eint + 1.0), m_npp) - 1.0;
77   }
78 
_Bx(const double eint)79   double _Bx(const double eint) const
80   {
81     if (eint == 0.0)
82       throw MYMONEYEXCEPTION_CSTRING("Zero interest");
83 
84     if (m_bep == false)
85       return static_cast<double>(1.0) / eint;
86 
87     return (eint + 1.0) / eint;
88   }
89 
_Cx(const double eint)90   double _Cx(const double eint) const
91   {
92     return m_pmt * _Bx(eint);
93   }
94 
eff_int()95   double eff_int() const
96   {
97     double nint = m_ir / 100.0;
98     double eint;
99 
100     if (m_disc) {             // periodically compound?
101       if (m_CF == m_PF) {     // same frequency?
102         eint = nint / static_cast<double>(m_CF);
103 
104       } else {
105         eint = pow((static_cast<double>(1.0) + (nint / static_cast<double>(m_CF))),
106                    (static_cast<double>(m_CF) / static_cast<double>(m_PF))) - 1.0;
107 
108       }
109 
110     } else {
111       eint = exp(nint / static_cast<double>(m_PF)) - 1.0;
112     }
113 
114     return eint;
115   }
116 
nom_int(const double eint)117   double nom_int(const double eint) const
118   {
119     double nint;
120 
121     if (m_disc) {
122       if (m_CF == m_PF) {
123         nint = m_CF * eint;
124 
125       } else {
126         nint = m_CF * (pow((eint + 1.0), (static_cast<double>(m_PF) / static_cast<double>(m_CF))) - 1.0);
127       }
128     } else
129       nint = log(pow(eint + 1.0, m_PF));
130 
131     return nint;
132   }
133 
rnd(const double x)134   double rnd(const double x) const
135   {
136     double r, f;
137 
138     if (m_prec > 0) {
139       f = pow(10.0, m_prec);
140       r = static_cast<double>(qRound64(x * f) / f);
141     } else {
142       r = static_cast<double>(qRound64(x));
143     }
144     return r;
145   }
146 
147   double          m_ir;   // nominal interest rate
148   double          m_pv;   // present value
149   double          m_pmt;  // periodic payment
150   double          m_fv;   // future value
151   double          m_npp;  // number of payment periods
152 
153   unsigned short  m_CF;   // compounding frequency
154   unsigned short  m_PF;   // payment frequency
155 
156   unsigned short  m_prec; // precision for roundoff for pv, pmt and fv
157   // i is not rounded, n is integer
158 
159   bool            m_bep;  // beginning/end of period payment flag
160   bool            m_disc; // discrete/continuous compounding flag
161 
162   unsigned short m_mask; // available value mask
163 
164 };
165 
166 #endif
167 
168