Unprompted← All traps

Thinking trap · Math

Counting fences, not gaps

Also called off-by-one error, fencepost error.

When you count a range, it is easy to mix up the items and the gaps between them. The answer is then off by one.

An everyday example

Say you must read pages 15 to 30 of a report, both included. Many people answer 15 pages, because 30 minus 15 is 15.

But that counts the gaps between the pages. To count the pages themselves, add one: 30 - 15 + 1 = 16 pages.

Hotel stays work the other way: you count the gaps, not the days. If you arrive on Monday and leave on Friday, you stay 4 nights, not 5.

Programmers call this the fencepost error. A straight fence always has one more post than it has gaps between posts.

How an AI answer falls for it

You ask an AI how many rows a spreadsheet formula covers. It answers with confidence:

“The range A2 to A101 covers 99 rows, because 101 minus 2 is 99.”

The AI subtracted the end points. That counts the gaps between the rows, not the rows.

Rows 2 and 101 are both part of the range, so it covers 101 - 2 + 1 = 100 rows. If you then divide a total by 99 instead of 100, the average is wrong.

Spreadsheet ranges and page ranges include both ends. Date ranges sometimes do and sometimes do not, so check each time.

How to catch it

  • Ask whether both ends of the range are included.
  • Test the rule on a tiny case. Pages 1 to 2 are 2 pages, so the rule must give 2.
  • Decide what you count: the posts or the gaps, the days or the nights.
  • When a result looks one too small or one too large, check the ends first.

Practice

Every daily challenge hides one trap, or none. Play today's challenge

Related traps

Further reading