Skip to content

ENH: Add constexpr overloads for itk::Math::abs#5797

Merged
hjmjohnson merged 1 commit intomainfrom
constexpr-math-operations
Feb 14, 2026
Merged

ENH: Add constexpr overloads for itk::Math::abs#5797
hjmjohnson merged 1 commit intomainfrom
constexpr-math-operations

Conversation

@hjmjohnson
Copy link
Copy Markdown
Member

@hjmjohnson hjmjohnson commented Feb 12, 2026

Include integral and floating-point support

Refactored itk::Math::abs c++ constexpr,
supporting both integral and floating-point types. Improved compile-time
evaluation with constexpr. Enhanced test coverage for itk::Math::abs.

PR Checklist

  • No API changes were made (or the changes have been approved)
  • No major design changes were made (or the changes have been approved)
  • Added test (or behavior not changed)

@github-actions github-actions bot added type:Enhancement Improvement of existing methods or implementation type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module labels Feb 12, 2026
@hjmjohnson hjmjohnson marked this pull request as draft February 12, 2026 20:47
@hjmjohnson hjmjohnson force-pushed the constexpr-math-operations branch from 4fcc6d0 to 0d1a52b Compare February 12, 2026 21:17
Copy link
Copy Markdown
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good on a glance.

@hjmjohnson hjmjohnson force-pushed the constexpr-math-operations branch 2 times, most recently from eb506bd to 60ab154 Compare February 13, 2026 00:14
@hjmjohnson hjmjohnson marked this pull request as ready for review February 13, 2026 12:05
@hjmjohnson hjmjohnson force-pushed the constexpr-math-operations branch 2 times, most recently from a19ef23 to d71da7d Compare February 13, 2026 12:53
@hjmjohnson hjmjohnson force-pushed the constexpr-math-operations branch from d71da7d to 236f95f Compare February 13, 2026 13:09
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.
@hjmjohnson hjmjohnson force-pushed the constexpr-math-operations branch from 83c0d97 to 3cc3f4a Compare February 13, 2026 18:50
Copy link
Copy Markdown
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@hjmjohnson hjmjohnson merged commit 1cf32ee into main Feb 14, 2026
18 checks passed
@hjmjohnson hjmjohnson deleted the constexpr-math-operations branch February 14, 2026 02:20
Comment on lines +828 to +843
/**
* @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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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:

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" 😇 )

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module type:Enhancement Improvement of existing methods or implementation type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants