lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master e7278f90 09/10: Add some functions to complem


From: Greg Chicares
Subject: [lmi-commits] [lmi] master e7278f90 09/10: Add some functions to complement std::nextafter()
Date: Mon, 20 Jun 2022 19:16:25 -0400 (EDT)

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

    Add some functions to complement std::nextafter()
    
    The std::nextafter() interface is kind of balky. These handier new
    functions can be moved to a header if they're ever wanted elsewhere.
---
 round_to_test.cpp | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/round_to_test.cpp b/round_to_test.cpp
index 0acc2777..54dd2868 100644
--- a/round_to_test.cpp
+++ b/round_to_test.cpp
@@ -152,6 +152,32 @@ class round_to_test
         );
 };
 
+namespace
+{
+template<typename T>
+/*constexpr*/ T next_outward(T t)
+{
+    static_assert(std::is_floating_point_v<T>);
+    constexpr T inf {std::numeric_limits<T>::infinity()};
+    T const outward_inf {t < 0 ? -inf : inf};
+    return std::nextafter(t, outward_inf);
+}
+
+/// |Distance| to next representable number away from zero.
+///
+/// When a limit on absolute error is wanted, it is important to
+/// choose the "outward" direction: for exact powers of two, the
+/// distance to the first number in the "inner" binade would be
+/// only half of one ULP.
+
+template<typename T>
+/*constexpr*/ T delta_outward(T t)
+{
+    static_assert(std::is_floating_point_v<T>);
+    return std::fabs(next_outward(t) - t);
+}
+} // Unnamed namespace.
+
 template<typename RealType>
 bool round_to_test::test_one_case
     (RealType       unrounded



reply via email to

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