Skip to content

Commit 9764640

Browse files
authored
Merge pull request InsightSoftwareConsortium#5931 from N-Dekker/DOC-improve-Math-Absolute-on-INT_MIN-and-bool
DOC: Improve `Math::Absolute(x)` doc on `INT_MIN` and `bool` support
2 parents a694cf9 + 9fdbc9b commit 9764640

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Modules/Core/Common/include/itkMath.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -861,16 +861,17 @@ using vnl_math::squared_magnitude;
861861
* @brief Returns the absolute value of a number.
862862
*
863863
* This function provides a c++17 implementation of the vnl_math::abs absolute value functionality.
864-
* itk::Absolute() preserves backward compatibility with vnl_math::abs that was originally used in ITK.
865-
*
866-
* Key differences between itk::Absolute() and std::abs()
867-
* - Where std::abs() returns the absolute value in the same type as the
868-
* input, itk::Absolute() returns the absolute value in the unsigned
869-
* equivalent of the input type.
870-
* - std::abs() is undefined when converting the minimum storage value of unsigned
871-
* integers to positive values. itk::Absolute() typecasts to a larger storage type
872-
* before taking the absolute value to prevent overflow failures. This is used in
873-
* all cases except 'signed long long' where there is no larger storage type.
864+
* Math::Absolute() preserves backward compatibility with vnl_math::abs that was originally used in ITK.
865+
*
866+
* Key differences between Math::Absolute() and std::abs():
867+
* - Where `std::abs(x)` returns the absolute value in the same type as the input, `Math::Absolute(x)` returns the
868+
* absolute value in the unsigned equivalent of the input type, when the input is an integer.
869+
* - `std::abs(x)` has undefined or compiler specific behavior when its argument is the minimum value of `int`, `long`,
870+
* or `long long`. On the other hand, Math::Absolute _does_ support the entire range of possible integer values. So for
871+
* example `Math::Absolute(INT_MIN)` properly returns 2147483648 as unsigned integer (assuming that `INT_MIN` has the
872+
* value -2147483648).
873+
* - Unlike std::abs, Math::Absolute _does_ support `bool`. For a `bool` argument, `Math::Absolute(x)` simply returns
874+
* the value of the argument.
874875
*
875876
* @tparam T The type of the input number.
876877
* @param x The input number.

0 commit comments

Comments
 (0)