Python math.signbit() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Recent Python

Definition

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)
CallWhat 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.

Function signatures
python
math.signbit(x, /)
# recent Python addition (3.15 at writing); returns bool

Parameters

ParameterDescription
xRequired positional real number (int or float).

Return Value

Return ValueDescription
boolTrue if sign bit is 1 (negative), else False.

Python Examples

Example 1 — Inspect Sign Bits Across Negative Zero, Normals, and Infinities

Use hasattr(math, "signbit") with math.copysign(1.0, x) < 0.0 fallback.

python
import math


def has_neg_sign(x):
    if hasattr(math, "signbit"):
        return math.signbit(x)
    return math.copysign(1.0, float(x)) < 0.0


for v in [3.5, -3.5, 0.0, -0.0, -math.inf, math.inf]:
    print(f"{v!r:>4} -> {has_neg_sign(v)}")

How It Works

  1. Detects -0.0 cleanly. Even though -0.0 == 0.0, the fallback copysign(1.0, -0.0) yields -1.0 < 0 -> True.
  2. Handles both infinities. Negative infinity returns True and positive infinity returns False.
  3. Portable helper pattern. Works identically across Python 3.8 through recent releases.

Example 2 — Compare x < 0 Against Sign Bit on -0.0 and Signed NaN

See x < 0 fail on -0.0 and negative-sign NaN while sign-bit inspection returns True.

python
import math

sign_bit = getattr(
    math, "signbit", lambda x: math.copysign(1.0, float(x)) < 0.0
)
neg_nan = math.copysign(math.nan, -1.0)

for label, val in [("neg_zero", -0.0), ("neg_nan", neg_nan)]:
    print(f"{label}: less_than_zero={val < 0}, signbit={sign_bit(val)}")

How It Works

  1. Why < 0 misses -0.0. Numerically -0.0 equals zero, so -0.0 < 0 evaluates to False.
  2. Why < 0 misses NaN. Any comparison with NaN returns False, yet math.copysign() set its sign bit to 1.
  3. 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])

How It Works

  1. Safe feature guard. Prevents AttributeError on versions before math.signbit() existed.
  2. Matching output list. Positive zero returns False while -0.0 and -2.5 return True.
  3. 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.
True.
A bool (True or False).

Next: math.sin()

Return the sine of x radians as a float.

sin() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful