Live data from Hacker News

Always use [closed, open) intervals

fhur.me

151–160 of 169 posts

Re: Always use [closed, open) intervals

#151
post #126

Just for completeness I'll mention that there is another style: start, count This seems to be popular in .NET ecosystem.

That’s for creating an enumerable of discrete integers, not for defining an interval.

I'd say bound, range is an acceptable way of defining an interval, it can save space for multidimensional intervals, though at the cost of increased computation.

Re: Always use [closed, open) intervals

#152
post #133

Earlier quoted context omitted.

It's a matter of (natural language) terminology. It's the difference between asking "which year of your life are you currently in" as opposed to "how many full years have elapsed since you were born?"

Agreed. My previous reply just pointed out that 1 is not "how many full years have elapsed since you were born?". The answer to this question is still 0.

[deleted]

Re: Always use [closed, open) intervals

#153
post #103

What's with the funny connected "st" in the article? I've never seen that before.

I don't see it, perhaps a different font loaded for me, but maybe you mean a ligature? https://en.wikipedia.org/wiki/Ligature_(writing)

Yes, that's it. I've not seen a ligature used that way in text before. English has words that can be spelled with ligatures to this day, but not for the sound "st"

https://en.wikipedia.org/wiki/List_of_English_words_that_may...

Re: Always use [closed, open) intervals

#154
post #153

Earlier quoted context omitted.

I don't see it, perhaps a different font loaded for me, but maybe you mean a ligature? https://en.wikipedia.org/wiki/Ligature_(writing)

Yes, that's it. I've not seen a ligature used that way in text before. English has words that can be spelled with ligatures to this day, but not for the sound "st" https://en.wikipedia.org/wiki/List_of_English_words_that_may...

Its just a fancy typographic ligature, not a spelling “ligature” (which, in turn, is more of a letter with an optional decomposition as an anti-ligature) like ash (æ) or ethel (œ).

Re: Always use [closed, open) intervals

#155
post #29

Half-open intervals are why I try as much as possible to stay away from languages that use 1-based indexing (Lua, Julia, Matlab, R, ...) - 1-based indexing lends itself to closed intervals because an array of N elements has [1,N] as its index range, whereas 0-based indexing lends itself to half-open intervals because an array of N elements has [0,N) as its index range. ------- However, I know of one case where closed…

I don’t think your mapping example works. You’re manipulating a range in a way that is inherently lossy and incorrect, and chosen a method that is trivially not reversible—try to go the other direction, which you will want to do in projecting from tile space to the original coordinate space, and your closed range introduces error. So using a closed range only helped in one direction, and gave you a misleading sense that your range was still equivalent. Consider also what happens with the next tile along: you have overlap between adjacent tiles unless your tiles are aligned to a multiple of two; and this demonstrates why it’s such a risky proposition, because you’re baking in unstated assumptions which will have a habit of coming back to bite you later when you want to relax them.

My observation with this kind of data is that people work in a theoretically-continuous coordinate space (though it’s probably represented in floating-point numbers so that it’s strictly discrete, just at very high precision), and, when forced to deal with coarser resolutions like tile pixel grids, immediately project back so they can work in the preferred coordinate space. Even if your coordinate spaces are all square, you will have margins of error however you do this, due to quantisation error—and does the pixel at (0, 0) show the data from (−0.5, −0.5) to (0.5, 0.5), or from (0, 0) to (1, 1)?

Re: Always use [closed, open) intervals

#157

Dumb question but why is ‘[‘ called ‘closed’ and ‘)’ called ‘open’? I somehow would have thought the reverse.

An open interval doesn’t include its boundaries. It’s like a room missing one or more walls, and which hence is “open” towards the respective side(s).

Imagine a series of adjacent boxes with shared walls, that you cut up. Depending on how you cut them up, each wall will stay with one of the boxes, which thus will remain “closed” on that side, while the other box who shared the same wall is now open on the side where you separated the boxes.

   +———+———+
   |   |   |
   +———+———+

       |
       V

  +———+  ———+
  |   |     |
  +———+  ———+

 closed  open
 
  [0,1] (1,2]

Re: Always use [closed, open) intervals

#158

I prefer [a, b) intervals because they encode two pieces of important information directly: 1. The first element (a) 2. The length (b-a) Which are what we most often need.

If, however, a and b are dates, these operations are nonsensical, as is using anything else except closed intervals: for date range comparisons it is much more understandable to use start-of-day on a and end-of-day on b.

This is only because many programming languages don’t have proper calendar-date data types. If they had, the values would just represent the dates, without an implied time-of-day, and adding/subtracting integers would simply add/subtract the respective number of days.

Re: Always use [closed, open) intervals

#160
post #29

Half-open intervals are why I try as much as possible to stay away from languages that use 1-based indexing (Lua, Julia, Matlab, R, ...) - 1-based indexing lends itself to closed intervals because an array of N elements has [1,N] as its index range, whereas 0-based indexing lends itself to half-open intervals because an array of N elements has [0,N) as its index range. ------- However, I know of one case where closed…

The example using closed intervals makes sense. I see why you mention 1-based indexing, but it’s mostly a different beast. I’ll defend 1-based indexing here because, having used Lua a lot, the indexing issue just goes away. People like what they’re used to. Most coders are used to 0-based, but we all start life 1-indexed. We begin counts with 1: chapter 1, the 1st floor of a building (in the US), 1 AD, the 1st of Dec…

> 1 AD

And this makes things really confusing. 1999 is 20th century even if the year starts with "19". 2000 is still 20th century, 21th century starts in 2001. "BC" dates feel like negative numbers but they are not. Using closed intervals [10 BC, 10 AD] spans 20 years, but [10 AD, 20 AD] spans 21 years.

Post reply on HN