Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
float_properties.hpp
Go to the documentation of this file.
1// Copyright 2025, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_FLOAT_PROPERTIES_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_FLOAT_PROPERTIES_HPP_
7
8// IWYU pragma: private, include "arene/base/math.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
14#include "arene/base/stdlib_choice/enable_if.hpp"
15#include "arene/base/stdlib_choice/forward.hpp"
16#include "arene/base/stdlib_choice/is_floating_point.hpp"
17#include "arene/base/stdlib_choice/is_same.hpp"
18#include "arene/base/stdlib_choice/numeric_limits.hpp"
19
20// parasoft-begin-suppress CERT_CPP-DCL51-f "False positive: these names are not reserved in namespace arene::base"
21// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
22
23namespace arene {
24namespace base {
25
26// parasoft-begin-suppress AUTOSAR-M0_1_2-d "False positive: comparison result is not always false for floating point"
27// parasoft-begin-suppress AUTOSAR-M6_2_2-a "Equality is to check the properties of one value, not compare two values"
28
30ARENE_IGNORE_ALL("-Wfloat-equal", "Equality is used to check the properties of a single value, not compare two values");
31
32namespace float_properties_detail {
33
34/// @brief Check whether or not the argument has a NaN value
35/// @tparam T Type of the number to check
36/// @param num The number to check
37/// @return @c true if @c num has a NaN value, otherwise @c false
38template <typename T, constraints<std::enable_if_t<std::is_floating_point<T>::value>> = nullptr>
39constexpr auto isnan(T const num) noexcept -> bool {
40 // NOLINTNEXTLINE(misc-redundant-expression) This expression is not trivially false for floating point numbers
41 return num != num;
42}
43
44/// @brief Check whether or not the argument has a (positive or negative) infinite value
45/// @tparam T Type of the number to check
46/// @param num The number to check
47/// @return @c true if @c num has an infinite value, otherwise @c false
48template <typename T, constraints<std::enable_if_t<std::is_floating_point<T>::value>> = nullptr>
49constexpr auto isinf(T const num) noexcept -> bool {
51}
52
53/// @brief Check whether or not the argument is finite, i.e. not infinity or NaN
54/// @tparam T Type of the number to check
55/// @param num The number to check
56/// @return @c true if @c num has a finite value, otherwise @c false
57template <typename T, constraints<std::enable_if_t<std::is_floating_point<T>::value>> = nullptr>
58constexpr auto isfinite(T const num) noexcept -> bool {
59 return (!::arene::base::float_properties_detail::isnan(num)) && (!::arene::base::float_properties_detail::isinf(num));
60}
61
62/// @brief Check whether or not the argument is normal, i.e. not infinity, NaN, subnormal, or zero
63/// @tparam T Type of the number to check
64/// @param num The number to check
65/// @return @c true if @c num has a normal value, otherwise @c false
66template <typename T, constraints<std::enable_if_t<std::is_floating_point<T>::value>> = nullptr>
67constexpr auto isnormal(T num) noexcept -> bool {
68 // This explicit check for NaN is needed for GCC, because it erroneously thinks that using a NaN makes an expression
69 // no longer eligible for constexpr.
70 if (::arene::base::float_properties_detail::isnan(num)) {
71 return false;
72 }
73
74 if (num < static_cast<T>(0.0)) {
75 // This is faster than a fully correct abs because it doesn't need to deal with corner cases like signed zero/NaN;
76 // for the purposes of determining normality it's fine.
77 num = -num;
78 }
79
80 // numeric_limits::min() for a floating-point type is the smallest *normal* value.
81 return (num >= std::numeric_limits<T>::min()) && (num < std::numeric_limits<T>::infinity());
82}
83
85
86/// @brief Implementation for the @c isnan CPO
87class isnan_impl {
88 public:
89 /// @brief Check whether or not the argument is a NaN value
90 /// @tparam T Type of the value to check
91 /// @param value Value to check
92 /// @return @c isnan(std::forward<T>(value)) using ordinary lookup combined with ADL
93 template <typename T>
94 constexpr auto operator()(T&& value) const noexcept(noexcept(isnan(std::forward<T>(value))))
95 -> decltype(static_cast<void>(isnan(std::forward<T>(value))), bool{}) {
96 // parasoft-begin-suppress CERT_C-PRE31-c "False positive: static_assert is a compile-time assert and can have no
97 // side-effects"
98 static_assert(
99 std::is_same<decltype(isnan(std::forward<T>(value))), bool>::value,
100 "ADL overload of isnan must return bool"
101 );
102 // parasoft-end-suppress CERT_C-PRE31-c
103 return isnan(std::forward<T>(value));
104 }
105};
106
107/// @brief Implementation for the @c isinf CPO
108class isinf_impl {
109 public:
110 /// @brief Check whether or not the argument has a (positive or negative) infinite value
111 /// @tparam T Type of the value to check
112 /// @param value Value to check
113 /// @return @c isinf(std::forward<T>(value)) using ordinary lookup combined with ADL
114 template <typename T>
115 constexpr auto operator()(T&& value) const noexcept(noexcept(isinf(std::forward<T>(value))))
116 -> decltype(static_cast<void>(isinf(std::forward<T>(value))), bool{}) {
117 // parasoft-begin-suppress CERT_C-PRE31-c "False positive: static_assert is a compile-time assert and can have no
118 // side-effects"
119 static_assert(
120 std::is_same<decltype(isinf(std::forward<T>(value))), bool>::value,
121 "ADL overload of isinf must return bool"
122 );
123 // parasoft-end-suppress CERT_C-PRE31-c
124 return isinf(std::forward<T>(value));
125 }
126};
127
128/// @brief Implementation for the @c isfinite CPO
129class isfinite_impl {
130 public:
131 /// @brief Check whether or not the argument is finite, i.e. not infinity or NaN
132 /// @tparam T Type of the value to check
133 /// @param value Value to check
134 /// @return @c isfinite(std::forward<T>(value)) using ordinary lookup combined with ADL
135 template <typename T>
136 constexpr auto operator()(T&& value) const noexcept(noexcept(isfinite(std::forward<T>(value))))
137 -> decltype(static_cast<void>(isfinite(std::forward<T>(value))), bool{}) {
138 // parasoft-begin-suppress CERT_C-PRE31-c "False positive: static_assert is a compile-time assert and can have no
139 // side-effects"
140 static_assert(
141 std::is_same<decltype(isfinite(std::forward<T>(value))), bool>::value,
142 "ADL overload of isfinite must return bool"
143 );
144 // parasoft-end-suppress CERT_C-PRE31-c
145 return isfinite(std::forward<T>(value));
146 }
147};
148
149/// @brief Implementation for the @c isnormal CPO
150class isnormal_impl {
151 public:
152 /// @brief Check whether or not the argument is normal, i.e. not infinity, NaN, subnormal, or zero
153 /// @tparam T Type of the value to check
154 /// @param value Value to check
155 /// @return @c isnormal(std::forward<T>(value)) using ordinary lookup combined with ADL
156 template <typename T>
157 constexpr auto operator()(T&& value) const noexcept(noexcept(isnormal(std::forward<T>(value))))
158 -> decltype(static_cast<void>(isnormal(std::forward<T>(value))), bool{}) {
159 // parasoft-begin-suppress CERT_C-PRE31-c "False positive: static_assert is a compile-time assert and can have no
160 // side-effects"
161 static_assert(
162 std::is_same<decltype(isnormal(std::forward<T>(value))), bool>::value,
163 "ADL overload of isnormal must return bool"
164 );
165 // parasoft-end-suppress CERT_C-PRE31-c
166 return isnormal(std::forward<T>(value));
167 }
168};
169
170} // namespace float_properties_detail
171
172// parasoft-end-suppress AUTOSAR-M0_1_2-d
173// parasoft-end-suppress AUTOSAR-M6_2_2-a
174
175// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
176// object used in multiple TUs."
177// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
178// object used in multiple TUs."
179/// @brief Check whether a value is NaN using ordinary lookup and ADL
180ARENE_CPP14_INLINE_VARIABLE(float_properties_detail::isnan_impl, isnan);
181
182/// @brief Check whether a value is infinite using ordinary lookup and ADL
183ARENE_CPP14_INLINE_VARIABLE(float_properties_detail::isinf_impl, isinf);
184
185/// @brief Check whether a value is finite using ordinary lookup and ADL
186ARENE_CPP14_INLINE_VARIABLE(float_properties_detail::isfinite_impl, isfinite);
187
188/// @brief Check whether a value is normal using ordinary lookup and ADL
189ARENE_CPP14_INLINE_VARIABLE(float_properties_detail::isnormal_impl, isnormal);
190// parasoft-end-suppress AUTOSAR-M7_3_3-a
191// parasoft-end-suppress CERT_CPP-DCL59-a
192
193} // namespace base
194} // namespace arene
195
196// parasoft-end-suppress CERT_CPP-DCL51-f
197// parasoft-end-suppress AUTOSAR-A7_1_5-a
198
199#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_FLOAT_PROPERTIES_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
static constexpr auto min() noexcept -> T
The minimum finite value; the minimum positive normalized value for floating point types.
Definition numeric_limits.hpp:235
static constexpr auto infinity() noexcept -> T
For floating point types where has_infinity is true, a representation of positive infinity.
Definition numeric_limits.hpp:249
Provides macros used to disable and enable compiler specific diagnostics using the _Pragma operator.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
#define ARENE_IGNORE_ALL(s, comment)
Pushes an ignore for a diagnostic into the compiler for any compiler.
Definition diagnostics.hpp:122
Definition accumulate.hpp:37
Definition accumulate.hpp:36
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24