News for hackers. Stuff that matters.
Re:Figuring out round, floor and ceil with integer division by fwsgonzo on Saturday November 26, 2022 @02:35AM (#33750698)
A fun fact about integer division is that on x86 dividing by zero is CPU exception 0. Except, it's not just for dividing by zero, it's whenever a division is impossible to perform.

So, for example what happens if you divide -1 by -INT_MIN? As you probably know, Abs(INT_MIN) is larger than INT_MAX, and so it is not possible to perform -1 / INT_MIN, as well as -1LL / INT64_MIN.

Re:Figuring out round, floor and ceil with integer division by im3w1l on Saturday November 26, 2022 @08:29AM (#33752463)
This had me scratching my head for a while. Shouldn't -1 / INT_MIN just be 0? Indeed it should. INT_MIN / -1 is what causes the issue. I also tested doing that the latter just to be sure, and on my system, performing that division led to a result of INT_MIN rather than an exception.

Edit: Actually further tests with INT_MIN / - 1 did lead to a crash. Maybe the first result was due to constant folding or something.

Re:Figuring out round, floor and ceil with integer division by Sharlin on Saturday November 26, 2022 @10:07AM (#33753236)
Like all signed overflow, it's undefined behavior in C and C++, so what exactly happens is up to the gods of optimizing compilers.