Floats
Constants
| Action |
Code |
Details |
|
NaN
|
|
|
|
NaN
|
|
|
|
NaN
|
|
|
|
Infinity
|
|
|
|
Infinity
|
|
|
|
Infinity
|
|
|
|
Negative infinity
|
|
|
|
Negative infinity
|
|
|
|
Negative infinity
|
|
|
|
Pi
|
|
|
|
e
|
|
|
|
Float epsilon (smallest representable difference)
|
|
|
|
Min float
|
|
|
|
Max float
|
|
|
Create
float() throws ValueError if the input cannot be parsed
| Action |
Code |
Details |
|
Float from string
|
|
Throws ValueError if string cannot be parsed |
|
Float from hex string representation
|
|
|
|
Float from string for locale
|
|
|
|
Float from string for temporary locale
|
Babel.parse_decimal('1,25', locale='nl_NL.utf8')
|
No way to do this cleanly and thread-safe in standard Python... |
|
Float from string for temporary locale
|
loc = locale.getlocale(locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, 'nl_NL')
f = locale.atof(x)
locale.setlocale(locale.LC_NUMERIC, loc)
|
|
|
Float from packed struct bytes
|
struct.unpack('f', x)[0] | e.g., b'x00x00 @'
|
|
Test
| Action |
Code |
Details |
|
Whole number
|
|
e.g., True for 5.0 |
|
Approximately equal
|
|
Uses proportional tolerance |
|
Approximately equal with proportional tolerance tol%
|
math.isclose(x, y, rel_tol=tol)
|
|
|
Approximately equal with absolute tolerance tol
|
math.isclose(x, y, abs_tol=tol)
|
|
|
NaN
|
|
Does not work for complex numbers (?) |
|
Finite
|
|
|
|
Infinite
|
|
|
|
Positive infinity
|
|
|
|
Negative infinity
|
|
|
Convert
| Action |
Code |
Details |
|
To string
|
|
|
|
To string, with comma as thousands separator
|
|
|
|
To string according to locale
|
import locale
locale.str(float)
|
E.g., different decimal symbol |
|
To hex string representation
|
|
Format: [sign] ['0x'] integer ['.' fraction] ['p' exponent], e.g., 0x1.400000p+1 |
|
To bytes
|
|
|