[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
- [lmi-commits] [lmi] master updated (d9955ce -> b83bdf0), Greg Chicares, 2020/08/23
- [lmi-commits] [lmi] master 9cbb041 1/7: Prefer 'git switch' to optionless 'git checkout', Greg Chicares, 2020/08/23
- [lmi-commits] [lmi] master 9553083 4/7: Prepare to round in class modal_outlay, Greg Chicares, 2020/08/23
- [lmi-commits] [lmi] master a92e5c3 5/7: Overload rounding function objects to handle vectors,
Greg Chicares <=
- [lmi-commits] [lmi] master 7ac2bd0 2/7: Say why a header is included, Greg Chicares, 2020/08/23
- [lmi-commits] [lmi] master 2ceaa93 3/7: Change order of loans and withdrawals in class modal_outlay, Greg Chicares, 2020/08/23
- [lmi-commits] [lmi] master b83bdf0 7/7: Improve concinnity, Greg Chicares, 2020/08/23
- [lmi-commits] [lmi] master 5afe8dc 6/7: Round values in class modal_outlay, Greg Chicares, 2020/08/23