I have a simple c++ app that performs the following calculations
long long calcOne = 3 * 100000000; // 3e8, essentially
long long calcTwo = 3 * 1000000000; // 3e9, essentially
long long calcThree = 3 * 10000000000; // 3e10, essentially
calcOne = 300000000
calcTwo = -1294967296
calcThree = 30000000000
Integer constants are, by default
That can fit into an
int. So, this constant gets parsed as an
int. But multiplying it by 3 overflows int.
This is too big to an int, so this constant is a
long long, so the resulting multiplication does not overflow.
Solution: explicitly use
long long constants:
long long calcOne = 3 * 100000000LL; // 3e8, essentially long long calcTwo = 3 * 1000000000LL; // 3e9, essentially long long calcThree = 3 * 10000000000LL; // 3e10, essentially