To initialize float constants to +inf, -inf, nan I always use float() called with a string:
print(float('inf'), float('+inf'), float('-inf'), float('nan'))
This prints:
[inf, inf, -inf, nan]
1.) Does there exist in Python an alternative method of initializing these constants (not calling float with a string)?
2.) Can I produce these constants (+/-inf, nan) with some mathematical operation?
E.g. for setting the variable f
to +inf
, by writing something like f = 1.0 / 0.0
(obviously, this is a division by zero error).
But in python, as it is a dynamic language, float values can be used to represent an infinite integer. One can use float('inf') as an integer to represent it as infinity. Below is the list of ways one can represent infinity in Python.
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis.
inf is infinity - a value that is greater than any other value. -inf is therefore smaller than any other value. nan stands for Not A Number, and this is not equal to 0 .
The NaN values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0. In C, this is implemented as a macro that returns an int value. The type of x shall be float , double or long double .
What does float ("NaN") and float ("inf") mean in Python? I would like to know what is a float ("inf") and a float ("NaN"). As already commented, float("inf") will create an infinite number:
The floating point specification is called IEEE 754-2008, and is behind a paywall. However, the Wikipedia page is quite detailed. C++ NaN stands for not a number. It is a value that depicts an undefined or unspecified value with floating point.
For those who are unfamiliar with the notation of inf and NaN, they stand for infinity and Not-a-Number, respectively. Even though infinity can be thought of as an extremely very large number, it has no end.
C++ INF denotes “infinity”. Due to the finite nature of floating point numbers ( 32-bit for floats, 64-bit for doubles), infinite is represented by a finite value. This type of error condition / value arises when the resultant number is overflowing or underflowing the capacity of the floating point number.
Technically, yes, there are other ways of initializing such values, but they're all either less obvious, or much less convenient.
If your platform uses IEEE floating point,1 any float
arithmetic that overflows, without raising any other flags besides overflow, is guaranteed to give you inf
. This means 1.0 / 0.0
probably won't work (Python will detect that this is a division by zero), but the even simpler 1e500
will.2
Once you have inf
, you can just do -inf
and inf/inf
to get the negative infinity and NaN values.
But would someone reading your code understand 1e500 / 1e500
as readily as float('nan')
? Probably not.
Meanwhile, you can always do something like struct.unpack('>f', b'\x7f\x80\0\0')[0]
, which unpacks the well-defined bit pattern for an IEEE big-endian double inf
value as a float
, whether your float
is that type under the covers or not. But why would you want to write (or read) that?3
But, if you're using Python 3.5 or later, you don't need to initialize those values; you can just use the constants in the math
module:
print(math.inf, +math.inf, -math.inf, math.nan)
And if you're using Python 2.7 or 3.4 or something, you can always just define your own constants and use them over and over:
inf, nan = float('inf'), float('nan')
print(inf, +inf, -inf, nan)
1. Technically, Python doesn't require IEEE floating point. In fact, what it requires are something that acts like the platform's C double
—which C doesn't require to be an IEEE type, and only if that makes sense for the implementation (e.g., Jython is obviously going to use the relevant Java type without caring what the C compiler used to compile the JVM thinks), and it doesn't clarify exactly what it means to act like a C double. However, the float
type—not to mention things like the math
module—really isn't going to work unless float
is something reasonably close to an IEEE float type, like maybe the pre-IEEE IBM and Intel types or the not-quite-IEEE Motorola compat types. Also, as of 2018, the only supported platforms by any of the three existing Python 3.x implementations all give you either IEEE 754-1985 double
or IEEE 754-2008 float64
. But, if this is really a potential issue for your code, you should check sys.float_info
to verify whatever assumptions are relevant.
2. It's conceivable that some platform might use an IEEE 754-1985 long double
or an IEEE 754-2008 float128
or something. If you're worried about that, just use a bigger number. Or, say, 1e500 ** 1e500 ** 1e500
.
3. Well, if you specifically need a quiet or signaling NaN, or one with a custom bit pattern instead of the default one… but anyone who needs that presumably already knows they need that.
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