Python Glossary Русский

Python 3.15+Numbers & mathfunction

math.signbit()

representation

True if the float's sign bit is set. Tells -0.0 from 0.0 and finds the sign of infinities and NaNs, where x < 0 does not help.

Syntax

math.signbit(x)

-0.0 == 0.0 is true and -0.0 < 0 is false, even though zero has a sign that affects results: 1 / -0.0 in numpy gives -inf, and atan2(0.0, -0.0) equals pi. The sign used to be checked with math.copysign(1, x) < 0; signbit does it directly. It works for NaN too: NaNs can carry a sign, but comparisons with them are always false.

Examples

import math
print(-0.0 < 0, math.signbit(-0.0))  # → False True
print(math.signbit(0.0), math.signbit(float('-inf')))  # → False True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy