lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master a92e5c3 5/7: Overload rounding function objec


From: Greg Chicares
Subject: [lmi-commits] [lmi] master a92e5c3 5/7: Overload rounding function objects to handle vectors
Date: Sun, 23 Aug 2020 18:00:52 -0400 (EDT)

branch: master
commit a92e5c3a276fafb95e85020e913998a9fd2d388e
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Overload rounding function objects to handle vectors
---
 round_to.hpp      | 11 +++++++++++
 round_to_test.cpp |  7 +++++++
 2 files changed, 18 insertions(+)

diff --git a/round_to.hpp b/round_to.hpp
index 52572a5..0996f6b 100644
--- a/round_to.hpp
+++ b/round_to.hpp
@@ -31,6 +31,7 @@
 #include <limits>
 #include <stdexcept>
 #include <type_traits>
+#include <vector>
 
 // Round a floating-point number to a given number of decimal places,
 // following a given rounding style.
@@ -263,6 +264,7 @@ class round_to
 
     bool operator==(round_to const&) const;
     RealType operator()(RealType r) const;
+    std::vector<RealType> operator()(std::vector<RealType> r) const;
 
     int decimals() const;
     rounding_style style() const;
@@ -359,6 +361,15 @@ inline RealType round_to<RealType>::operator()(RealType r) 
const
 }
 
 template<typename RealType>
+inline std::vector<RealType> 
round_to<RealType>::operator()(std::vector<RealType> r) const
+{
+    std::vector<RealType> z;
+    z.reserve(r.size());
+    for(auto const& i : r) {z.push_back(operator()(i));}
+    return z;
+}
+
+template<typename RealType>
 int round_to<RealType>::decimals() const
 {
     return decimals_;
diff --git a/round_to_test.cpp b/round_to_test.cpp
index d7144c1..696a168 100644
--- a/round_to_test.cpp
+++ b/round_to_test.cpp
@@ -544,6 +544,13 @@ int test_main(int, char*[])
     BOOST_TEST(2 == round1.decimals());
     BOOST_TEST(r_to_nearest == round1.style());
 
+    // Test a vector.
+    std::vector<double> const v0 {3.1415926535, 2.718281828};
+    std::vector<double> const v1 {round0(v0)};
+    BOOST_TEST_EQUAL(v0.size(), v1.size());
+    BOOST_TEST((3.14 - v1[0]) < 1e-14);
+    BOOST_TEST((2.72 - v1[1]) < 1e-14);
+
     // Try to provoke division by zero in ctor-initializer.
     //
     // nonstd::power() negates a negative exponent, but negating



reply via email to

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