|
| 1 | +--- |
| 2 | +Title: '.SinCos()' |
| 3 | +Description: 'Returns both the sine and cosine of a given angle.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Arithmetic' |
| 9 | + - 'Functions' |
| 10 | + - 'Methods' |
| 11 | + - 'Numbers' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-sharp' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`Math.SinCos()`** method returns both the sine and cosine of a specified angle (in radians) as a tuple. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +Math.SinCos(angle); |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `angle`: A double-precision floating-point number representing an angle in radians. |
| 28 | + |
| 29 | +**Return value:** |
| 30 | + |
| 31 | +The method returns a tuple containing both the sine and cosine of the `angle` as `double` values. If the value of `angle` equals `NaN`, `NegativeInfinity`, or `PositiveInfinity`, the method returns `NaN` for both values. |
| 32 | + |
| 33 | +> **Note:** This method is more efficient than calling `Math.Sin()` and `Math.Cos()` separately when both values are needed. |
| 34 | +
|
| 35 | +## Example 1 |
| 36 | + |
| 37 | +In this example, the code converts 45 degrees to radians and uses the `Math.SinCos()` method to return both the sine and cosine of that angle: |
| 38 | + |
| 39 | +```cs |
| 40 | +using System; |
| 41 | + |
| 42 | +public class Example { |
| 43 | + public static void Main(string[] args) { |
| 44 | + double degrees = 45; |
| 45 | + double radians = degrees * Math.PI/180; |
| 46 | + |
| 47 | + var (sine, cosine) = Math.SinCos(radians); |
| 48 | + |
| 49 | + Console.WriteLine("The sine of " + degrees + " degrees is: " + sine); |
| 50 | + Console.WriteLine("The cosine of " + degrees + " degrees is: " + cosine); |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +The example will result in the following output: |
| 56 | + |
| 57 | +```shell |
| 58 | +The sine of 45 degrees is: 0.7071067811865476 |
| 59 | +The cosine of 45 degrees is: 0.7071067811865476 |
| 60 | +``` |
| 61 | + |
| 62 | +## Example 2 |
| 63 | + |
| 64 | +In this example, both sine and cosine values of a 30° angle are calculated using `Math.SinCos()`: |
| 65 | + |
| 66 | +```cs |
| 67 | +using System; |
| 68 | + |
| 69 | +public class Example { |
| 70 | + |
| 71 | + public static void Main(string[] args) { |
| 72 | + // Angle in degrees |
| 73 | + double angle = 30; |
| 74 | + |
| 75 | + var (sine, cosine) = Math.SinCos(angle * Math.PI/180); |
| 76 | + |
| 77 | + Console.WriteLine("The sine of " + angle + " degrees is: " + sine); |
| 78 | + Console.WriteLine("The cosine of " + angle + " degrees is: " + cosine); |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +The output of this code is: |
| 84 | + |
| 85 | +```shell |
| 86 | +The sine of 30 degrees is: 0.5 |
| 87 | +The cosine of 30 degrees is: 0.8660254037844386 |
| 88 | +``` |
| 89 | + |
| 90 | +> **Note:** The `Math.SinCos()` method is supported starting from .NET 6 and later. It is not available in earlier .NET versions. |
0 commit comments