Hacker News new | ask | show | jobs
by jffhn 447 days ago
>Though abs() returning negative numbers is hilarious.

Math.abs(Integer.MIN_VALUE) in Java very seriously returns -2147483648, as there is no int for 2147483648.

4 comments

You inspired me to check what .NET does in that situation.

It throws an OverflowException: ("Negating the minimum value of a twos complement number is invalid.")

Oh no, Pytorch does the same thing:

a = torch.tensor(-2*31, dtype=torch.int32) assert a == a.abs()

numpy as well. and tensorflow
Unchecked integer overflow strikes again.
Rust does the same in release, although it panics in debug.