C# Tutorial C# Advanced C# References

C# Math - CopySign() Method



The C# CopySign() method returns a number with magnitude of first argument and sign of second argument.

Syntax

public static double CopySign (double magnitude, double sign);

Parameters

magnitude Specify a value providing the magnitude of the result.
sign Specify a value providing the sign of the result.

Return Value

Returns a number with magnitude of first argument and sign of second argument.

Example:

In the example below, CopySign() method returns a number with magnitude of first argument and sign of second argument.

using System;

class MyProgram {
  static void Main(string[] args) {
    Console.WriteLine("Math.CopySign(48.45, 3) = "
                      + Math.CopySign(48.45, 3));
    Console.WriteLine("Math.CopySign(48.45, -3) = "
                      + Math.CopySign(48.45, -3));
    Console.WriteLine("Math.CopySign(-48.45, 3) = "
                      + Math.CopySign(-48.45, 3));
    Console.WriteLine("Math.CopySign(-48.45, -3) = "
                      + Math.CopySign(-48.45, -3));
    Console.WriteLine("Math.CopySign(Double.NegativeInfinity, 2) = "
                      + Math.CopySign(Double.NegativeInfinity, 2));
    Console.WriteLine("Math.CopySign(Double.PositiveInfinity, -3) = "
                      + Math.CopySign(Double.PositiveInfinity, -3));
  }
}

The output of the above code will be:

Math.CopySign(48.45, 3) = 48.45
Math.CopySign(48.45, -3) = -48.45
Math.CopySign(-48.45, 3) = 48.45
Math.CopySign(-48.45, -3) = -48.45
Math.CopySign(Double.NegativeInfinity, 2) = Infinity
Math.CopySign(Double.PositiveInfinity, -3) = -Infinity

❮ C# Math Methods

5