View Single Post
rcgldr
#5
Feb22-12, 11:54 AM
HW Helper
P: 7,132
Most processors have an overflow bit for integer math, but most compilers don't provide access to it (short of using inline assembly or assembly callable functions). Checking for overflow on addition can be done by checking to see if both addends have the same sign, and if so, if the sum also has the same sign (else overflow has occurred). Subtraction can be handled by negating the number to subtract and using the addition check. Overflow from multiplication may cause an exception depending on the cpu. If not, multiplication can be checked by dividing the product by one of the multiplicands to see if you get the other multiplicand. Overflow from division may cause an exception depending on the cpu. If not, you need to check the quotient by multiplying the quotient by the divisor to see if it matches the dividend.