As the absolute value of -2147483648 is outside of the range of positive values of 32-bit signed integers, the value overflows back into the negative range.
There are a few potential solutions to this problem, none entirely preferable.
add a doc comment to Abs stating that this is expected behaviour and let users handle the edge case
panic in Abs if the output value is negative
change the signature of Abs to func Abs(int) (int, bool) where the boolean return value states whether the operation was successful
return a value other than -2147483648 (e.g. 0), however this may be the most confusion of all potential solutions, so not really relevant as a solution.
I am not expecting a fix, but would welcome a discussion on this topic, and perhaps we can update the documentation of Abs based on the outcome discussion.
Environment information:
Bug description The
core/math/sint.Abs
function returns a negative value (-2147483648
) when given the input-2147483648
.From core/math/sint/sint.go:
Reproduction steps Steps to reproduce the behavior:
Discussion:
As the absolute value of
-2147483648
is outside of the range of positive values of 32-bit signed integers, the value overflows back into the negative range.There are a few potential solutions to this problem, none entirely preferable.
Abs
stating that this is expected behaviour and let users handle the edge caseAbs
if the output value is negativeAbs
tofunc Abs(int) (int, bool)
where the boolean return value states whether the operation was successful-2147483648
(e.g.0
), however this may be the most confusion of all potential solutions, so not really relevant as a solution.I am not expecting a fix, but would welcome a discussion on this topic, and perhaps we can update the documentation of
Abs
based on the outcome discussion.Cheers, Robin