lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 4d9e5a4 3/4: Revive npv(); use it to test irr


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 4d9e5a4 3/4: Revive npv(); use it to test irr() [384]
Date: Wed, 14 Feb 2018 20:34:06 -0500 (EST)

branch: master
commit 4d9e5a4fb75314115a5f49f24ee41e3f2cfbaa96
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Revive npv(); use it to test irr() [384]
---
 financial.hpp      |  5 +----
 financial_test.cpp | 12 +++++++++++-
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/financial.hpp b/financial.hpp
index 55535d3..c58b5d7 100644
--- a/financial.hpp
+++ b/financial.hpp
@@ -66,10 +66,8 @@ long double fv
     return z / (vn * v);
 }
 
-#if 0
-// TODO ?? Have we any use for this (untested) template function?
 template<typename InputIterator>
-long double const npv
+long double npv
     (InputIterator first
     ,InputIterator last
     ,long double i
@@ -90,7 +88,6 @@ long double const npv
         }
     return z;
 }
-#endif // 0
 
 template<typename InputIterator>
 class irr_helper
diff --git a/financial_test.cpp b/financial_test.cpp
index c061d6a..14df4d2 100644
--- a/financial_test.cpp
+++ b/financial_test.cpp
@@ -94,7 +94,17 @@ int test_main(int, char*[])
     BOOST_TEST(materially_equal( 0.666120736082859, results[ 9], 1e-9));
     BOOST_TEST(materially_equal( 0.017575671480682, results[99]));
 
-    // An NPV test will soon be added here.
+    // For any stream, NPV at the IRR rate should ideally be zero.
+    std::vector<double> q{p};
+    q.push_back(-b.back());
+    double r = npv(q.begin(), q.end(), results.back());
+    // 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.
+    BOOST_TEST(std::fabs(r) <= 1e-13);
+
+    // Trivially, NPV at 0% interest is summation.
+    BOOST_TEST(materially_equal(-4950.0, npv(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]