Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Hàm lượng giác (Trigonometric Functions) trong C#

Lớp System.Math cung cấp các hàm lượng giác cơ bản. Tất cả góc đều tính bằng radian (không phải độ). Chuyển đổi: radian = degree * Math.PI / 180.

Các phương thức chính

Phương thứcMô tảĐ역역 (Domain)Giá trị trả về (Range)
Sin(double)Sineℝ[-1, 1]
Cos(double)Cosineℝ[-1, 1]
Tan(double)Tangentℝ \ {π/2 + kπ}ℝ
Asin(double)Arcsine (nghịch sine)[-1, 1][-π/2, π/2]
Acos(double)Arccosine[-1, 1][0, π]
Atan(double)Arctangentℝ(-π/2, π/2)
Atan2(y, x)Arctangent của y/x (xác định góc đúng quadrant)ℝ×ℝ[-π, π]
Sinh/Cosh/TanhHàm hyperbolicℝℝ / [1,∞) / (-1,1)

Ví dụ cơ bản

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("=== Hàm lượng giác cơ bản ===");
        Console.WriteLine($"Sin(0)     = {Math.Sin(0)}");           // 0
        Console.WriteLine($"Cos(0)     = {Math.Cos(0)}");           // 1
        Console.WriteLine($"Tan(0)     = {Math.Tan(0)}");           // 0
        Console.WriteLine($"Sin(PI/2)  = {Math.Sin(Math.PI/2)}");   // 1
        Console.WriteLine($"Cos(PI)    = {Math.Cos(Math.PI)}");     // -1
        
        Console.WriteLine("\n=== Hàm nghịch (Arc) ===");
        Console.WriteLine($"Asin(0)    = {Math.Asin(0)}");           // 0
        Console.WriteLine($"Acos(0)    = {Math.Acos(0)}");           // π/2 ≈ 1.5708
        Console.WriteLine($"Atan(1)    = {Math.Atan(1)}");           // π/4 ≈ 0.7854
        Console.WriteLine($"Atan2(1,1) = {Math.Atan2(1, 1)}");       // π/4
        
        Console.WriteLine("\n=== Chuyển đổi Degree ↔ Radian ===");
        double deg = 45;
        double rad = deg * Math.PI / 180;
        Console.WriteLine($"{deg}° = {rad:F4} rad");
        Console.WriteLine($"Sin({deg}°) = {Math.Sin(rad):F4}");
        
        // Helper methods
        double deg2 = Math.Asin(0.5) * 180 / Math.PI; // 30°
        Console.WriteLine($"Asin(0.5) = {deg2:F1}°");
    }
}

Kết quả

=== Hàm lượng giác cơ bản ===
Sin(0)     = 0
Cos(0)     = 1
Tan(0)     = 0
Sin(PI/2)  = 1
Cos(PI)    = -1

=== Hàm nghịch (Arc) ===
Asin(0)    = 0
Acos(0)    = 1.5707963267949
Atan(1)    = 0.785398163397448
Atan2(1,1) = 0.785398163397448

=== Chuyển đổi Degree ↔ Radian ===
45° = 0.7854 rad
Sin(45°) = 0.7071
Asin(0.5) = 30.0°

Hàm hỗ trợ hữu ích

public static class MathExtensions
{
    public static double DegToRad(double deg) => deg * Math.PI / 180.0;
    public static double RadToDeg(double rad) => rad * 180.0 / Math.PI;
    
    public static double SinDeg(double deg) => Math.Sin(DegToRad(deg));
    public static double CosDeg(double deg) => Math.Cos(DegToRad(deg));
    public static double TanDeg(double deg) => Math.Tan(DegToRad(deg));
    
    // Atan2 trả về góc [-180, 180] độ
    public static double Atan2Deg(double y, double x) => RadToDeg(Math.Atan2(y, x));
}

Lưu ý độ chính xác

  • Kết quả là double (64-bit), có sai số làm tròn (~1e-15).
  • Với độ chính xác cao hơn → dùng decimal + thuật toán tùy chỉnh (khó).
  • Atan2(y, x) tốt hơn Atan(y/x): xử lý x=0, xác định đúng quadrant.