Given three integers a, b and c (all of them are 64 bit signed integers,
to
), check if a + b > c.
Since a + b can overflow (or underflow) easily (when the sum is too big or too small to hold using a 64 bit signed integer), we should avoid this.
bool chk(long long a, long long b, long long c) {
if(a >= 0 && b >= 0) {
if(a > LLONG_MAX - b) return true;
}
if(a <= 0 && b <= 0) {
if(a < LLONG_MIN - b) return false;
}
return a + b > c;
}
–EOF (The Ultimate Computing & Technology Blog) —
198 wordsLast Post: Why Absolute Value of INT_MAX and INT_MIN is different?
Next Post: Coding Exercise - 1. Life, the Universe, and Everything - Learning LUA programming language