[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] valyuta/004 5a29ed5 2/6: Refactor an assertion
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] valyuta/004 5a29ed5 2/6: Refactor an assertion |
Date: |
Thu, 7 Jan 2021 04:30:41 -0500 (EST) |
branch: valyuta/004
commit 5a29ed5b184d81e01a43829753d1fe171052aa84
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Refactor an assertion
It may seem silly to assert this trivial invariant so often, but in
practice it has failed uncomfortably often.
---
account_value.hpp | 2 ++
ihs_acctval.cpp | 36 ++++++++++++++++++++++++++++++++++--
ihs_avmly.cpp | 33 +++++++--------------------------
3 files changed, 43 insertions(+), 28 deletions(-)
diff --git a/account_value.hpp b/account_value.hpp
index e81b37d..d5bfc9d 100644
--- a/account_value.hpp
+++ b/account_value.hpp
@@ -121,6 +121,8 @@ class LMI_SO AccountValue final
currency specamt_for_7702(int year) const;
currency specamt_for_7702A(int year) const;
+ void assert_pmts_add_up(char const* file, int line, int month);
+
// We're not yet entirely sure how to handle ledger values. Right now,
// we have pointers to a Ledger and also to its variant and invariant
// parts. We put data into the parts, and then insert the parts into
diff --git a/ihs_acctval.cpp b/ihs_acctval.cpp
index f4731e4..9db5a85 100644
--- a/ihs_acctval.cpp
+++ b/ihs_acctval.cpp
@@ -48,7 +48,10 @@
#include "stratified_algorithms.hpp"
#include <algorithm>
+#include <cfloat> // DECIMAL_DIG
#include <cmath>
+#include <iomanip> // setprecision()
+#include <ios> // ios_base::fixed()
#include <iterator> // back_inserter()
#include <limits>
#include <numeric>
@@ -175,6 +178,36 @@ currency AccountValue::specamt_for_7702A(int year) const
;
}
+void AccountValue::assert_pmts_add_up(char const* file, int line, int month)
+{
+ // If the currency unit is cents (as it ultimately will be), then
+ // all currency amounts should be an exact integral number of
+ // cents, and payments should add up exactly. For the nonce, the
+ // currency unit might not be cents, in which case payments should
+ // add up within a tiny tolerance.
+ bool const okay =
+#if defined CURRENCY_UNIT_IS_CENTS
+ GrossPmts[month] == EeGrossPmts[month] +
ErGrossPmts[month]
+#else // !defined CURRENCY_UNIT_IS_CENTS
+ materially_equal(GrossPmts[month].d(), EeGrossPmts[month].d() +
ErGrossPmts[month].d())
+#endif // !defined CURRENCY_UNIT_IS_CENTS
+ ;
+ if(okay)
+ return;
+
+ alarum()
+ << "Payments don't add up [file '" << file << "', line " << line <<
"]\n"
+ << month << " month\n"
+ << Year << " Year\n"
+ << std::fixed << std::setprecision(DECIMAL_DIG)
+ << EeGrossPmts[month] << " EeGrossPmts[month]\n"
+ << ErGrossPmts[month] << " ErGrossPmts[month]\n"
+ << GrossPmts[month] << " GrossPmts[month]\n"
+ << EeGrossPmts[month] + ErGrossPmts[month] << " EeGrossPmts[month] +
ErGrossPmts[month]\n"
+ << LMI_FLUSH
+ ;
+}
+
//============================================================================
std::shared_ptr<Ledger const> AccountValue::ledger_from_av() const
{
@@ -1337,8 +1370,7 @@ void AccountValue::FinalizeYear()
for(int j = 0; j < 12; ++j)
{
-// LMI_ASSERT(materially_equal(GrossPmts[j], EeGrossPmts[j] +
ErGrossPmts[j]));
- LMI_ASSERT(GrossPmts[j] == EeGrossPmts[j] + ErGrossPmts[j]);
+ assert_pmts_add_up(__FILE__, __LINE__, j);
InvariantValues().GrossPmt [Year] += GrossPmts [j].d();
InvariantValues().EeGrossPmt[Year] += EeGrossPmts[j].d();
InvariantValues().ErGrossPmt[Year] += ErGrossPmts[j].d();
diff --git a/ihs_avmly.cpp b/ihs_avmly.cpp
index dd9558a..b754b97 100644
--- a/ihs_avmly.cpp
+++ b/ihs_avmly.cpp
@@ -636,8 +636,7 @@ void AccountValue::TxExch1035()
);
}
-// LMI_ASSERT(materially_equal(GrossPmts[Month], EeGrossPmts[Month] +
ErGrossPmts[Month]));
- LMI_ASSERT(GrossPmts[Month] == EeGrossPmts[Month] + ErGrossPmts[Month]);
+ assert_pmts_add_up(__FILE__, __LINE__, Month);
}
//============================================================================
@@ -1223,8 +1222,7 @@ void AccountValue::TxAscertainDesiredPayment()
<< Month << " Month\n"
<< LMI_FLUSH
;
-// LMI_ASSERT(materially_equal(GrossPmts[Month], EeGrossPmts[Month] +
ErGrossPmts[Month]));
- LMI_ASSERT(GrossPmts[Month] == EeGrossPmts[Month] + ErGrossPmts[Month]);
+ assert_pmts_add_up(__FILE__, __LINE__, Month);
currency eepmt = C0;
if(ee_pay_this_month)
@@ -1266,8 +1264,7 @@ void AccountValue::TxAscertainDesiredPayment()
<< Month << " Month\n"
<< LMI_FLUSH
;
-// LMI_ASSERT(materially_equal(GrossPmts[Month], EeGrossPmts[Month] +
ErGrossPmts[Month]));
- LMI_ASSERT(GrossPmts[Month] == EeGrossPmts[Month] + ErGrossPmts[Month]);
+ assert_pmts_add_up(__FILE__, __LINE__, Month);
// bignum: the largest integer convertible to and from double.
LMI_ASSERT(GrossPmts[Month] < currency(1LL << 53, raw_cents{}));
@@ -1284,9 +1281,7 @@ void AccountValue::TxAscertainDesiredPayment()
GrossPmts [Month] += Dumpin;
}
-// if(!(materially_equal(GrossPmts[Month], EeGrossPmts[Month] +
ErGrossPmts[Month])))
-// LMI_ASSERT(materially_equal(GrossPmts[Month], EeGrossPmts[Month] +
ErGrossPmts[Month]));
- LMI_ASSERT(GrossPmts[Month] == EeGrossPmts[Month] + ErGrossPmts[Month]);
+ assert_pmts_add_up(__FILE__, __LINE__, Month);
}
/// Limit payment (e.g., to the non-MEC maximum).
@@ -1306,19 +1301,7 @@ void AccountValue::TxLimitPayment(double a_maxpmt)
// we shouldn't.
// TODO ?? TAXATION !! Clean this up, and put GPT limit here, on prem net of
WD.
- if(GrossPmts[Month] != EeGrossPmts[Month] + ErGrossPmts[Month])
-// if(!(materially_equal(GrossPmts[Month], EeGrossPmts[Month] +
ErGrossPmts[Month])))
- warning()
- << GrossPmts[Month] << " GrossPmts[Month]\n"
- << EeGrossPmts[Month] + ErGrossPmts[Month] << " EeGrossPmts[Month]
+ ErGrossPmts[Month]\n"
- << EeGrossPmts[Month] << " EeGrossPmts[Month]\n"
- << ErGrossPmts[Month] << " ErGrossPmts[Month]\n"
- << Year << " Year\n"
- << Month << " Month\n"
- << LMI_FLUSH
- ;
-// LMI_ASSERT(materially_equal(GrossPmts[Month], EeGrossPmts[Month] +
ErGrossPmts[Month]));
- LMI_ASSERT(GrossPmts[Month] == EeGrossPmts[Month] + ErGrossPmts[Month]);
+ assert_pmts_add_up(__FILE__, __LINE__, Month);
if(mce_reduce_prem == yare_input_.AvoidMecMethod &&
!Irc7702A_->IsMecAlready())
{
@@ -1342,8 +1325,7 @@ void AccountValue::TxLimitPayment(double a_maxpmt)
GrossPmts[Month] = EeGrossPmts[Month] + ErGrossPmts[Month];
}
-// LMI_ASSERT(materially_equal(GrossPmts[Month], EeGrossPmts[Month] +
ErGrossPmts[Month]));
- LMI_ASSERT(GrossPmts[Month] == EeGrossPmts[Month] + ErGrossPmts[Month]);
+ assert_pmts_add_up(__FILE__, __LINE__, Month);
if(Solving || mce_run_gen_curr_sep_full == RunBasis_)
{
@@ -1357,8 +1339,7 @@ void AccountValue::TxLimitPayment(double a_maxpmt)
GrossPmts[Month] = EeGrossPmts[Month] + ErGrossPmts[Month];
}
-// LMI_ASSERT(materially_equal(GrossPmts[Month], EeGrossPmts[Month] +
ErGrossPmts[Month]));
- LMI_ASSERT(GrossPmts[Month] == EeGrossPmts[Month] + ErGrossPmts[Month]);
+ assert_pmts_add_up(__FILE__, __LINE__, Month);
}
//============================================================================
- [lmi-commits] [lmi] valyuta/004 updated (d4180d4 -> 96d02bc), Greg Chicares, 2021/01/07
- [lmi-commits] [lmi] valyuta/004 ae4fa16 1/6: Restore monthly-trace precision for currency, Greg Chicares, 2021/01/07
- [lmi-commits] [lmi] valyuta/004 96d02bc 6/6: Record timings, Greg Chicares, 2021/01/07
- [lmi-commits] [lmi] valyuta/004 5a29ed5 2/6: Refactor an assertion,
Greg Chicares <=
- [lmi-commits] [lmi] valyuta/004 780c7c3 4/6: Improve accuracy when CURRENCY_UNIT_IS_CENTS not defined, Greg Chicares, 2021/01/07
- [lmi-commits] [lmi] valyuta/004 1f7a1c7 3/6: Note some ideas for future consideration, Greg Chicares, 2021/01/07
- [lmi-commits] [lmi] valyuta/004 4a3e0c2 5/6: Treat RAM as abundant, Greg Chicares, 2021/01/07