Hi,
I've been contemplating a possible new standard function std::nabs,
which would return a negated absolute value of its argument:
assert(std::nabs(1) == -1);
assert(std::nabs(-1) == -1);
assert(std::nabs(0) == 0);
The idea behind this addition is that, unlike std::abs, this function
will never cause a signed integer overflow. This is because in two's
complement representation, which is mandatory since C++20, any positive
integer can be negated without overflow, but not vise versa.
I wonder if this idea has come up before.
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals