Overflow occurs when the result of an arithmetic operation does not match the data type of the operation. You may have an overflow with an unsigned byte if you add 255 + 1, because the result (256) does not fit into 8 bit bytes.
You may have a floating point overflow if the result of the floating point operation is too large to represent an exponential type of floating point data or mantissa.
You can also have an underflow with floating point types when the result of a floating point operation is too small to represent a floating point data type. For example, if a floating point data type can handle metrics in the range of -100 to +100, and you assign a value with a -80 metric, the result will have a metric of about -160, which will not match the given floating point data type.
You need to worry about overflows and threads in your code, because it can be a silent killer: your code produces incorrect results, but may not signal an error.
Regardless of whether you can safely ignore overflows, a lot depends on the nature of your program. Images of screen pixels from 3D data have a much larger tolerance for numerical errors than, say, financial calculations.
Overflow checking is often disabled in the default compiler settings. What for? Since additional code to check for overflow after each operation takes time and space, which can degrade the performance of your code at run time.
Do yourself a favor, and at least develop and test your code with overflow checking enabled.
dthorpe Apr 14 '10 at 23:52 2010-04-14 23:52
source share