Fencepost Error
A fencepost error (occasionally called a "telegraph pole" or "lamp-post" error) is a specific type of off-by-one error. The following problem illustrates the error:
If you build a straight fence 100 meters long with posts 10 meters apart, how many posts do you need?The intuitive answer 10 is wrong. The fence has 10 sections, but 11 posts.
The reverse error occurs when the number of posts is known and the number of sections is assumed to be the same. The actual number of sections is one fewer than the number of posts.
More generally, the problem can be stated as follows:
If you have n telegraph poles, how many gaps are there between them?The correct answer may be n − 1, n, or n + 1, depending on the conditions. The precise problem definition must be carefully considered, as the setup for one scenario may give the wrong answer for other scenarios.
Fencepost errors can also occur in units other than length. For example, the time pyramid, consisting of 120 blocks placed at 10 year intervals between blocks, is scheduled to take 1190 (not 1200) years to build, from the installation of the first block to the last block.
"Fencepost error" can, in rare occasions, refer to an error induced by unexpected regularities in input values, which can (for instance) completely thwart a theoretically efficient binary tree or hash function implementation. This error involves the difference between expected and worst case behaviours of an algorithm.
In larger numbers being off-by-one is often not a major issue. In smaller numbers, however, and specific cases where accuracy is paramount committing an off-by-one error can be disastrous. Sometimes such an issue will also be repeated and, therefore, worsened, by someone passing on an incorrect calculation if the following person makes the same kind of mistake again (of course, the error might also be reverted).
An example of this error can occur in the computational language Matlab with the linspace
function, whose parameters are (lower value, upper value, number of values) and not (lower value, upper value, number of increments). A programmer who misunderstands the third parameter to be the number of increments might hope that linspace(0,10,5)
would achieve a sequence but instead would get
.
Read more about this topic: Off-by-one Error
Famous quotes containing the word error:
“When we do not know the truth of a thing, it is of advantage that there should exist a common error which determines the mind of man.... For the chief malady of man is restless curiosity about things which he cannot understand; and it is not so bad for him to be in error as to be curious to no purpose.”
—Blaise Pascal (16231662)