C copysign() Function

Definition
In C, copysign() builds a number with the size of x and the sign of y. copysign(3.5, -1.0) is -3.5.
You pass two double values. The result is a double. Include <math.h>. On GCC, link with -lm.
Syntax
Pass the magnitude, then the sign source. Start with this call.
| Call | What it does |
|---|
copysign(3.5, -1.0) | Keeps 3.5 as the size and applies a minus sign. |
copysign(-2.0, 4.0) | Keeps 2.0 as the size and applies a plus sign. |
Include <math.h>. On GCC, link with -lm. The magnitude comes from x; the sign comes from y.
Library declarations
double copysign(double x, double y);
float copysignf(float x, float y);
long double copysignl(long double x, long double y);
Parameters
| Parameter | Description |
|---|
| x | Required. The value that supplies the magnitude (absolute size). |
| y | Required. The value that supplies the sign bit. |
Return Value
| Return Value | Description |
|---|
| double | A value with |x| and the sign of y. |
C Examples
Example 1 — Basic Sign Copy
Take a positive size and force a negative sign from another value.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", copysign(3.5, -1.0));
printf("%.1f\n", copysign(-2.0, 4.0));
return 0;
}
How It Works
- Include math.h. That declares
copysign. - First call. Size 3.5, sign from
-1.0 → -3.5. - Second call. Size 2.0, sign from
4.0 → 2.0. - Result type. Return value is still
double.
Example 2 — Signed Zero
On IEEE floating-point systems, +0.0 and -0.0 can differ only in the sign bit.
#include <stdio.h>
#include <math.h>
int main(void) {
double a = copysign(0.0, -1.0);
double b = copysign(0.0, 1.0);
printf("a = %.1f, signbit = %d\n", a, signbit(a));
printf("b = %.1f, signbit = %d\n", b, signbit(b));
return 0;
}
a = -0.0, signbit = 1
b = 0.0, signbit = 0
How It Works
- Copy a minus onto zero.
copysign(0.0, -1.0) can make -0.0. - Copy a plus onto zero.
copysign(0.0, 1.0) stays +0.0. - Use signbit. Print how the sign bit is set.
- Why it matters. Some algorithms care about signed zero.
Example 3 — Force Sign From a Flag
Use +1.0 or -1.0 as a simple sign flag for a magnitude.
#include <stdio.h>
#include <math.h>
int main(void) {
double mag = 12.5;
double sign_flag = -1.0;
double v = copysign(mag, sign_flag);
printf("value = %.1f\n", v);
return 0;
}
How It Works
- Pick a magnitude.
mag is 12.5. - Pick a sign flag.
-1.0 means negative. - Combine with copysign. Result is
-12.5. - Useful pattern. Keep size and direction in separate variables.
Frequently Asked Questions
copysign(x, y) returns a value with the magnitude of x and the sign of y.
x supplies the size. y supplies the sign. Both are double.
A double with |x| and the sign taken from y.
Include <math.h>. On GCC, link with -lm.
No. Multiplying flips the current sign. copysign sets the sign from y, even when x is already negative.
Next: cos()
Compute the cosine of a floating-point angle in radians.
cos() tutorial → CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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