What is Math.CopySign() in C#?

C# has a built-in Math class which provides useful mathematical functions and operations. The class has the CopySign() function, which is used to return a number with the magnitude of the first parameter and the sign of the second parameter.

Syntax

public static double CopySign (double param1, double param2);

Parameters

  • param1: This parameter is of the Double type and represents the input value whose magnitude is to be used in CopySign().
  • param2: This parameter is of the Double type and represents the input value whose sign is to be used in CopySign().

Return value

  • Double: This value returns a Double type number with magnitude of param1 and sign of param2.

Example

using System;
class Educative
{
static void Main()
{
double result = Math.CopySign(2,3);
System.Console.WriteLine("CopySign(2,3) = "+ result);
double result1 = Math.CopySign(1, -4);
System.Console.WriteLine("CopySign(1, -4) = "+ result1);
double result2 = Math.CopySign(-2, 1);
System.Console.WriteLine("CopySign(-2, 1) = "+ result2);
}
}

Output

  • CopySign ( 2 , 3 ) = 2
  • CopySign ( 1 , -4 ) = -1
  • CopySign ( -2 , 1 ) = 2

Free Resources