198

I have seen number ranges represented as [first1,last1) and [first2,last2).

I would like to know what such a notation means.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Nav
  • 19,885
  • 27
  • 92
  • 135
  • 5
    `[first, last)` is a half-open interval as others have noted. In some textbooks, this is also written as `[first, last>` and has exactly the same meaning, only the syntax is different. – darioo Dec 09 '10 at 08:49
  • 13
    A better place for this question would be http://math.stackexchange.com/ (IMHO). But never mind! :) – xk0der Dec 09 '10 at 09:28
  • 13
    As a Mnemonic, think the square bracket grabs on to that value, meaning "up to and including". And the round parenthesis is softer and less restrictive meaning: "up to but not including". – Eric Leschinski Feb 06 '16 at 16:29
  • As a programmer whenever I see square brackets it always gives me a remembrance of Extended Backus-Naur form - https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form – RBT Aug 08 '16 at 23:47
  • 3
    I recommend migrating this to math.SE – Ky - Sep 06 '16 at 12:09
  • 7
    Alternative mnemonic, if you put the square brackets back to back in reverse order it looks like a capital I for Inclusive: `][`. Or if you put parenthesis back to back in reverse order it looks like an X for eXclusive: `)(` – Brad Cupit Jan 19 '21 at 20:37

4 Answers4

325

A bracket - [ or ] - means that end of the range is inclusive -- it includes the element listed. A parenthesis - ( or ) - means that end is exclusive and doesn't contain the listed element. So for [first1, last1), the range starts with first1 (and includes it), but ends just before last1.

Assuming integers:

  • (0, 5) = 1, 2, 3, 4
  • (0, 5] = 1, 2, 3, 4, 5
  • [0, 5) = 0, 1, 2, 3, 4
  • [0, 5] = 0, 1, 2, 3, 4, 5
Stefan
  • 919
  • 2
  • 13
  • 24
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • 5
    This evolves from grade school pre-algebra, where you learn about functions f(x) and the function's domain and range, where a function like f(x)=x^2, would have a range of 0 to positive infinity, denoted with [0,∞). – JohnMerlino Nov 02 '14 at 15:31
  • 2
    @Timbo ∞ is not a number. – JakeD Jan 20 '17 at 00:29
  • 5
    @pycoder your definition of number seems unnecessarily limiting. https://en.wikipedia.org/wiki/Surreal_number – Tim Sparkles Jan 20 '17 at 00:49
  • @Timbo https://en.wikipedia.org/wiki/Infinity If infinity is a number, then it is the only number that I know of such that x / x != 1 and the only number where x / 2 = x (with the exception of 0). – JakeD Jan 20 '17 at 01:00
  • 1
    @pycoder. Almost correct. Try also plugging -∞ and 1/∞ into your equations. One is the only number I know of such that y * x = y. Zero, one, infinities, and infinitesimals are unlike other numbers in a number of ways. Each type of number -- each number -- is unlike all other numbers in a number of ways. The ancient Greeks didn't think transcendental numbers were numbers. Imaginary and complex numbers are outside the hyperreal number sets. When people say "∞ is not a number" they mean "∞ is not a _real_ number" or similar for whatever subset of numbers they want you to work in. – Tim Sparkles Jan 20 '17 at 02:12
  • @Timbo Good point, although I wasn't intending to imply that because infinity has unique qualities, it is not a number. I suppose it depends on the definition of number. My initial comment was a quote that I remember from my Calculus I professor. I hadn't thought to challenge it, and I guess I missed the context he was intending to apply that definition. – JakeD Jan 20 '17 at 02:18
  • 8
    @JakeD Regarding your initial comment, you're right in a way that infinity is not a number, hence why the set [0, ∞) does not include it. – wjandrea Jul 28 '17 at 18:30
  • 7
    ∞ isn't an *ordinal* number, of the sort that you can do arithmetic with. But it's a valid cardinal number when answering questions like "How many integers are there?". It's also, as in this case, perfectly valid as a *limit* – Kevin Wright Aug 03 '18 at 21:46
43

That's a half-open interval.

  • A closed interval [a,b] includes the end points.
  • An open interval (a,b) excludes them.

In your case the end-point at the start of the interval is included, but the end is excluded. So it means the interval "first1 <= x < last1".

Half-open intervals are useful in programming because they correspond to the common idiom for looping:

for (int i = 0; i < n; ++i) { ... } 

Here i is in the range [0, n).

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
22

The concept of interval notation comes up in both Mathematics and Computer Science. The Mathematical notation [, ], (, ) denotes the domain (or range) of an interval.

  • The brackets [ and ] means:

    1. The number is included,
    2. This side of the interval is closed,
  • The parenthesis ( and ) means:

    1. The number is excluded,
    2. This side of the interval is open.

An interval with mixed states is called "half-open".

For example, the range of consecutive integers from 1 .. 10 (inclusive) would be notated as such:

  • [1,10]

Notice how the word inclusive was used. If we want to exclude the end point but "cover" the same range we need to move the end-point:

  • [1,11)

For both left and right edges of the interval there are actually 4 permutations:

(1,10) =   2,3,4,5,6,7,8,9       Set has  8 elements
(1,10] =   2,3,4,5,6,7,8,9,10    Set has  9 elements
[1,10) = 1,2,3,4,5,6,7,8,9       Set has  9 elements
[1,10] = 1,2,3,4,5,6,7,8,9,10    Set has 10 elements

How does this relate to Mathematics and Computer Science?

Array indexes tend to use a different offset depending on which field are you in:

  • Mathematics tends to be one-based.
  • Certain programming languages tends to be zero-based, such as C, C++, Javascript, Python, while other languages such as Mathematica, Fortran, Pascal are one-based.

These differences can lead to subtle fence post errors, aka, off-by-one bugs when implementing Mathematical algorithms such as for-loops.

Integers

If we have a set or array, say of the first few primes [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ], Mathematicians would refer to the first element as the 1st absolute element. i.e. Using subscript notation to denote the index:

  • a1 = 2
  • a2 = 3
  • :
  • a10 = 29

Some programming languages, in contradistinction, would refer to the first element as the zero'th relative element.

  • a[0] = 2
  • a[1] = 3
  • :
  • a[9] = 29

Since the array indexes are in the range [0,N-1] then for clarity purposes it would be "nice" to keep the same numerical value for the range 0 .. N instead of adding textual noise such as a -1 bias.

For example, in C or JavaScript, to iterate over an array of N elements a programmer would write the common idiom of i = 0, i < N with the interval [0,N) instead of the slightly more verbose [0,N-1]:

function main() {
    var output = "";
    var a = [ 2, 3, 5, 7,  11, 13, 17, 19, 23, 29 ];
    for( var i = 0; i < 10; i++ ) // [0,10)
       output += "[" + i + "]: " + a[i] + "\n";

    if (typeof window === 'undefined') // Node command line
        console.log( output )
    else
        document.getElementById('output1').innerHTML = output;
}
 <html>
     <body onload="main();">
         <pre id="output1"></pre>
     </body>
 </html>

Mathematicians, since they start counting at 1, would instead use the i = 1, i <= N nomenclature but now we need to correct the array offset in a zero-based language.

e.g.

function main() {
    var output = "";
    var a = [ 2, 3, 5, 7,  11, 13, 17, 19, 23, 29 ];
    for( var i = 1; i <= 10; i++ ) // [1,10]
       output += "[" + i + "]: " + a[i-1] + "\n";

    if (typeof window === 'undefined') // Node command line
        console.log( output )
    else
        document.getElementById( "output2" ).innerHTML = output;
}
<html>
    <body onload="main()";>
        <pre id="output2"></pre>
    </body>
</html>

Aside:

In programming languages that are 0-based you might need a kludge of a dummy zero'th element to use a Mathematical 1-based algorithm. e.g. Python Index Start

Floating-Point

Interval notation is also important for floating-point numbers to avoid subtle bugs.

When dealing with floating-point numbers especially in Computer Graphics (color conversion, computational geometry, animation easing/blending, etc.) often times normalized numbers are used. That is, numbers between 0.0 and 1.0.

It is important to know the edge cases if the endpoints are inclusive or exclusive:

  • (0,1) = 1e-M .. 0.999...
  • (0,1] = 1e-M .. 1.0
  • [0,1) = 0.0 .. 0.999...
  • [0,1] = 0.0 .. 1.0

Where M is some machine epsilon. This is why you might sometimes see const float EPSILON = 1e-# idiom in C code (such as 1e-6) for a 32-bit floating point number. This SO question Does EPSILON guarantee anything? has some preliminary details. For a more comprehensive answer see FLT_EPSILON and David Goldberg's What Every Computer Scientist Should Know About Floating-Point Arithmetic

Some implementations of a random number generator, random() may produce values in the range 0.0 .. 0.999... instead of the more convenient 0.0 .. 1.0. Proper comments in the code will document this as [0.0,1.0) or [0.0,1.0] so there is no ambiguity as to the usage.

Example:

  • You want to generate random() colors. You convert three floating-point values to unsigned 8-bit values to generate a 24-bit pixel with red, green, and blue channels respectively. Depending on the interval output by random() you may end up with near-white (254,254,254) or white (255,255,255).
     +--------+-----+
     |random()|Byte |
     |--------|-----|
     |0.999...| 254 | <-- error introduced
     |1.0     | 255 |
     +--------+-----+

For more details about floating-point precision and robustness with intervals see Christer Ericson's Real-Time Collision Detection, Chapter 11 Numerical Robustness, Section 11.3 Robust Floating-Point Usage.

Community
  • 1
  • 1
Michaelangel007
  • 2,798
  • 1
  • 25
  • 23
1

It can be a mathematical convention in the definition of an interval where square brackets mean "extremal inclusive" and round brackets "extremal exclusive".

Eddy
  • 1,662
  • 2
  • 21
  • 36