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
Trigonometric Functions in C#
Trigonometric functions in C# are part of the Math class in the System namespace. These functions enable you to perform mathematical calculations involving angles, including basic trigonometric operations (Sin, Cos, Tan) and their inverse functions (Asin, Acos, Atan).
All angle measurements in C# trigonometric functions are in radians, not degrees. To convert degrees to radians, multiply by ?/180.
Common Trigonometric Functions
| Function | Description | Return Type |
|---|---|---|
| Math.Sin(double) | Returns the sine of the specified angle | double |
| Math.Cos(double) | Returns the cosine of the specified angle | double |
| Math.Tan(double) | Returns the tangent of the specified angle | double |
| Math.Asin(double) | Returns the inverse sine (arcsine) in radians | double |
| Math.Acos(double) | Returns the inverse cosine (arccosine) in radians | double |
| Math.Atan(double) | Returns the inverse tangent (arctangent) in radians | double |
Using Basic Trigonometric Functions
Example
using System;
class Program {
static void Main() {
double angle = Math.PI / 4; // 45 degrees in radians
Console.WriteLine("Angle: " + angle + " radians (45 degrees)");
Console.WriteLine("Sin(45°): " + Math.Sin(angle));
Console.WriteLine("Cos(45°): " + Math.Cos(angle));
Console.WriteLine("Tan(45°): " + Math.Tan(angle));
Console.WriteLine();
Console.WriteLine("Special angles:");
Console.WriteLine("Sin(?/2): " + Math.Sin(Math.PI / 2));
Console.WriteLine("Cos(0): " + Math.Cos(0));
Console.WriteLine("Tan(?/4): " + Math.Tan(Math.PI / 4));
}
}
The output of the above code is −
Angle: 0.7853981633974483 radians (45 degrees)
Sin(45°): 0.7071067811865476
Cos(45°): 0.7071067811865476
Tan(45°): 0.9999999999999999
Special angles:
Sin(?/2): 1
Cos(0): 1
Tan(?/4): 0.9999999999999999
Using Inverse Trigonometric Functions
Example
using System;
class Program {
static void Main() {
Console.WriteLine("Inverse trigonometric functions:");
Console.WriteLine("Acos(0): " + Math.Acos(0) + " radians");
Console.WriteLine("Asin(0.2): " + Math.Asin(0.2) + " radians");
Console.WriteLine("Atan(-5): " + Math.Atan(-5) + " radians");
Console.WriteLine();
Console.WriteLine("Converting radians to degrees:");
double acosResult = Math.Acos(0);
double degrees = acosResult * (180 / Math.PI);
Console.WriteLine("Acos(0) in degrees: " + degrees + "°");
}
}
The output of the above code is −
Inverse trigonometric functions: Acos(0): 1.5707963267948966 radians Asin(0.2): 0.20135792079033077 radians Atan(-5): -1.3734007669450158 radians Converting radians to degrees: Acos(0) in degrees: 90°
Practical Example with Degree-Radian Conversion
Example
using System;
class Program {
static double DegreesToRadians(double degrees) {
return degrees * (Math.PI / 180);
}
static double RadiansToDegrees(double radians) {
return radians * (180 / Math.PI);
}
static void Main() {
double angleInDegrees = 30;
double angleInRadians = DegreesToRadians(angleInDegrees);
Console.WriteLine("Working with " + angleInDegrees + " degrees:");
Console.WriteLine("In radians: " + angleInRadians);
Console.WriteLine("Sin(30°): " + Math.Sin(angleInRadians));
Console.WriteLine("Cos(30°): " + Math.Cos(angleInRadians));
Console.WriteLine("Tan(30°): " + Math.Tan(angleInRadians));
Console.WriteLine();
double result = Math.Asin(0.5);
Console.WriteLine("Asin(0.5) = " + result + " radians");
Console.WriteLine("Asin(0.5) = " + RadiansToDegrees(result) + "°");
}
}
The output of the above code is −
Working with 30 degrees: In radians: 0.5235987755982988 Sin(30°): 0.49999999999999994 Cos(30°): 0.8660254037844387 Tan(30°): 0.5773502691896257 Asin(0.5) = 0.5235987755982989 radians Asin(0.5) = 30.000000000000004°
Conclusion
C# trigonometric functions in the Math class provide essential mathematical operations for angles. Remember that all functions work with radians, so convert from degrees when necessary. These functions are commonly used in graphics programming, engineering calculations, and mathematical applications.
