Skip to content

Floats

Constants

Action Code Details
NaN
math.nan
NaN
float('nan')
NaN
numpy.nan
Infinity
math.inf
Infinity
float('inf')
Infinity
numpy.inf
Negative infinity
-math.inf
Negative infinity
float('-inf')
Negative infinity
numpy.NINF
Pi
math.pi
e
math.e
Float epsilon (smallest representable difference)
sys.float_info.epsilon
Min float
sys.float_info.min
Max float
sys.float_info.max

Create

float() throws ValueError if the input cannot be parsed

Action Code Details
Float from string
float(x)
Throws ValueError if string cannot be parsed
Float from hex string representation
float.fromhex(x)
Float from string for locale
locale.atof(x)
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
float.is_integer(x)
e.g., True for 5.0
Approximately equal
math.isclose(x, y)
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
math.isnan(x)
Does not work for complex numbers (?)
Finite
math.isfinite(x)
Infinite
math.isinf(x)
Positive infinity
math.isinf(x) and x > 0
Negative infinity
math.isinf(x) and x < 0

Extract

Action Code Details
Hash
hash(x)

Convert

Action Code Details
To string
str(x)
To string, with comma as thousands separator
f'{x:,}'
To string according to locale
import locale
locale.str(float)
E.g., different decimal symbol
To hex string representation
x.hex()
Format: [sign] ['0x'] integer ['.' fraction] ['p' exponent], e.g., 0x1.400000p+1
To bytes
struct.pack('f', x)