Theiling Online    Sitemap    Conlang Mailing List HQ   

Re: Fence post error (was: CONLANG Digest -)

From:John Cowan <jcowan@...>
Date:Wednesday, January 5, 2000, 15:03
FFlores wrote:

> What's the fence post error?
From the Jargon File: fencepost error n. A problem with the discrete equivalent of a boundary condition, often exhibited in programs by iterative loops. From the following problem: "If you build a fence 100 feet long with posts 10 feet apart, how many posts do you need?" (Either 9 or 11 is a better answer than 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 is that? The obvious answer is n - m, but that is off by one; the right answer is n - m + 1. A program that used the `obvious' formula would have a fencepost error in it. See also *zeroth* and *off-by-one error*, and note that not all off-by-one errors are fencepost errors. The game of Musical Chairs involves a catastrophic off-by-one error where N people try to sit in N - 1 chairs, but it's not a fencepost error. Fencepost errors come from counting things rather than the spaces between them, or vice versa, or by neglecting to consider whether one should count one or both ends of a row. -- Schlingt dreifach einen Kreis vom dies! || John Cowan <jcowan@...> Schliesst euer Aug vor heiliger Schau, || http://www.reutershealth.com Denn er genoss vom Honig-Tau, || http://www.ccil.org/~cowan Und trank die Milch vom Paradies. -- Coleridge (tr. Politzer)