Strange Delphi integer multiplication behavior

delphi, math

Solution

Alright, I'll make this an answer.

The error message should be pretty clear. You have an integer overflow here:

130245932 * 1103516849

because `130245932 * 1103516849 = 143728580475708268` which is too large to fit into a 32-bit integer.

Problem

I'm working on some ancient Delphi code and I've come across something which I don't quite understand. ``` [bla is set to 130245932] outresult := ((bla * 1103516849) + 12359); [outresult is equal to -413953101] ``` How does multiplying two positive numbers result in a negative number? And why is it that when I take the bla variable out of the equation and just use the integer directly (like this) ``` outresult := ((130245932 * 1103516849) + 12359); ``` I receive an error before the app even compiles ``` [DCC Error] Unit1.pas(60): E2099 Overflow in conversion or arithmetic operation ``` Some genius would be appreciated. Thanks.

Original source