math.signbit(x) returns True if the sign bit of float(x) is set (negative), and False otherwise.
Unlike testing x < 0, signbit() detects negative zero (-0.0) and negative-signed NaN (copysign(nan, -1)), as well as negative finite numbers and -inf.
math.signbit() is a function in Python's standard math module: import it with import math and call math.signbit(...), or import the name directly with from math import signbit. New in recent Python versions (3.15 at the time of writing; check your version with import sys; print(sys.version)).
Syntax
Import math (or use math.copysign(1.0, x) < 0 for older Python versions).
python
import math
neg = math.signbit(x)
Call
What it does
math.signbit(-0.0)
Returns True (whereas -0.0 < 0 is False).
math.signbit(0.0)
Returns False.
math.signbit(float("-inf"))
Returns True.
math.signbit(3.5)
Returns False.
On Python versions without math.signbit(), math.copysign(1.0, float(x)) < 0.0 tests the exact IEEE 754 sign bit identically.
Why < 0 misses -0.0. Numerically -0.0 equals zero, so -0.0 < 0 evaluates to False.
Why < 0 misses NaN. Any comparison with NaN returns False, yet math.copysign() set its sign bit to 1.
Bit-level sign check succeeds. Both native signbit and copysign(1.0, val) < 0 detect the bit.
Example 3 — Detect math.signbit() Support at Runtime
Show explicit output for Python versions with math.signbit() and older fallback code.
python
import math
sample = [0.0, -0.0, -2.5]
if hasattr(math, "signbit"):
print("math.signbit:", [math.signbit(x) for x in sample])
else:
print("fallback:", [math.copysign(1.0, float(x)) < 0.0 for x in sample])
Output
# On a Python version that has math.signbit:
math.signbit: [False, True, True]
# Fallback on older Python versions:
fallback: [False, True, True]
Lines starting with # label which branch ran; your Python interpreter prints only the matching line.
How It Works
Safe feature guard. Prevents AttributeError on versions before math.signbit() existed.
Matching output list. Positive zero returns False while -0.0 and -2.5 return True.
Verify Python version. Run import sys; print(sys.version) when testing new math features.
Frequently Asked Questions
It is new in recent Python versions (3.15 at the time of writing); check your version with import sys; print(sys.version) or hasattr(math, "signbit").
x < 0 is False for -0.0 and signed -nan, whereas math.signbit(x) checks the IEEE 754 sign bit directly and returns True for both.
Use math.copysign(1.0, float(x)) < 0.0, which inspects the sign bit of x without comparing x numerically.