by mdellabitta · 8y ago · 0 comments
Now I'm plagued by off-by-one errors in my mental math, particularly when it comes to intervals. If you ask me how many days there are between now and Christmas, I will be uncertain about the answer unless I individually count them all off on my fingers.
Well, between day 1 and day 3 there is 1 day. By finite induction between day n>3 and day 1, there are n-2 days, and analogously between days n>k>1 there are n-k-1 days.
"well, if it spanned to row 19, there would be two elements, so N elements end at row 17+N, so 33-17 is 16, so there are 16 elements."
That's painstaking and I should fix it, lol.
Subtract the start and stop. If the start and stop are both excluded, subtract 1. If one of them is included, make no change. If both are included, add 1.
Excluded start and stops happen when you have external boundaries. Like, your last day on the old project was the 10th, and your first day on the new one is the 15th. You had no project for 15-10-1=4 days.
One included happens when you're measuring between numbers. If you start working at 2 and finish at 8, you worked 8-2=6 hours.
Double inclusion counts when you have internal boundaries, or are counting how many things are "touched". If you work on something from the 11th to the 15th, you worked on it 15-11+1=5 days. (Also, your example.)
Stop position - start position + (number of included positions - 2) = total
| H | e | l | l | o |
Index 0 1 2 3 4 5
Length |<--------5-------->|
This makes it easy to visualize that 1-based indexing has indices centered on the memory cell, so calculating a span requires reducing the subtractor by 1.I was just taught counting using it, it was a quick bonus math class one day.
It is super useful for counting things off without losing place. Even being able to go up to 10 on just one hand is useful.