Counting isn't easy. Suppose your boss wants you to work from 8am to 11am, and mop floors 8 to 11. Simple - it's one floor per hour, right?

Nope! There are 4 floors to mop (8, 9, 10 and 11) but only 3 hours to work (8-9, 9-10, and 10-11).

Whoa -- we count floors and hours differently? You bet. And somehow, if the boss said "Mop floors 8 to 11 on April 8th to 11th" everything would be ok.

Today let's unravel this counting mystery.

## Basic Counting

Numbers help us know "how many". Take a numbered list of houses:

There's 52 houses there. Don't count each one: the addresses count for us! The numbers label the houses one by one, just as we'd do. We can just read the last item: "1 to 52" is 52 houses.

**Examples:**

- Employee ID cards from 1 to 3493. There are 3493 employees.
- Days of the month from 1 to 31. There are 31 days in that month.

## What Does Subtraction Mean?

We often see numbers as points on a line:

These points can be houses, floors, or plain integers. Whatever they are, they're labeled so we can count them easily. 1 to 10 means ten items.

But what about a range like 8 to 11? How many items are there?

We'd probably try subtraction: 11 - 8 = 3, right? But here's the key:

**Subtraction is a span between numbers, not a count**

The equation 11 - 8 = 3 means there are 3 "spans" between 8 and 11, but four numbers in that range!

A span is a *distance measure*, like time from 8am and 11am (3 hours) or the distance between 8 and 11 inches (3 inches).

But when counting floors, we aren't asking for the distance between floors 8 to 11 (which is in fact 3 floors or 30 feet, assuming 10 feet per floor). We want a count of how many items the range "8 to 11" includes!

## Working With Spans and Counts

Realizing there are **two types of counting** was a big mental shift. We have two possible choices when "counting" from a to b:

**Distance from a to b:**b - a [regular subtraction]**Number of items from a to b:**b - a + 1 (span touches extra element)

Ok, the formulas work. But why does a count need an extra element?

Well, the shortest span (distance 1) actually touches two numbers:

A span is a line segment with a start and end; a span of distance 1 covers 2 points. As we grow the span, we gobble up more points and are always "one ahead".

Here's another way to think about it. A span of 3 means we start with an item (#8) and count out 3 more (#9, #10, #11). So, a span of N includes 1 original item and N new items, for a total of N + 1 items.

I like seeing new viewpoints; use what works for you.

## The Fencepost Problem

The confusion between spans and counts is commonly called the fencepost problem. Are you counting the posts (points) or the distance between them (fence spans)?

The question goes like this:

You're building a fence 100 feet long, with posts every 10 feet. How many posts do you need?

Here's how to think about it with our new mental model:

Hrm, we want a fence 100 feet long. Ok: that's a span of ten, 10-foot segments. But we want the number ofposts: how many posts do those ten segments touch? Well, a span always touches an extra point, so ten segments means 11 posts.

11 fenceposts it is. But the problem isn't natural for me - I have to think about spans vs points. I'd double-check with a smaller example - a fence that is 10 feet long needs 2 posts, so yes, we need an "extra post".

A few more examples:

**Working Days:**I worked April 8th to April 11th. How many days did I work? Well, that's a span of 3 (11-8), but we "touch" 4 days: April 8, 9, 10, 11. So I worked 4 days.**Hours:**Hours are like spans. Working from 8 to 11 means you are covering the*spans*8-9, 9-10, and 10-11. 8 to 11 means a "time" of 3 hours.**Seconds:**I start a race, and the start time at 12:01:08 (12 hours, 1 minute, 8 seconds). It ends at 12:01:11. How long did it go? 11 - 8 = 3 seconds. (Short race)

Interesting, eh? Some units of time are measured with spans (seconds) and others are items to be counted (days).

The measuring type depends on the context. We see small units of time as "instants" and want the duration between those instants, not the "number" of instants we touched.

We see days as a large fuzzy blob covering a time period (9am-5am) -- and we want to know how many blobs we covered. Saying you worked April 8 to April 9th implies you worked a timespan of 9am-5pm on two days.

The counting type depends on the context - but at least you know *why* we count them differently.

## Final Thoughts

Counting isn't simple. The fencepost problem and other "off by one" boundary errors are notoriously common. But don't just remember a special trick (add 1) - remember there are two ways to measure something:

**Are we measuring a difference (distance, time)?**Then do regular subtraction to get a span.**Are we counting items?**Then subtract and add 1 (b - a + 1) - we want the number of items the span touches.

It's not easy to recognize the difference - we're used to having one way to measure. Try a simple example (a fence 10 feet long) to test if you've got a count or span.

I've run into the fencepost problem many times, but the articles I read just told me how to fix it. No, no, no - *why does it happen*?

It turns out we often use the same approach for two different types of counting. And while the right method may not be obvious, at least we know to try both approaches. Happy math.

## Leave a Reply

53 Comments on "Learning How to Count (Avoiding The Fencepost Problem)"

Your article prints out several question marks and symbols instead of the characters. I’m in the U.S. and it’s displaying that way both in Safari and Firefox.

It looks like a copy-paste from Word.

http://www.atchius.net/betterexplained.png

For your referenceβ¦ that’s how your most recent blog entry looks in Safari on a Mac. RSS feed in Mail.app is the same. In Camino, I get regular ?s instead of the diamond icons.

Your past entries are fine.

Thanks for the fast response guys! I was using a new publishing process and didn’t realize it had all these issues — fixing them up right now.

Ok, the weird characters should be fixed. Thanks again for the notice everyone!

That’s a great explanation. I really like your articles (this is a problem that always frustrates me, as well).

The mathematical terms for that would be inclusive or exclusive, if anyone remembers “sets” from elementary school (or junior high – it’s been awhile). Inclusive means add the 1 (count the number of items), and exclusive means don’t add it (tell me the distance between).

The difference is caused by the adoption of closed intervals in our language. When we say “all integers between 1 and N”, we mean N numbers (i.e., we include the both the boundaries).

It is often more convenient (at least in programming) to work with half-open intervals. Consider a standard way of cycling through N numbers in C-like languages:

for (int i = 0; i

We mention boundaries 0 and N, yet we only mean N numbers, not N+1.

You can read more about this in Dijkstra’s note: Why numbering should start at zero.

The code snippet in my comment above should read:

for (int i = 0; i < N; i++)

…

Sorry about that.

Interesting post on a common issue. Thanks!

[…] Kalid placed an interesting blog post on Learning How to Count (Avoiding The Fencepost Problem …Here’s a brief overviewWhat Does Subtraction Mean? We often see numbers as points on a line: These points can be houses, floors, or plain integers. Whatever they are, they’re labeled so we can count them easily. 1 to 10 means ten items. …. It looks like a copy- paste from Word. Slari β April 28, 2009 @ 6:50 am. http://www.atchius.net/betterexplained.png. For your referenceβ¦ that’s how your most recent blog entry looks in Safari on a Mac. RSS feed in Mail.app is the same. In Camino, I get regular ?s … […]

Thanks Marian, that link was interesting too. It was an excellent

argument, but I didn’t understand the bit around what follows.

‘Latter’ refers to examples (a) and (c). I don’t follow why inclusion

of the upper bound would force the upper bound to be unnatural as the

set shrinks to the empty set. (sorry about the awkward use of the word

bound) Given 2 =< i =< 3 we have {2,3}, given 2 =< i =< 2 we have

only {2}, with 2 =< i < 2 we still have {2} (or nonsense).

Is it because beginning with zero you can't show the empty set with

approach (c), but you can with the (a)?

The passage: "Consider now the subsequences starting at the smallest

natural number: Inclusion of the upper bound would then force the

latter to be unnatural by the time it had shrunk to the empty one."

Hey,

Thanks a lot for your article. I never looked at counting numbers like this before.

A very clear and easy explanation of things.

Looking forward to more posts from you. π

[…] read-more […]

Thanks for the article. I love when you post new stuff.

Please, keep posting π

Great post. I like the idea of thinking in spans and also physical entities. It’s something we all do but maybe have not consciously thought about it.

This reminded me of how the years of the 1800’s are the 19th century (and not the 18th century).

Keep em coming!

@Phil, @ Marian: Thanks for the details! Yes, it’s interesting how certain types of time ranges imply inclusive or exclusive ranges. We tend to “know” which one to use given the context and/or idioms like the for loop (always start at 0, always be less than N).

@Queryman: I think my form may have eaten your comment, I think you may have written “Given 2 = i”

@Scientific Chick: Thanks!

@Ismeet: You’re welcome, appreciate the encouragement π

@Tiago: Thanks!

@CL: Ah, that 19th centry / 1800s issue has been on my mind too, great example. The first “span” of a hundred years (century) is from year 1 to 100, leading to all the confusion we now suffer. Will try to keep them coming :).

OMG! That’s how I’ve envisioned counting things/distances/etc for years! I thought I was the only one! I always said “you have to start at the beginning of 5, but you go to the end of 8” so that makes 4 not 3!

Music is written in such a way. Bar lines are the points for counting measures, so that statements like “four measures back” aren’t ambiguous.

When I began numbering my scores, I noticed that a four measure rest starting at measure 21 encompasses spans 21-22, 22-23, 23-24, and 24-25, but it only numbers 21-24, because the measure between 24 and 25 is numbered 24. That was the epiphany that there are two different counting systems, and that they must coexist.

@Jehan: Nice! It’s really cool figuring out the tricks other people use to handle potentially confusing topics.

@Nobody: Great example with music. I think it makes sense, as music must convey timing accurately and clearly. It’s an interesting solution to just “number the fence pieces” to avoid confusion about what 4 measures back means. Thanks for the example.

this post reminded me of an old puzzle:

If you take one pill every half hour, how many will you take in one hour?

I use this counting on daily hospital rounds. In surgery we keep track of Hospital Day (HD) and Post-Op Day (POD) for our patients. HD is a fencepost count and POD is a measure of the interval from the day of operation.

@andrew: Interesting puzzle — depends on whether you count 1:00, 1:30 and 2:00 as belonging to the same hour π

@Brian: Cool, thanks for the real world example!

Also depends on when you take your first pill:

If you take a pill at 1:00, then in the hour 1:01 to 2:01 you will have taken just two pills.

[…] I just discovered BetterExplained.com, the brainchild of Kalid Azad. It’s great! Kalid writes clear, thorough explanations of all kinds of ideas from simple stuff to complicated stuff. Math, computers, marketing, communication and more. […]

Finally, a clear way explanation of why 1 Jan 2000 was not the beginning of the new century (and millennium) – 1 Jan 2001 was!