Content deleted Content added
→Methods to mitigate integer overflow problems: Static analysis tools, formal verification and design by contract relevant to "avoidance" method |
|||
Line 66:
There are several methods of handling overflow:
# Avoidance: by carefully ordering operations, checking operands in advance and selecting the correct data type, it is possible to ensure that the result will never be larger than can be stored. [[Static program analysis|Static analysis]] tools, [[formal verification]] and [[design by contract]] techniques can be used to more confidently and robustly ensure that an overflow cannot accidentally result.
# Handling: If it is anticipated that overflow may occur and when it happens detected and other processing done. Example: it is possible to add two numbers each two bytes wide using just a byte addition in steps: first add the low bytes then add the high bytes, but if it is necessary to carry out of the low bytes this is arithmetic overflow of the byte addition and it necessary to detect and increment the sum of the high bytes. [[Central processing unit|CPUs]] generally have a way of detecting this to support addition of numbers larger than their register size, typically using a status bit.
# Propagation: if a value is too large to be stored it can be assigned a special value indicating that overflow has occurred and then have all successive operation return this flag value. This is useful so that the problem can be checked for once at the end of a long calculation rather than after each step. This is often supported in Floating Point Hardware called [[floating point unit|FPUs]].
|