Writing down a set of numbers
A thermostat, a run of accepted values, and the three notations that describe that run.
A thermostat in a server room is set to hold the temperature at 18 degrees or warmer, and strictly below 22, because at 22 the alarm sounds. Somebody hands you a list of readings and asks which ones the thermostat would call acceptable.
That is a question about a set of numbers, and the fastest way to see the shape of the answer is to test every candidate and look at the pattern.
Change t < high to t <= high and run it again. Exactly one line changes. That single line is the whole difference between two of the notations below.
The accepted readings are not scattered. They form one unbroken run, from 18 up to 22, with 18 in and 22 out. The interesting information is only at the two ends: where the run starts, where it stops, and whether each end is itself included.
Interval notation
An interval is an unbroken run of the number line, described by its two ends. The notation records the ends and whether each one is included. A square bracket means the end is included; a round bracket means it is not.
| Interval | Read as | In words |
|---|---|---|
| closed | everything from to , both ends included | |
| open | everything strictly between and , neither end included | |
| half-open | is included, is not | |
| half-open | is included, is not |
The thermostat's acceptable set is . Eighteen is in, so it gets a square bracket. Twenty-two is out, so it gets a round one. That is the entire answer, written in five characters.
For a run that never stops, the symbol (infinity) marks the end that goes on forever, and marks one that goes on forever to the left. So is every number 3 or above, and is every negative number.
Set-builder notation
Interval notation only describes unbroken runs. Set-builder notation describes a set by stating the condition its members satisfy, so it can describe anything. It is written , and read aloud as "the set of all such that the condition holds". The vertical bar is read "such that"; some books use a colon instead, and it means the same.
Notice that the right hand side is the condition your program computed, character for character. Set-builder notation is the mathematical spelling of an if.
Combining two sets
Two conditions can be demanded at once, or either one accepted. Run both over the same numbers and watch which values survive each.
Change the second condition to (x > 4) and (x <= 5) and run it again. Now no value at all is in both. Which column is the one that went empty?
The values in both sets form the intersection, written . The values in one or the other or both form the union, written . Intersection is and; union is or.
A worked example, every step
Take and . Find and .
- Draw both on one line, or list the conditions: says , and says .
- For the intersection, both conditions must hold. The left end has to satisfy the stricter of the two left conditions: and together mean , because 0 is the further right of the two starting points, and it is excluded.
- The right end works the same way: and together mean , because 3 is the further left of the two stopping points, and it is excluded.
- So . Both ends are round, because both came from a strict condition.
- For the union, a number needs only one condition. covers from (included) up to 3, and covers from just after 0 up to 5 (included). They overlap on , so the two runs join into one run with no gap.
- So . The left end keeps the square bracket it had in , and the right end keeps the square bracket it had in .
Write in interval notation.
. The word "or" is a union. The first piece runs from the far left up to 1, excluded, so it is . The second starts at 6, included, and never stops, so it is . The two pieces do not touch, so the union symbol has to stay.