Representing intervals or ranges? [closed]

╄→гoц情女王★ 提交于 2019-12-12 12:10:32

问题


In general, whenever you're representing a range of any kind, you have several choices for what kinds of values to choose for the beginning and ending of your range. For example, if you want to have a range containing the integers 1, 2, 3, 4, 5 you could choose these possible values:

  • begin = 0, end = 5 (aka begin < x <= end)
  • begin = 1, end = 5 (aka begin <= x <= end)
  • begin = 0, end = 6 (aka begin < x < end)
  • begin = 1, end = 6 (aka begin <= x < end (the C++ STL and many other libraries seem to choose this)).

I'm not sure what measures I should use to choose one of these options.


回答1:


I was hoping someone would give me a link to a nice paper that E.W. Dijkstra wrote on the topic. I managed to plug just the right search terms into Google, and found the link I was looking for. The paper is "Why numbering should start at 0" and also covers why ranges should be represented with a half open interval [begin, end).

The basic argument has several pieces:

  1. Direct experience with a programming environment (the programming language Mesa at Xerox PARC) that had support for all 4 different choices resulted in people standardizing on [start, end) because of frequent errors made with all the other choices.
  2. If you have an interval that starts at 0, having the start be -1 or something similar is just awkward and broken. This argues strongly for the interval starting at begin (i.e. all the begin <= x choices).
  3. The math for determining the interval size, for computing the start of the next adjacent interval, and a whole bunch of other similar things just works out nicely if end is one past start. For example, the size is end - begin. And end is the begin of the next adjacent interval. There are fewer chances for off-by-one errors in your calculations.
    • On a related note, the empty range is [begin, begin), and very obvious. It would have to be the rather awkward [begin, begin - 1] if it were closed on both sides. This is especially awkward of your range begins at 0.



回答2:


I personally would choose the option

  • begin = 1, end = 5 (aka begin <= x <= end)

I like to keep my structures as clear and similar to the human reasoning as possible. If you tell someone "the numbers between 1 and 5" both 1 and 5 are meant to be in the set.

Of course if there are good technical reasons to use something else why not but if there are none I would choose the option which is easier to understand at the first glance.




回答3:


I'd say it depends on the (impicit or explicit) type of the interval you're trying to express. For floats and rationals, I think I'd prefer half-open intervals (so, essentially min <= value < max or min < value <= max). For integral values, the transformation between open, closed and half-open intervals is trivial, so I'd probably go with half-open intervals there as well.




回答4:


Interesting question. I'd like to suggest since foreach loop is quite ubiquitous now, which to choose becomes less relevant. You can just loop through a collection without knowing which range convention the underlying implementation uses.



来源:https://stackoverflow.com/questions/8441749/representing-intervals-or-ranges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!