lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] odd/eraseme_error 7b5c07f 6/9: Move and rename a fun


From: Greg Chicares
Subject: [lmi-commits] [lmi] odd/eraseme_error 7b5c07f 6/9: Move and rename a function
Date: Thu, 8 Jul 2021 16:26:48 -0400 (EDT)

branch: odd/eraseme_error
commit 7b5c07ff92051da8f92373da6c45bb9eb4d6e7b3
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Move and rename a function
    
    Precise usage differentiates NPV from PV; this is PV.
---
 financial.hpp      | 24 ------------------------
 financial_test.cpp | 34 ++++++++++++++++++++++++++++++++--
 2 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/financial.hpp b/financial.hpp
index a69fddb..9e32f32 100644
--- a/financial.hpp
+++ b/financial.hpp
@@ -71,30 +71,6 @@ long double fv
     return z;
 }
 
-// rename; note the -100% NaN issue
-template<typename InputIterator>
-long double npv
-    (InputIterator first
-    ,InputIterator last
-    ,long double i
-    )
-{
-    if(first == last)
-        {
-        return 0.0L;
-        }
-    long double const v = 1.0L / (1.0L + i);
-    long double vn = 1.0L;
-    long double z = *first;
-    InputIterator j = first;
-    while(++j != last)
-        {
-        vn *= v;
-        z += *j * vn;
-        }
-    return z;
-}
-
 template<typename InputIterator>
 class irr_helper
 {
diff --git a/financial_test.cpp b/financial_test.cpp
index 5143b38..b32f361 100644
--- a/financial_test.cpp
+++ b/financial_test.cpp
@@ -34,6 +34,36 @@
 #include <iostream>
 #include <vector>
 
+/// Present value, for local use only--beware division by zero.
+///
+/// This could be reimplemented in terms of i rather than v for
+/// general use. The problem with using v is that i can easily
+/// be -100%, in which case v=1/(1+i)=1/0, but it is preferable
+/// to avoid division by zero.
+
+template<typename InputIterator>
+long double pv
+    (InputIterator first
+    ,InputIterator last
+    ,long double i
+    )
+{
+    if(first == last)
+        {
+        return 0.0L;
+        }
+    long double const v = 1.0L / (1.0L + i);
+    long double vn = 1.0L;
+    long double z = *first;
+    InputIterator j = first;
+    while(++j != last)
+        {
+        vn *= v;
+        z += *j * vn;
+        }
+    return z;
+}
+
 int test_main(int, char*[])
 {
     double pmts[3] = {100.0,  200.0,  300.0};
@@ -114,10 +144,10 @@ int test_main(int, char*[])
     // This NPV is -9.777068044058979E-12 in a gnumeric spreadsheet,
     // versus -9.86988e-014 with MinGW-w64 gcc-6.3.0; the 1e-13
     // tolerance is simply the materially_equal() default.
-    LMI_TEST(std::fabs(npv(q.begin(), q.end(), results.back())) <= 1e-13);
+    LMI_TEST(std::fabs(pv(q.begin(), q.end(), results.back())) <= 1e-13);
 
     // Trivially, NPV at 0% interest is summation.
-    LMI_TEST(materially_equal(-4950.0L, npv(q.begin(), q.end(), 0.0)));
+    LMI_TEST(materially_equal(-4950.0L, pv(q.begin(), q.end(), 0.0)));
 
     // Test const vectors.
     std::vector<double> const cp(p);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]