Skip to content

Commit dff1797

Browse files
[Term Entry] C# Math-functions: CopySign()
* Add term entry: C# Math.CopySign() (#7950) * minor content fixes * Update copysign.md * Minor changes ---------
1 parent bdbcb90 commit dff1797

File tree

1 file changed

+86
-0
lines changed
  • content/c-sharp/concepts/math-functions/terms/copysign

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
Title: 'CopySign()'
3+
Description: 'Returns a value with the magnitude of the first operand and the sign of the second operand.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Web Development'
7+
Tags:
8+
- 'Functions'
9+
- 'Math'
10+
- 'Methods'
11+
- 'Numbers'
12+
CatalogContent:
13+
- 'learn-c-sharp'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`Math.CopySign()`** [method](https://www.codecademy.com/resources/docs/c-sharp/methods) 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.
18+
19+
## Syntax
20+
21+
```pseudo
22+
Math.CopySign(double x, double y)
23+
```
24+
25+
**Parameters:**
26+
27+
- `x` (double): The value whose magnitude (absolute value) will be used.
28+
- `y` (double): The value whose sign will be applied to the result.
29+
30+
**Return value:**
31+
32+
- Returns a `double` value with the magnitude of `x` and the sign of `y`.
33+
- If `x` is `NaN`, the result is `NaN`.
34+
- If `y` is `NaN`, the result is treated as if `y` were positive.
35+
36+
## Example: Adjusting Velocity Based on Direction
37+
38+
In this example, `Math.CopySign()` is used to adjust a projectile’s velocity so that its direction matches a steering input:
39+
40+
```cs
41+
using System;
42+
43+
public class CopySignExample
44+
{
45+
public static void Main()
46+
{
47+
double projectileSpeed = 18.75;
48+
double steeringDirection = -0.5; // Negative indicates reverse direction
49+
50+
double adjustedVelocity = Math.CopySign(projectileSpeed, steeringDirection);
51+
52+
Console.WriteLine($"Adjusted velocity: {adjustedVelocity}");
53+
}
54+
}
55+
```
56+
57+
This program outputs:
58+
59+
```shell
60+
Adjusted velocity: -18.75
61+
```
62+
63+
## Codebyte Example: Standardizing Numeric Sign Alignment
64+
65+
In this example, `Math.CopySign()` ensures a set of magnitudes follow given directional signs, even when the sign is `NaN`:
66+
67+
```codebyte/csharp
68+
using System;
69+
70+
public class Program
71+
{
72+
public static void Main()
73+
{
74+
double[] magnitudes = { 3.5, 12.0, 6.25 };
75+
double[] directionSamples = { -1.0, 0.0, double.NaN };
76+
77+
Console.WriteLine("Magnitude\tDirection\tResult");
78+
79+
for (int i = 0; i < magnitudes.Length; i++)
80+
{
81+
double result = Math.CopySign(magnitudes[i], directionSamples[i]);
82+
Console.WriteLine($"{magnitudes[i],-9}\t{directionSamples[i],-9}\t{result}");
83+
}
84+
}
85+
}
86+
```

0 commit comments

Comments
 (0)