Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
ulp_tolerance.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ULP_TOLERANCE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ULP_TOLERANCE_HPP_
7
12#include "arene/base/stdlib_choice/enable_if.hpp"
13#include "arene/base/stdlib_choice/is_integral.hpp"
14#include "arene/base/stdlib_choice/make_signed.hpp"
16
17// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
18
19namespace arene {
20namespace base {
21
22namespace ulp_tolerance_detail {
23
24// parasoft-begin-suppress AUTOSAR-A14_5_1-a "False positive: constructor is not templated so explicit copy not needed"
25/// @brief Floating point fuzzy-equality comparator object with a particular ULP tolerance
26template <typename UlpCount>
27class ulp_tolerance_comparator {
28 /// @brief The tolerance in ULPs (units in the last place; essentially treats the bits of a float as an integer)
29 UlpCount tolerance_;
30
31 public:
32 /// @brief Construct a comparator with the given ULP tolerance
33 /// @param tol The ULP tolerance to use
34 constexpr explicit ulp_tolerance_comparator(UlpCount tol) noexcept
35 : tolerance_(tol) {}
36
37 /// @brief Get the ULP tolerance of this comparator
38 /// @return The ULP tolerance of this comparator
39 constexpr auto tolerance() const noexcept -> UlpCount { return tolerance_; }
40
41 /// @brief Perform a floating point fuzzy-equality comparison between two numbers
42 /// @tparam T A floating-point type to compare
43 /// @param left First comparison argument
44 /// @param right Second comparison argument
45 /// @return @c true iff the absolute ULP difference between @c left and @c right is less than or equal to @c tolerance
46 template <typename T, constraints<std::enable_if_t<::arene::base::enable_ulp_distance_comparable<T>>> = nullptr>
47 constexpr auto operator()(T left, T right) const noexcept -> bool {
48 auto const distance_result = ulp_distance<std::make_signed_t<UlpCount>>(left, right);
49 if (distance_result.has_error()) {
50 // This can happen if the arguments are unordered (e.g. NaNs) or their distance is too big to represent.
51 // In either case, it's fair treat these as not within tolerance.
52 return false;
53 }
54
55 // This abs is guaranteed not to go out of bounds because ulp_distance is never the most negative value.
56 return cmp_less_equal(math::abs(*distance_result), tolerance_);
57 }
58};
59// parasoft-end-suppress AUTOSAR-A14_5_1-a
60
61/// @brief Function object to create fuzzy-equality comparator objects with a particular ULP tolerance
62class ulp_tolerance_comparator_factory {
63 public:
64 /// @brief Create a floating point fuzzy-equality comparator with the given tolerance in ULPs
65 /// @tparam UlpCount An integer type
66 /// @param tolerance The maximum allowed ULP difference between two floating point numbers considered "equal"
67 /// @c tolerance is sometimes referred to as "epsilon" but this is an ambiguous term for floating point.
68 /// If <c>tolerance < 0</c> then all comparisons will return false.
69 template <typename UlpCount, constraints<std::enable_if_t<std::is_integral<UlpCount>::value>> = nullptr>
70 constexpr auto operator()(UlpCount tolerance) const noexcept -> ulp_tolerance_comparator<UlpCount> {
71 return ulp_tolerance_comparator<UlpCount>(tolerance);
72 }
73};
74
75} // namespace ulp_tolerance_detail
76
77// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object."
78// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object."
79/// @brief Create a floating-point fuzzy-equality comparator with the given ULP tolerance
80/// A negative tolerance makes all comparisons return false.
81ARENE_CPP14_INLINE_VARIABLE(ulp_tolerance_detail::ulp_tolerance_comparator_factory, ulp_tolerance);
82// parasoft-end-suppress AUTOSAR-M7_3_3-a
83// parasoft-end-suppress CERT_CPP-DCL59-a
84
85} // namespace base
86} // namespace arene
87
88// parasoft-end-suppress AUTOSAR-M2_10_1-a
89
90#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_ULP_TOLERANCE_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition accumulate.hpp:37
constexpr auto ulp_distance
Compute the distance in units in the last place between two floating-point values.
Definition accumulate.hpp:36
Provides backports of the safe integer comparison functions from C++20.