pythonmathnumberssetinfinite

How to check if the set with commas is infinite or finite?


I am trying to use the method math.isinf to find out if the set is infinite. The set is

{...,-5,-4,-3,-2,-1,0,1,2,3,4,5,...}
import math
Infinte_set = {-math.inf,-5,-4,-3,-2,-1,0,1,2,3,4,5,math.inf}
print(math.isinf(Infinte_set))

I was expecting True or False but what I got is this:

TypeError                                 Traceback (most recent call last)
<ipython-input-13-3d08b071af6f> in <module>
      5 import math
      6 Infinte_set = {-math.inf,-5,-4,-3,-2,-1,0,1,2,3,4,5,math.inf}
----> 7 print(math.isinf(Infinte_set))

TypeError: must be real number, not set

Solution

  • You can pass the min value or max value of the set in math.isinf function to check for infinity.

    print(math.isinf(min(Infinte_set)) or math.isinf(max(Infinte_set)))
    

    math.isinf(min(Infinte_set)) -> min(Infinte_set) would be the minimum numerical value, in your case it would be -infinity.

    math.isinf(max(Infinte_set)) -> max(Infinte_set) would be the minimum numerical value, in your case it would be +infinity.

    If in case you don't know how or works:

    True or False -> True
    False or True -> True
    False or False -> False
    True or True -> True