Search code examples
pythonfloating-pointinfinity

How to get a floating point infinity that when multiplied by zero gives zero


How do I get a "small enough" infinity that its product with zero is zero?

I'm using an integer programming solver for Python, and some of my variables have infinite cost. I can't use float('inf') because float('inf')*0 = NaN


Solution

  • You might use the largest finite number that float can hold:

    In [9]: print sys.float_info.max
    1.79769313486e+308
    
    In [10]: sys.float_info.max * 0
    Out[10]: 0.0