2 ms·
Being brutal heterodox: STOP WRITING SIGNED ARITHMETIC. Your code assumes that negating a negative value is positive. Your division check forgot about INT_MIN
by millstone 7y ago
Being brutal heterodox: STOP WRITING SIGNED ARITHMETIC.
Your code assumes that negating a negative value is positive. Your division check forgot about INT_MIN / -1. Your signed integer average is wrong. You confused bitshift with division. etc. etc. etc.
Unsigned arithmetic is tractable and should be treated with caution. Signed arithmetic is terrifying and should be treated with the same PPE as raw pointers or `volatile`.
This applies if arithmetic maps to CPU instructions, but not to Python or Haskell or etc. If you have automatic bignums, signed arithmetic is of course better.