C# Math CopySign() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.CopySign(x, y) takes the size of x and gives it the sign of y. Math.CopySign(5, -1) is -5: the size 5, and the sign minus from -1. Math.CopySign(-5, 1) is 5.

Imagine two cards. One card says how big (the size), and the other card says which way (plus or minus). CopySign combines the size from the first card with the direction from the second. It reads the real sign bit of a double, so it even notices the difference between 0.0 and -0.0 (negative zero), which Sign() cannot. It is a small advanced tool, used when you want to point a value in a chosen direction.

This is a static member of the static class System.Math. You call it with the class name, such as Math.CopySign(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. Math.CopySign was added in .NET Core 3.0. It is not in classic .NET Framework.

Syntax

Pass the number that provides the size first, then the number that provides the sign. Both are double values.

C#
double a = Math.CopySign(5, -1);     // -5
double b = Math.CopySign(-5, 1);     // 5
double c = Math.CopySign(3, -0.0);   // -3 (negative zero counts as negative)
CallWhat it does
Math.CopySign(5, -1)Size 5, sign from -1: returns -5.
Math.CopySign(-5, 1)Size 5, sign from 1: returns 5.
Math.CopySign(3, -0.0)Negative zero counts as a negative sign: returns -3.
Math.CopySign(double.PositiveInfinity, -1)Returns -Infinity.

Only the sign of the second number is used. Its size does not matter at all. A zero as the second number is treated as positive for 0.0 and negative for -0.0. The result is the same as Math.Abs(x) with the sign of y put on, but without any special case for zero. NaN keeps its own sign bit rules, so avoid relying on the sign of a NaN.

Library declarations
C#
public static double CopySign(double x, double y);

Parameters

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

Return Value

Return ValueDescription
doubleA number with the size of x and the sign of y.

C# Examples

Example 1 — Combine a Size with a Sign

Give the size of one number the sign of another, in all four combinations.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.CopySign(5, -1));
        Console.WriteLine(Math.CopySign(-5, 1));
        Console.WriteLine(Math.CopySign(-5, -2));
        Console.WriteLine(Math.CopySign(7.0, 3));
    }
}

How It Works

  1. Positive size, negative sign. The size of 5 with the sign of -1 gives -5.
  2. Negative size, positive sign. The size of -5 is 5, and the sign of 1 is plus, so the answer is 5. The original sign of x is thrown away.
  3. Both negative. The size of -5 with the sign of -2 gives -5. Only whether y is negative matters, not how big it is.
  4. Both positive. The size 7 with a positive sign stays 7.

Example 2 — Negative Zero and CopySign

See that -0.0 carries a minus sign, that Math.Sign cannot see it, and that CopySign can.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.CopySign(3, -0.0));
        Console.WriteLine(Math.CopySign(3, 0.0));
        Console.WriteLine(double.IsNegative(-0.0));
        Console.WriteLine(Math.Sign(-0.0));
    }
}

How It Works

  1. Negative zero. A double can store -0.0, a zero with a minus sign. It is equal to 0.0 in comparisons, but its sign bit is set.
  2. CopySign sees it. The sign of -0.0 is minus, so CopySign(3, -0.0) is -3. With plain 0.0 you get 3.
  3. IsNegative agrees. double.IsNegative(-0.0) is True. It reads the sign bit directly.
  4. Math.Sign cannot. Math.Sign(-0.0) is 0, because it treats both zeros as zero. That is the gap CopySign fills.

Example 3 — Push in a Chosen Direction

Write a helper that uses the size of an amount and the direction of another number, and see why Abs times Sign can fail.

C#
using System;

class Program {
    static double Push(double amount, double direction) {
        return Math.CopySign(Math.Abs(amount), direction);
    }

    static void Main() {
        Console.WriteLine(Push(10, -3));
        Console.WriteLine(Push(10, 8));
        Console.WriteLine(Push(-10, 8));
        Console.WriteLine(Math.CopySign(double.PositiveInfinity, -1));

        Console.WriteLine(Math.Abs(7) * Math.Sign(0));
        Console.WriteLine(Math.CopySign(7, 0));
    }
}

How It Works

  1. The helper. Push takes the size of amount with Math.Abs, then gives it the sign of direction. So the result always points the way direction says.
  2. -3, 8, and a negative amount. A push of 10 in the direction of -3 is -10. In the direction of 8 it is 10. Even an amount of -10 becomes 10 for a positive direction.
  3. Infinity. The size of positive infinity with a minus sign is -Infinity. No special case is needed.
  4. The Abs times Sign trap. Math.Abs(7) * Math.Sign(0) is 7 times 0, which is 0: the size is lost. Math.CopySign(7, 0) keeps the size and returns 7.

Frequently Asked Questions

It returns the size (magnitude) of the first number with the sign of the second number. Math.CopySign(5, -1) is -5.
It was added in .NET Core 3.0, so it is in modern .NET. It is not in classic .NET Framework.
Abs(x) * Sign(y) gives 0 when y is 0 and throws for NaN. CopySign always keeps the size of x, and it can even tell 0.0 from -0.0.
A double can hold -0.0, a zero with its sign bit set. It equals 0.0 in comparisons, but double.IsNegative(-0.0) is true and Math.CopySign(3, -0.0) is -3.
No. Math is a static class, so you call Math.CopySign(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Cos()

Return the cosine of an angle given 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.

12 people found this page helpful