# How to best handle itk::Math::Absolute

**URL:** https://discourse.itk.org/t/how-to-best-handle-itk-absolute/7724
**Category:** Uncategorized
**Created:** [March 5, 2026, 10:09am UTC](https://discourse.itk.org/t/how-to-best-handle-itk-absolute/7724 "2026-03-05T10:09:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [March 5, 2026, 10:09am UTC](https://discourse.itk.org/t/how-to-best-handle-itk-absolute/7724/1 "2026-03-05T10:09:41Z")

</div>

The recent [PR 5802](https://github.com/InsightSoftwareConsortium/ITK/pull/5802) has triggered a [bunch of new warnings](https://my.cdash.org/builds/3382718/errors) in RTK because “`std::abs()` returns the absolute value in the same type as the input, `itk::Math::Absolute()` returns the absolute value in the unsigned equivalent of the input type”. Looking closely at the warning

```auto
/home/srit/src/rtk/dashboard_tests/RTK/include/rtkJosephForwardProjectionImageFilter.hxx:178:1: required from here
[CTest: warning matched] /home/srit/src/rtk/dashboard_tests/RTK/include/rtkJosephForwardProjectionImageFilter.hxx:383:28: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
  383 | for (int i{ 0 }; i < itk::Math::Absolute(fs - ns) - 1; ++i)
      | ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

I think the behavior of the new `itk::Absolute` is undesirable in these cases and I am concerned this is not obvious to the user as the legacy `itk::Math::abs` calls `itk::Math::Absolute`. In this code, if `fs` equals `ns`, then you’d prefer `itk::Absolute` to return an `int` and not an `unsigned int`. So my questions are:

- If you agree, should we keep the new `itk::Absolute` as is?
- If yes, what is in your opinion the best option here? I can think of a `static_cast` of the value returned by `Absolute` or making `i` `unsigned` and moving the `-1` to the left-hand side, but I find both much less readable than the original version… Any other suggestion?

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [March 5, 2026, 1:43pm UTC](https://discourse.itk.org/t/how-to-best-handle-itk-absolute/7724/2 "2026-03-05T13:43:24Z")

</div>

Thanks for asking, @simon.rit

Another option in this specific case might be to use `std::abs`. 🤷

I think it’s a design choice for `itk::Math::Absolute(x)` to always return an _unsigned_ integer type, for an integer argument. Hans (@hjmjohnson ), what do you think?

* * *

FYI, In the past `itk::Math::abs(int x)` already returned an `unsigned int`, going 10 years back in time:

[ITK/Modules/Core/Common/include/itkMath.h at 70058bb6476cacc3350aeb5d08c60796b2de4483 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/blob/70058bb6476cacc3350aeb5d08c60796b2de4483/Modules/Core/Common/include/itkMath.h#L794C17-L794C18)

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [March 5, 2026, 2:13pm UTC](https://discourse.itk.org/t/how-to-best-handle-itk-absolute/7724/3 "2026-03-05T14:13:02Z")

</div>

Good (and obvious) solution for `std::abs` which would have my preference now, thanks. I might as well drop the use of `itk::Math::Absolute` in my code.  
I guess I’m concerned by the transparent change in behavior compared to one month ago if the signedness compilation warning is disabled (even if it was already like this 10 years ago).
