File indexing completed on 2024-05-12 16:42:36
0001 /* 0002 SPDX-FileCopyrightText: 2003-2012 Thomas Baumgart <tbaumgart@kde.org> 0003 SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef MYMONEYFINANCIALCALCULATOR_P_H 0008 #define MYMONEYFINANCIALCALCULATOR_P_H 0009 0010 #include "mymoneyfinancialcalculator.h" 0011 0012 #include <qglobal.h> 0013 0014 // ---------------------------------------------------------------------------- 0015 // QT Includes 0016 0017 #include <QString> 0018 0019 // ---------------------------------------------------------------------------- 0020 // KDE Includes 0021 0022 // ---------------------------------------------------------------------------- 0023 // Project Includes 0024 0025 #include "mymoneyexception.h" 0026 0027 class MyMoneyFinancialCalculatorPrivate 0028 { 0029 Q_DISABLE_COPY(MyMoneyFinancialCalculatorPrivate) 0030 0031 public: 0032 0033 MyMoneyFinancialCalculatorPrivate() : 0034 m_ir(0.0), 0035 m_pv(0.0), 0036 m_pmt(0.0), 0037 m_fv(0.0), 0038 m_npp(0.0), 0039 m_CF(0), 0040 m_PF(0), 0041 m_prec(2), 0042 m_bep(false), 0043 m_disc(false), 0044 m_mask(0) 0045 { 0046 } 0047 0048 double _fi(const double eint) const 0049 { 0050 return _Ax(eint) *(m_pv + _Cx(eint)) + m_pv + m_fv; 0051 } 0052 0053 double _fip(const double eint) const 0054 { 0055 double AA = _Ax(eint); 0056 double CC = _Cx(eint); 0057 double D = (AA + 1.0) / (eint + 1.0); 0058 0059 return m_npp *(m_pv + CC) * D - (AA * CC) / eint; 0060 } 0061 0062 double _Ax(const double eint) const 0063 { 0064 return pow((eint + 1.0), m_npp) - 1.0; 0065 } 0066 0067 double _Bx(const double eint) const 0068 { 0069 if (eint == 0.0) 0070 throw MYMONEYEXCEPTION_CSTRING("Zero interest"); 0071 0072 if (m_bep == false) 0073 return static_cast<double>(1.0) / eint; 0074 0075 return (eint + 1.0) / eint; 0076 } 0077 0078 double _Cx(const double eint) const 0079 { 0080 return m_pmt * _Bx(eint); 0081 } 0082 0083 double eff_int() const 0084 { 0085 double nint = m_ir / 100.0; 0086 double eint; 0087 0088 if (m_disc) { // periodically compound? 0089 if (m_CF == m_PF) { // same frequency? 0090 eint = nint / static_cast<double>(m_CF); 0091 0092 } else { 0093 eint = pow((static_cast<double>(1.0) + (nint / static_cast<double>(m_CF))), 0094 (static_cast<double>(m_CF) / static_cast<double>(m_PF))) - 1.0; 0095 0096 } 0097 0098 } else { 0099 eint = exp(nint / static_cast<double>(m_PF)) - 1.0; 0100 } 0101 0102 return eint; 0103 } 0104 0105 double nom_int(const double eint) const 0106 { 0107 double nint; 0108 0109 if (m_disc) { 0110 if (m_CF == m_PF) { 0111 nint = m_CF * eint; 0112 0113 } else { 0114 nint = m_CF * (pow((eint + 1.0), (static_cast<double>(m_PF) / static_cast<double>(m_CF))) - 1.0); 0115 } 0116 } else 0117 nint = log(pow(eint + 1.0, m_PF)); 0118 0119 return nint; 0120 } 0121 0122 double rnd(const double x) const 0123 { 0124 double r, f; 0125 0126 if (m_prec > 0) { 0127 f = pow(10.0, m_prec); 0128 r = static_cast<double>(qRound64(x * f) / f); 0129 } else { 0130 r = static_cast<double>(qRound64(x)); 0131 } 0132 return r; 0133 } 0134 0135 double m_ir; // nominal interest rate 0136 double m_pv; // present value 0137 double m_pmt; // periodic payment 0138 double m_fv; // future value 0139 double m_npp; // number of payment periods 0140 0141 unsigned short m_CF; // compounding frequency 0142 unsigned short m_PF; // payment frequency 0143 0144 unsigned short m_prec; // precision for roundoff for pv, pmt and fv 0145 // i is not rounded, n is integer 0146 0147 bool m_bep; // beginning/end of period payment flag 0148 bool m_disc; // discrete/continuous compounding flag 0149 0150 unsigned short m_mask; // available value mask 0151 0152 }; 0153 0154 #endif 0155