Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for representing floating infinity in python 3.x

Since a very large floating value can be represented as follows: sys.float_info.max and float("inf").

Is one better than the other? If yes, under what circumstances?

Or which is one more preferred for scientific computing?

like image 930
letsBeePolite Avatar asked Nov 26 '25 12:11

letsBeePolite


2 Answers

Definitely not the first:

>>> import sys
>>> sys.float_info.max
1.7976931348623157e+308

That's the largest finite float.

You're missing the preferred method ;-)

>>> import math
>>> math.inf
inf

Note also:

>>> math.nan
nan
like image 126
Tim Peters Avatar answered Nov 29 '25 02:11

Tim Peters


This should indirectly tell you everything you need to know:

>>> float('inf') > sys.float_info.max
True
>>> float('inf') <= sys.float_info.max
False
like image 41
Mark Ransom Avatar answered Nov 29 '25 03:11

Mark Ransom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!