fencepost error

fencepost error

(1)(Rarely "lamp-post error") A problem with the discreteequivalent of a boundary condition, often exhibited inprograms by iterative loops. From the following problem: "Ifyou build a fence 100 feet long with posts 10 feet apart, howmany posts do you need?" (Either 9 or 11 is a better answerthan the obvious 10).

For example, suppose you have a long list or array of items,and want to process items m through n; how many items arethere? The obvious answer is n - m, but that is off by one;the right answer is n - m + 1. The "obvious" formula exhibitsa fencepost error.

See also zeroth and note that not all off-by-one errorsare fencepost errors. The game of Musical Chairs involves acatastrophic off-by-one error where N people try to sit in N -1 chairs, but it's not a fencepost error. Fencepost errorscome from counting things rather than the spaces between them,or vice versa, or by neglecting to consider whether one shouldcount one or both ends of a row.

fencepost error

(2)(Rare) An error induced by unexpected regularities in inputvalues, which can (for instance) completely thwart atheoretically efficient binary tree or hash codingimplementation. The error here involves the differencebetween expected and worst case behaviours of an algorithm.