C++ std::copysign() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::copysign(x, y) builds a number from two parts. The size comes from x and the sign comes from y. So copysign(5.0, -1.0) is -5 and copysign(-5.0, 2.0) is 5.

Only the sign of y matters. Its size is ignored. This makes copysign() handy when you want to move in a direction without writing an if, and it can also see the sign of negative zero.

std::copysign() is declared in <cmath> and is part of the group Absolute value and sign. These functions work with the sign of a number. abs() and fabs() remove a minus sign, and copysign() and signbit() copy or read it.

Syntax

Include <cmath> and call std::copysign. Start with these lines.

C++
double a = std::copysign(5.0, -1.0);    // -5
double b = std::copysign(-5.0, 2.0);    //  5
float  c = std::copysign(2.5f, -3.0f);  // -2.5f
CallWhat it does
std::copysign(5.0, -1.0)Size of 5.0 with the sign of -1.0. Returns -5.
std::copysign(-5.0, 2.0)The sign of y is positive, so the result is 5.
std::copysign(3.0, -0.0)Negative zero counts as a negative sign. Returns -3.

Write std::copysign so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       copysign(float x, float y);
double      copysign(double x, double y);
long double copysign(long double x, long double y);
// plus overloads for mixed and integer arguments

Parameters

ParameterDescription
xRequired. The number that gives the size (magnitude) of the result.
yRequired. The number that gives the sign of the result. Only its sign is used.

Return Value

Return ValueDescription
Value with y's signThe magnitude of x and the sign of y.
NaNIf x is NaN, the result is NaN, but it takes the sign of y.

C++ Examples

Example 1 — Basic copysign()

Give a size and a sign from two different numbers, and watch which one decides what.

C++
#include <cmath>
#include <iostream>

int main() {
    std::cout << "copysign(5.0, -1.0)  = " << std::copysign(5.0, -1.0) << '\n';
    std::cout << "copysign(-5.0, 2.0)  = " << std::copysign(-5.0, 2.0) << '\n';
    std::cout << "copysign(3.5, -0.0)  = " << std::copysign(3.5, -0.0) << '\n';
    std::cout << "copysign(0.0, -2.0)  = " << std::copysign(0.0, -2.0) << '\n';
    return 0;
}

How It Works

  1. The first call takes the size 5 from x and the minus sign from y, giving -5.
  2. In the second call the sign of y is plus, so the minus sign of -5.0 is thrown away and the result is 5.
  3. -0.0 has a negative sign, so 3.5 becomes -3.5. A zero size stays zero, but it can carry a sign, so the last line prints -0.

Example 2 — Step Toward a Target

A robot moves a fixed step of 0.5. The direction depends on where the target is, and copysign() picks it without an if.

C++
#include <cmath>
#include <iostream>

int main() {
    double position = 10.0;
    double step = 0.5;

    double targetLow = 4.0;
    double targetHigh = 20.0;

    double moveA = std::copysign(step, targetLow - position);
    double moveB = std::copysign(step, targetHigh - position);

    std::cout << "Toward 4:  move " << moveA << " to " << position + moveA << '\n';
    std::cout << "Toward 20: move " << moveB << " to " << position + moveB << '\n';
    return 0;
}

How It Works

  1. targetLow - position is -6. Only its sign is used, so copysign(0.5, -6.0) gives -0.5.
  2. targetHigh - position is 10, a positive number, so the same step becomes +0.5.
  3. The step size never changes. Only the direction does. Adding the move to position gives 9.5 and 10.5.

Example 3 — Seeing the Sign of Negative Zero

A normal comparison cannot tell -0.0 from 0.0, but copysign() can.

C++
#include <cmath>
#include <iostream>

int main() {
    double z = -0.0;

    std::cout << "z < 0:  " << (z < 0) << '\n';
    std::cout << "z == 0: " << (z == 0) << '\n';
    std::cout << "copysign(1.0, z) = " << std::copysign(1.0, z) << '\n';
    std::cout << "copysign(1.0, 0.0) = " << std::copysign(1.0, 0.0) << '\n';
    return 0;
}

How It Works

  1. -0.0 is not less than zero, so z < 0 prints 0. A normal comparison cannot see the sign here.
  2. z == 0 is true, because -0.0 and 0.0 compare equal. That is why the sign needs another tool.
  3. copysign(1.0, z) looks at the sign bit and returns -1, while copysign(1.0, 0.0) returns 1. This is the quick way to tell them apart.

Frequently Asked Questions

std::copysign(x, y) returns a number with the size (magnitude) of x and the sign of y.
A value with the magnitude of x and the sign of y. The sign of y is used even when y is -0.0.
No. Only the sign of y is used. copysign(5.0, -100.0) and copysign(5.0, -0.1) both return -5.
You can, but copysign() is shorter, and it also handles -0.0 and NaN signs correctly, which a comparison like y < 0 does not.
Include <cmath> and write std::copysign(). That always works and picks the C++ overloads. The global name ::copysign often works too, but the standard does not promise it, and it may only take a double.

Next: cos()

Keep going through the <cmath> functions. Cosine of an angle in radians.

cos() tutorial →

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.

8 people found this page helpful