ENH: Add constexpr overloads for itk::Math::abs#5797
Conversation
4fcc6d0 to
0d1a52b
Compare
eb506bd to
60ab154
Compare
a19ef23 to
d71da7d
Compare
d71da7d to
236f95f
Compare
Include bool, integral, and floating-point support Refactored `itk::Math::abs` with constexpr for bool, integer and floating point types. Conditionally use `std::abs` within itk:: when constexpr is available (i.e. c++23 and greater), or revert to custom constexpr code for c++17-c++23. Maintain backward compatibility with pre-ITKv6.0 computations while enabling explicit `std::abs` usage for modern standards. Signed types are promoted to unsigned return types for integers. Remove "using std::abs" which was polluting the itk namespace with hard to interpret intent, provide explicit support for all itk types with the itk::safe_abs(). itk::safe_abs is constexpr compatible for bool, floating, and integer types.
83c0d97 to
3cc3f4a
Compare
| /** | ||
| * @brief Returns the absolute value of a number. | ||
| * | ||
| * This function provides a c++17 implementation of the vnl_math::abs absolute value functionality. | ||
| * safe_abs preserves backward compatibility with vnl_math::abs that was originally used in ITK. | ||
| * | ||
| * Where std::abs returns the absolute value in the same type as the input, itk::safe_abs | ||
| * returns the absolute value in the unsigned equivalent of the input type. | ||
| * | ||
| * @tparam T The type of the input number. | ||
| * @param x The input number. | ||
| * @return The absolute value of the input number as an unsigned type for integer types. | ||
| */ | ||
| template <typename T> | ||
| constexpr auto | ||
| safe_abs(T x) noexcept |
There was a problem hiding this comment.
@hjmjohnson I understand that an ITK specific function name is helpful, to avoid confusion with std::abs, thanks. Can we possibly still reconsider the name of the ITK function? Why did you choose "safe_abs"? Would a different name also be OK to you? According to the Naming Conventions of our CodingStyleGuide: Global functions and class methods, either static or class members, are named beginning with a capital letter. "itk::Math::Round" follows this naming convention:
ITK/Modules/Core/Common/include/itkMath.h
Line 177 in 3cc3f4a
So then, what do you think of "itk::Math::Abs", instead of "itk::Math::safe_abs"?
(Our style guide also says, that "names are generally spelled out; use of abbreviations is discouraged". So you might also consider "itk::Math::Absolute" 😇 )
There was a problem hiding this comment.
Also the signature does not indicate the return type, so perhaps incorporations "Unsigned" into the name or uabs of you want C name style would be useful?
Include integral and floating-point support
Refactored
itk::Math::absc++ constexpr,supporting both integral and floating-point types. Improved compile-time
evaluation with
constexpr. Enhanced test coverage foritk::Math::abs.PR Checklist