Simple question but, I see exclusive and inclusive when referring to number ranges.
For example, this is a line from an algorithms book:
The following function prints the powers of 2 from 1 through n (inclusive).
What is meant by this? What makes a number range inclusive or exclusive?
We could do this pretty easily: my_list = range(51) … the word “inclusive” means that the value 50 should be included in the range. So, the ending value of the range is set to 51 (exclusive) in the Python statement, meaning that 51 is not included in the range. Submitted by Glenn Richard. almost 8 years.
There really are two kinds of ranges. One is the exclusive range, which is the highest score minus the lowest score (or h − l) and the one we just defined. The second kind of range is the inclusive range, which is the highest score minus the lowest score plus 1 (or h − l + 1).
The largest number in the interval is written second, following a comma. Parentheses, ( or ), are used to signify that an endpoint value is not included, called exclusive. Brackets, [ or ], are used to indicate that an endpoint value is included, called inclusive.
In Computer Science, inclusive/exclusive doesn't apply to algorithms, but to a number range (more specifically, to the endpoint of the range):
1 through 10 (inclusive) 1 2 3 4 5 6 7 8 9 10 1 through 10 (exclusive) 1 2 3 4 5 6 7 8 9
In mathematics, the 2 ranges above would be:
[1, 10] [1, 10)
You can remember it easily:
The following function prints the powers of 2 from 1 through n (inclusive).
This means that the function will compute 2^i
where i = 1, 2, ..., n
, in other words, i
can have values from 1 up to and including the value n
. i.e n is Included in Inclusive
If, on the other hand, your book had said:
The following function prints the powers of 2 from 1 through n (exclusive).
This would mean that i = 1, 2, ..., n-1
, i.e. i
can take values up to n-1, but not including, n
, which means i = n-1
is the highest value it could have.i.e n is excluded in exclusive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With