Python math.copysign() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.copysign(x, y) returns a float with the magnitude (absolute value) of x and the sign of y.

Because it inspects the IEEE 754 sign bit of y directly, math.copysign(3.0, -0.0) returns -3.0 even though -0.0 == 0.0 is True.

math.copysign() is a function in Python's standard math module: import it with import math and call math.copysign(...), or import the name directly with from math import copysign.

Syntax

Import math and pass magnitude source x and sign source y.

python
import math

signed = math.copysign(x, y)
CallWhat it does
math.copysign(3, -0.0)Returns -3.0 (copies negative zero sign bit).
math.copysign(-5, 2)Returns 5.0.
math.copysign(0.0, -4)Returns -0.0.
math.copysign(4.5, float("-inf"))Returns -4.5.

On Python versions without math.signbit(), math.copysign(1.0, x) < 0 is the standard way to test whether x has its sign bit set.

Function signatures
python
math.copysign(x, y, /)
# returns: float with |x| and sign bit of float(y)

Parameters

ParameterDescription
xRequired positional real number supplying the magnitude.
yRequired positional real number supplying the sign bit.

Return Value

Return ValueDescription
floatFloat combining fabs(x) with the sign bit of y.

Python Examples

Example 1 — Combine Magnitudes and Signs Including Negative Zero

Copy positive, negative, and negative-zero signs onto numbers as floats.

python
import math

print(math.copysign(3, -0.0))
print(math.copysign(-5, 2))
print(math.copysign(12.5, -1))
print(math.copysign(0.0, -7))

How It Works

  1. Negative zero transfers minus. Passing -0.0 as y turns positive 3 into -3.0.
  2. Positive strips minus. Passing 2 turns -5 into 5.0.
  3. Creates signed zero. Copying a negative sign onto 0.0 produces -0.0.

Example 2 — Step a Slider Toward Any Target Without Branching

Move pos toward target by a fixed step size using math.copysign(step, target - pos).

python
import math

pos = 10.0
step = 2.5
for target in [25.0, 3.0, 10.0]:
    delta = math.copysign(step, target - pos)
    print(f"target={target:>4} -> delta={delta:+5.1f}")

How It Works

  1. Sign carries direction. Subtracting target - pos supplies positive or negative direction without writing an if/else.
  2. Constant magnitude kept. Step size stays fixed at 2.5 regardless of distance.
  3. Zero offset uses positive zero. When target == pos, +0.0 gives +2.5.

Example 3 — Detect Negative Zero Bit reliably With copysign()

Test whether a float is -0.0 when == 0.0 cannot distinguish the sign.

python
import math


def is_negative_zero(x):
    return x == 0.0 and math.copysign(1.0, x) < 0.0


print(-0.0 == 0.0)
print(is_negative_zero(-0.0), is_negative_zero(0.0))
print(math.copysign(4.5, float("-inf")))

How It Works

  1. Equality hides the sign. In Python, -0.0 == 0.0 evaluates to True.
  2. Sign test reveals it. Placing x as the second argument of copysign(1.0, x) reflects its sign onto 1.0, cleanly separating -0.0 from 0.0.
  3. Infinite sign works too. Passing -inf flips 4.5 to -4.5.

Frequently Asked Questions

A float with the absolute value of x and the sign of y.
Because IEEE 754 negative zero has its sign bit set to 1, and copysign copies the sign bit directly rather than checking y < 0.
Check x == 0.0 and math.copysign(1.0, x) < 0.0 (or math.signbit(x) on Python versions that provide it).
Yes, even when both inputs are integers.
math.copysign(float("nan"), -1) returns a NaN with its sign bit set, printed as nan.

Next: math.cos()

Return the cosine of x radians as a float.

cos() →

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.

9 people found this page helpful