Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Math.Sin() Method in C#
The Math.Sin() method in C# is used to return the sine of a specified angle. The method accepts an angle in radians and returns a double value representing the sine of that angle.
Syntax
Following is the syntax for the Math.Sin() method −
public static double Sin(double val);
Parameters
-
val − A double value representing an angle in radians.
Return Value
The method returns a double value representing the sine of the specified angle. The return value ranges from -1 to 1. For special cases −
-
If the parameter is
NaNor infinity, the method returnsNaN. -
If the parameter is positive or negative zero, the method returns positive or negative zero respectively.
Using Math.Sin() with Degree to Radian Conversion
Since Math.Sin() expects radians, you must convert degrees to radians using the formula: radians = degrees × ? / 180.
Example
using System;
public class Demo {
public static void Main() {
double val1 = 30.0;
double radians = (val1 * Math.PI) / 180;
Console.WriteLine("Sin(" + val1 + "°) = " + Math.Sin(radians));
double val2 = 90.0;
radians = (val2 * Math.PI) / 180;
Console.WriteLine("Sin(" + val2 + "°) = " + Math.Sin(radians));
}
}
The output of the above code is −
Sin(30°) = 0.5 Sin(90°) = 1
Using Math.Sin() with Special Values
Example
using System;
public class Demo {
public static void Main() {
double val1 = 45.0;
double val2 = Double.PositiveInfinity;
double val3 = Double.NaN;
double val4 = 0.0;
Console.WriteLine("Sin(45°) = " + Math.Sin((val1 * Math.PI) / 180));
Console.WriteLine("Sin(PositiveInfinity) = " + Math.Sin(val2));
Console.WriteLine("Sin(NaN) = " + Math.Sin(val3));
Console.WriteLine("Sin(0) = " + Math.Sin(val4));
}
}
The output of the above code is −
Sin(45°) = 0.707106781186547 Sin(PositiveInfinity) = NaN Sin(NaN) = NaN Sin(0) = 0
Common Sine Values
| Angle (Degrees) | Angle (Radians) | Sin Value |
|---|---|---|
| 0° | 0 | 0 |
| 30° | ?/6 | 0.5 |
| 45° | ?/4 | ?2/2 ? 0.707 |
| 60° | ?/3 | ?3/2 ? 0.866 |
| 90° | ?/2 | 1 |
Conclusion
The Math.Sin() method calculates the sine of an angle measured in radians. Remember to convert degrees to radians when working with degree measurements, and handle special cases like infinity and NaN appropriately in your applications.
