[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
- [lmi-commits] [lmi] master updated (876d4b5b -> 50009ece), Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master e986f0c9 02/10: Specify types more explicitly in unit test, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master a6f2830d 01/10: Explain the necessity of a certain cast, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 3c9845b4 06/10: State the purpose of a particular unit test, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 8f5b6960 04/10: Demonstrate object sizes in unit test, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 1744d568 07/10: Regularize every mention of nextafter(), Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master e711587c 08/10: Expunge a workaround for an obsolete compiler, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 168e9a70 03/10: Specify a constant more clearly, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 7258e4ae 05/10: Establish and test explicit postconditions, Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master 50009ece 10/10: x⋅(1+ε) ≢ nextafter(x) [264], Greg Chicares, 2022/06/20
- [lmi-commits] [lmi] master e7278f90 09/10: Add some functions to complement std::nextafter(),
Greg Chicares <=