From b5e07e04fccbff8e786aa6449adec70f01b86c77 Mon Sep 17 00:00:00 2001 From: jaideepkathiresan Date: Thu, 6 Nov 2025 16:00:05 +0530 Subject: [PATCH 1/3] Add term entry: C# Math.CopySign() (#7950) --- .../math-functions/terms/copysign/copysign.md | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 content/c-sharp/concepts/math-functions/terms/copysign/copysign.md diff --git a/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md b/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md new file mode 100644 index 00000000000..1f81f3af521 --- /dev/null +++ b/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md @@ -0,0 +1,84 @@ +--- +Title: 'CopySign()' +Description: 'Returns a value with the magnitude of one number and the sign of another.' +Subjects: + - 'Computer Science' + - 'Web Development' +Tags: + - 'Functions' + - 'Math' + - 'Methods' + - 'Numbers' +CatalogContent: + - 'learn-c-sharp' + - 'paths/computer-science' +--- + +The C# **`Math.CopySign()`** method returns a value with the magnitude of the first argument and the sign of the second argument. It is helpful when aligning numeric signs while preserving absolute values. + +## Syntax of C# `Math.CopySign()` + +```pseudo +Math.CopySign(magnitude, sign); +``` + +**Parameters:** + +- `magnitude`: A `double` or `float` whose absolute value will be used in the result. +- `sign`: A `double` or `float` whose sign will be applied to the result. + +**Return value:** + +- Returns a `double` or `float` that combines the magnitude of `magnitude` with the sign of `sign`. +- If `magnitude` is `NaN`, the result is `NaN`. +- If `sign` is `NaN`, the result preserves the magnitude of `magnitude` with a positive sign. + +## Example + +The following example keeps a projectile speed positive while matching the direction of a steering input: + +```cs +using System; + +public class CopySignExample +{ + public static void Main() + { + double projectileSpeed = 18.75; + double steeringDirection = -0.5; // Negative indicates reverse direction + + double adjustedVelocity = Math.CopySign(projectileSpeed, steeringDirection); + + Console.WriteLine($"Adjusted velocity: {adjustedVelocity}"); + } +} +``` + +This program outputs: + +```shell +Adjusted velocity: -18.75 +``` + +## Codebyte Example + +```codebyte/csharp +using System; + +public class Program +{ + public static void Main() + { + double[] magnitudes = { 3.5, 12.0, 6.25 }; + double[] directionSamples = { -1.0, 0.0, double.NaN }; + + Console.WriteLine("Magnitude\tDirection\tResult"); + + for (int i = 0; i < magnitudes.Length; i++) + { + double result = Math.CopySign(magnitudes[i], directionSamples[i]); + Console.WriteLine($"{magnitudes[i],-9}\t{directionSamples[i],-9}\t{result}"); + } + } +} +``` From 3a45e2d551b799fd98ece0f038affe68c52cf123 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 7 Nov 2025 11:39:28 +0530 Subject: [PATCH 2/3] minor content fixes --- .../math-functions/terms/copysign/copysign.md | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md b/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md index 1f81f3af521..02f43d5b768 100644 --- a/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md +++ b/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md @@ -1,6 +1,6 @@ --- Title: 'CopySign()' -Description: 'Returns a value with the magnitude of one number and the sign of another.' +Description: 'Returns a value with the magnitude of the first operand and the sign of the second operand.' Subjects: - 'Computer Science' - 'Web Development' @@ -14,28 +14,28 @@ CatalogContent: - 'paths/computer-science' --- -The C# **`Math.CopySign()`** method returns a value with the magnitude of the first argument and the sign of the second argument. It is helpful when aligning numeric signs while preserving absolute values. +The **`Math.CopySign()`** method in C# returns a value that combines the magnitude of the first argument with the sign of the second. It’s useful for matching the sign of one number to another while keeping the original magnitude. ## Syntax of C# `Math.CopySign()` ```pseudo -Math.CopySign(magnitude, sign); +Math.CopySign(double x, double y) ``` **Parameters:** -- `magnitude`: A `double` or `float` whose absolute value will be used in the result. -- `sign`: A `double` or `float` whose sign will be applied to the result. +- `x` (double): The value whose magnitude (absolute value) will be used. +- `y` (double): The value whose sign will be applied to the result. **Return value:** -- Returns a `double` or `float` that combines the magnitude of `magnitude` with the sign of `sign`. -- If `magnitude` is `NaN`, the result is `NaN`. -- If `sign` is `NaN`, the result preserves the magnitude of `magnitude` with a positive sign. +- Returns a `double` value with the magnitude of `x` and the sign of `y`. +- If `x` is `NaN`, the result is `NaN`. +- If `y` is `NaN`, the result is treated as if `y` were positive. -## Example +## Example: Adjusting velocity based on direction -The following example keeps a projectile speed positive while matching the direction of a steering input: +In this example, `Math.CopySign()` is used to adjust a projectile’s velocity so that its direction matches a steering input: ```cs using System; @@ -60,7 +60,9 @@ This program outputs: Adjusted velocity: -18.75 ``` -## Codebyte Example +## Codebyte Example: Standardizing numeric sign alignment + +In this example, `Math.CopySign()` ensures a set of magnitudes follow given directional signs, even when the sign is `NaN`: ```codebyte/csharp using System; From 8e8c59b9aebc1b6e5e01bae3248bb3590426bdc7 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 7 Nov 2025 11:40:05 +0530 Subject: [PATCH 3/3] Update copysign.md --- .../c-sharp/concepts/math-functions/terms/copysign/copysign.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md b/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md index 02f43d5b768..0396a73ec22 100644 --- a/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md +++ b/content/c-sharp/concepts/math-functions/terms/copysign/copysign.md @@ -16,7 +16,7 @@ CatalogContent: The **`Math.CopySign()`** method in C# returns a value that combines the magnitude of the first argument with the sign of the second. It’s useful for matching the sign of one number to another while keeping the original magnitude. -## Syntax of C# `Math.CopySign()` +## Syntax ```pseudo Math.CopySign(double x, double y)