Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
copysign
math
communitycreator

What is Math.CopySign() in C#?

Arslan Bajwa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

RELATED TAGS

c#
copysign
math
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring