Live data from Hacker News

Always use [closed, open) intervals

fhur.me

81–90 of 169 posts

Re: Always use [closed, open) intervals

#81

FWIW, Postgres converts all ranges (=interval in this context) for discrete data types to half open intervals even when a closed one was requested. So the daterange '[2022-01-01, 2022-01-07]' will result in [2022-01-01,2022-01-08) and the integer range '[1,7]' will result in '[1,8)' So it seems the Postgres devs agree with the author. Edit: typo fixed for integer range

Small comment:

I think you have your integer example backwards, ie [1,8) = [1,7].

Re: Always use [closed, open) intervals

#82

> Never, ever, ever use [closed, closed] intervals I’m not really a fan of “never,” or “always” rules, when it comes to programming. I’ve found it’s usually better to have a “make sure to justify deviations from” heuristics. I usually use [closed..open) ranges (as they are called in Swift), but sometimes, an inclusive range is a lot more appropriate, for expressing an operation (for example, I may express a range as…

I've moved away from this. Advice is supposed to convey information simply and efficiently, and "asterisks" for every edge case just muddle it. Now my pet peeve has moved to folk wheedling about 100% accuracy in advice. ;-) e.g. Great advice: "Never run a red light" vs Technically-correct-but-will-never-be-read advice: "Don't run a red light, unless you're on a bicycle and you know that the intersesction you're at is…

That's why I have “make sure to justify deviations from” in there.

It's easy enough to make that policy, but you run the risk of making the development process too unwieldy to implement (See: Taligent's Style Guide).

I would hope that most style guides have caveats for deviation (disclaimer: I haven't read a style guide in eons, so I don't know what they generally say, these days).

I'd say that "We should do it this way, but, if you think your way is better, convince me." is a good approach.

Really, I see a lot of problems, caused by corporations' obsession with hiring lots of bad programmers, and trying to force them to be good programmers, by setting up strict boundaries, as opposed to just hiring a few good programmers, in the first place (which, admittedly, has its own issues). This is nothing new. I have seen this since the 1980s.

The really cool thing about software, is its flexibility. I feel that the enormous toolbox at my disposal, along with my experience in using said tools, makes it possible for me to develop some pretty good stuff. If someone tried to force me to do a "lowest common denominator" approach, I don't think the results would be good.

Re: Always use [closed, open) intervals

#83
post #32
post #25

I think this has to do with the nature of the metric underneath. Closed-open intervals are the way to go for integers. However they don’t seen to be a good fit for sampling from continuuous space

When you are working on a continuous space, what exactly is the difference in a program whether you use a closed-closed or half-open interval? The only case that comes to my mind where it makes a difference at all is when you take discrete values from the interval and happen to hit exactly the end of the interval. But then the difference comes from the "discrete" part again. As long as you work on the continuous spac…

It has to do with how cpus do floating point division.

Here is an article that discusses this, along with workarounds:

https://dl.acm.org/doi/10.1145/3503512

From the abstract: "Drawing a floating-point number uniformly at random from an interval [a, b) is usually performed by a location-scale transformation of some floating-point number drawn uniformly from [0, 1). Due to the weak properties of floating-point arithmetic, such a transformation cannot ensure respect of the bounds, uniformity or spatial equidistributivity."

Re: Always use [closed, open) intervals

#85

Earlier quoted context omitted.

I've moved away from this. Advice is supposed to convey information simply and efficiently, and "asterisks" for every edge case just muddle it. Now my pet peeve has moved to folk wheedling about 100% accuracy in advice. ;-) e.g. Great advice: "Never run a red light" vs Technically-correct-but-will-never-be-read advice: "Don't run a red light, unless you're on a bicycle and you know that the intersesction you're at is…

There is still a difference between leaving out special cases to give more succinct advice and going out of your way to repeat the words "never"/"ever" three times to imply the absolute absense of any exceptions.

Yeah. I'm on board for 'never', but by the second 'ever' you lose me.

Re: Always use [closed, open) intervals

#86

FWIW, Postgres converts all ranges (=interval in this context) for discrete data types to half open intervals even when a closed one was requested. So the daterange '[2022-01-01, 2022-01-07]' will result in [2022-01-01,2022-01-08) and the integer range '[1,7]' will result in '[1,8)' So it seems the Postgres devs agree with the author. Edit: typo fixed for integer range

Small comment: I think you have your integer example backwards, ie [1,8) = [1,7].

Yes, of course ;) Copy & Paste error.

Re: Always use [closed, open) intervals

#87

Ehh, this post misses the important tidbid. You should keep your code consistent across your organization, so that a large number of programmers knows how your code works. You should have a "default writing style", and the "default writing style" should be used unless you have very, very, very good reasons to avoid it. (And an errant +1 or -1 here and there isn't a good enough reason to switch). There are four styles…

Dijkstra is a smooth talker and sometimes he's able to convince people that an argument is settled even when it is not... Here, he conveniently ignores examples that are better for [closed,closed], such as iterating backwards over the range. That is more annoying for [closed,open) because it turns into the abhorent (open,closed].

An argument I prefer is this: [closed,closed] is better for representing an index into the range while 0-based with [closed,open) is better for offsets. They are not the same, as C would have you believe, and there are situations where behaves nicer than the other.

Re: Always use [closed, open) intervals

#88

I am surprised I can't see this here. Wasn't anyone else taught [closed; closed] and ]open; open[ notation in school?

Nope, I learned [closed, open) in school and .. vs ... in ruby. Python range() is [closed, open) so that's what I use everywhere now.

Re: Always use [closed, open) intervals

#89
post #59
post #45

Python uses "closed open" intervals with `range(0, n)`, the reverse is then `range(n - 1, -1, -1)`, which is then highly unintuitive. This in connection with 0-based array indexing makes certain algorithms then very cumbersome. For example Knuth-Shuffle . In Python this is: from random import randrange x = [10, 20, 30, 40, 50 ] for i in range(len(x) - 1, 0, -1): r = randrange(i + 1) x[i], x[r] = x[r], x[i] print(x) W…

There's one pretty common exception that's "closed closed". random.randint(a, b) Return a random integer N such that a https://docs.python.org/3/library/random.html#random.randint But in reality nowadays you almost always want to use the newer, simpler and more secure "secrets" module.

TIL about the "secrets" module. I have typically used os.urandom() when I needed secure random.

Re: Always use [closed, open) intervals

#90

Ehh, this post misses the important tidbid. You should keep your code consistent across your organization, so that a large number of programmers knows how your code works. You should have a "default writing style", and the "default writing style" should be used unless you have very, very, very good reasons to avoid it. (And an errant +1 or -1 here and there isn't a good enough reason to switch). There are four styles…

Once you go beyond integers, using [closed, open) or (open, closed] becomes more important, because otherwise you cannot represent adjacent intervals.

The empty interval problem applies to integers too: [15,15) and (15,15] are empty, [x,y] is not for any x and y.
Post reply on HN