What is the meaning of “exclusive” and “inclusive” when describing number ranges?

前端 未结 3 1636
野趣味
野趣味 2021-02-01 05:23

Simple question but, I see exclusive and inclusive when referring to number ranges.

For example, this is a line from an algorithms book:

The follo

相关标签:
3条回答
  • 2021-02-01 05:34

    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:

    • Inclusive - Including the last number
    • Exclusive - Excluding the last number
    0 讨论(0)
  • 2021-02-01 05:41

    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.

    0 讨论(0)
  • 2021-02-01 05:47

    In simple terms, inclusive means within and the number n, while exclusive means within and without the number n.

    Note: that each argument should be marked its "clusivity"/ "participation"

    # 1 (inclusive) through 5 (inclusive)
    1 <= x <= 5 == [1, 2, 3, 4, 5]
    
    # 1 (inclusive) through 5 (exclusive)
    1 <= x < 5 == [1, 2, 3, 4]
    
    # 1 (exclusive) through 5 (inclusive)
    1 < x <= 5 == [2, 3, 4, 5]
    
    # 1 (exclusive) through 5 (exclusive)
    1 < x < 5 == [2, 3, 4]
    
    0 讨论(0)
提交回复
热议问题