Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/catch_approx.cpp
diff options
context:
space:
mode:
authorChris Thrasher2024-05-16 13:23:38 -0600
committerChris Thrasher2024-05-18 09:42:42 -0600
commitc974e3097460a8b47d2db00ce4ee9bbb7a19cef9 (patch)
treeb0eeb90ba10c7118042ded4a3f5a75c87364beee /src/catch2/catch_approx.cpp
parent4e8d92bf02f7d1c8006a0e7a5ecabd8e62d98502 (diff)
downloadcatch3-constexpr_approx.tar.gz
Allow for `Catch::Approx` to be used in a `constexpr` contextconstexpr_approx
Diffstat (limited to 'src/catch2/catch_approx.cpp')
-rw-r--r--src/catch2/catch_approx.cpp69
1 files changed, 5 insertions, 64 deletions
diff --git a/src/catch2/catch_approx.cpp b/src/catch2/catch_approx.cpp
index 08c6799b..5d355efb 100644
--- a/src/catch2/catch_approx.cpp
+++ b/src/catch2/catch_approx.cpp
@@ -10,73 +10,14 @@
#include <catch2/internal/catch_reusable_string_stream.hpp>
#include <cmath>
-#include <limits>
-
-namespace {
-
-// Performs equivalent check of std::fabs(lhs - rhs) <= margin
-// But without the subtraction to allow for INFINITY in comparison
-bool marginComparison(double lhs, double rhs, double margin) {
- return (lhs + margin >= rhs) && (rhs + margin >= lhs);
-}
-
-}
namespace Catch {
- Approx::Approx ( double value )
- : m_epsilon( static_cast<double>(std::numeric_limits<float>::epsilon())*100. ),
- m_margin( 0.0 ),
- m_scale( 0.0 ),
- m_value( value )
- {}
-
- Approx Approx::custom() {
- return Approx( 0 );
- }
-
- Approx Approx::operator-() const {
- auto temp(*this);
- temp.m_value = -temp.m_value;
- return temp;
- }
-
-
- std::string Approx::toString() const {
- ReusableStringStream rss;
- rss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )";
- return rss.str();
- }
-
- bool Approx::equalityComparisonImpl(const double other) const {
- // First try with fixed margin, then compute margin based on epsilon, scale and Approx's value
- // Thanks to Richard Harris for his help refining the scaled margin value
- return marginComparison(m_value, other, m_margin)
- || marginComparison(m_value, other, m_epsilon * (m_scale + std::fabs(std::isinf(m_value)? 0 : m_value)));
- }
-
- void Approx::setMargin(double newMargin) {
- CATCH_ENFORCE(newMargin >= 0,
- "Invalid Approx::margin: " << newMargin << '.'
- << " Approx::Margin has to be non-negative.");
- m_margin = newMargin;
- }
-
- void Approx::setEpsilon(double newEpsilon) {
- CATCH_ENFORCE(newEpsilon >= 0 && newEpsilon <= 1.0,
- "Invalid Approx::epsilon: " << newEpsilon << '.'
- << " Approx::epsilon has to be in [0, 1]");
- m_epsilon = newEpsilon;
- }
-
-namespace literals {
- Approx operator ""_a(long double val) {
- return Approx(val);
- }
- Approx operator ""_a(unsigned long long val) {
- return Approx(val);
- }
-} // end namespace literals
+std::string Approx::toString() const {
+ ReusableStringStream rss;
+ rss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )";
+ return rss.str();
+}
std::string StringMaker<Catch::Approx>::convert(Catch::Approx const& value) {
return value.toString();