📜  查找 Sin(x) 值的 C# 程序

📅  最后修改于: 2022-05-13 01:54:36.235000             🧑  作者: Mango

查找 Sin(x) 值的 C# 程序

Sin(x) 也称为正弦。它是角度的函数。在直角三角形中,垂线长度与斜边长度之比称为角的正弦值。

sin θ = perpendicular / hypotenuse

下面给出了一些comman角的正弦值,

  1. 罪 0 ° = 0
  2. 罪 30 ° = 1 / 2
  3. 正弦 45° = 1 / √2
  4. 罪 60 ° = √3 / 2
  5. 罪 90 ° = 1

本文重点介绍如何在 C# 中计算角度的正弦值。

方法一

我们可以使用内置的 sin() 方法计算角度的正弦值。此方法在 Math 类下定义,是系统命名空间的一部分。 Math 类非常有用,因为它提供了常量和一些三角函数、对数等的静态方法。

句法:

范围:

  • 角度:一个双精度值(以弧度为单位的角度)

返回类型:

  • 双倍:如果“角度”是双倍的
  • NaN:如果“角度”等于 NaN、NegativeInfinity 或 PositiveInfinity

示例 1:

C#
// C# program to illustrate how we can 
// calculate the value of sin(x)
// using Sin() method
using System.IO;
using System;
  
class GFG{
      
static void Main()
{
      
    // Angle in degree 
    double angleInDegree1 = 0;
      
    // Converting angle in radian
    // since Math.sin() method accepts
    // angle in radian
    double angleInRadian1 = (angleInDegree1 * (Math.PI)) / 180;
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angleInDegree1, Math.Sin(angleInRadian1));
                        
    // Angle in degree 
    double angleInDegree2 = 45;
      
    // Converting angle in radian
    // since Math.sin() method accepts
    // angle in radian
    double angleInRadian2 = (angleInDegree2 * (Math.PI)) / 180;
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angleInDegree2, Math.Sin(angleInRadian2));
      
    // Angle in degree 
    double angleInDegree3 = 90;
         
    // Converting angle in radian
    // since Math.sin() method accepts
    // angle in radian
    double angleInRadian3 = (angleInDegree3 * (Math.PI)) / 180;
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angleInDegree3, Math.Sin(angleInRadian3));
      
    // Angle in degree 
    double angleInDegree4 = 135;
         
    // Converting angle in radian
    // since Math.sin() method accepts
    // angle in radian
    double angleInRadian4 = (angleInDegree4 * (Math.PI)) / 180;
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angleInDegree4, Math.Sin(angleInRadian4));
}
}


C#
// C# program to illustrate how we can 
// calculate the value of sin(x)
// using Sin() method
using System;
  
class GFG{
  
static public void Main()
{
      
    // Angle in radian
    double angle1 = Double.NegativeInfinity;
      
    // Angle in radian
    double angle2 = Double.PositiveInfinity;
      
    // Angle in radian
    double angle3 = Double.NaN;
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angle1, Math.Sin(angle1));
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angle2, Math.Sin(angle2));
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angle3, Math.Sin(angle3));
}
}


C#
// C# program to illustrate how we can 
// calculate the value of sin(x)
// using Maclaurin's method
using System;
  
class GFG{
  
static double findSinX(int angleInDegree, int terms)
{
      
    // Converting angle in degree into radian 
    double current = Math.PI * angleInDegree / 180f;
      
    // Declaring variable to calculate final answer
    double answer = current;
    double temp = current;
      
    // Loop till number of steps provided by the user
    for(int i = 1; i <= terms; i++)
    {
          
        // Updating temp and answer accordingly
        temp = ((-temp) * current * current) / 
                    ((2 * i) * (2 * i + 1));
        answer = answer + temp;
    }
  
    // Return the final answer
    return answer;
}
  
// Driver code
static public void Main()
{
      
    // Angle in degree
    int angleInDegree1 = 45;
    
    // Number of steps 
    int terms1 = 10;
  
    // Calling function to calculate sine of angle
    double answer1 = findSinX(angleInDegree1, terms1);
  
    // Print the final answer
    Console.WriteLine("The value of sin({0}) = {1}", 
                      angleInDegree1, answer1);
      
    // Angle in degree
    int angleInDegree2 = 90;
      
    // Number of steps
    int terms2 = 20;
  
    // Calling function to calculate sine of angle      
    double result2 = findSinX(angleInDegree2, terms2);
  
    // Print the final answer
    Console.WriteLine("The value of sin({0}) = {1}", 
                      angleInDegree2, result2);
      
    // Angle in degree
    int angleInDegree3 = 135;
  
    // Number of steps
    int terms3 = 30;
  
    // Calling function to calculate sine of angle          
    double result3 = findSinX(angleInDegree3, terms3);
  
    // Print the final answer
    Console.WriteLine("The value of sin({0}) = {1}", 
                      angleInDegree3, result3);
      
    // Angle in degree
    int angleInDegree4 = 180;
    
    // Number of steps
    int terms4 = 40;
      
    // Calling function to calculate sine of angle
    double result4 = findSinX(angleInDegree4, terms4);
  
    // Print the final answer
    Console.WriteLine("The value of sin({0}) = {1}", 
                      angleInDegree4, result4);
}
}


输出
The value of sin(0) = 0 
The value of sin(45) = 0.707106781186547 
The value of sin(90) = 1 
The value of sin(135) = 0.707106781186548 

示例 2:

C#

// C# program to illustrate how we can 
// calculate the value of sin(x)
// using Sin() method
using System;
  
class GFG{
  
static public void Main()
{
      
    // Angle in radian
    double angle1 = Double.NegativeInfinity;
      
    // Angle in radian
    double angle2 = Double.PositiveInfinity;
      
    // Angle in radian
    double angle3 = Double.NaN;
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angle1, Math.Sin(angle1));
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angle2, Math.Sin(angle2));
      
    // Using Math.Sin() method to calculate value of sine
    Console.WriteLine("The value of sin({0}) = {1} ", 
                      angle3, Math.Sin(angle3));
}
}

输出

Sine of angle1: NaN
Sine of angle2: NaN
Sine of angle3: NaN

方法二

我们可以使用 Maclaurin 展开计算角度的正弦值。所以 sin(x) 的麦克劳林级数展开为:

sin(x) = x - x3 / 3! + x5 / 5! - x7 / 7! + ....

按照下面给出的步骤找到 sin(x) 的值:

  1. 初始化一个变量angleInDegree ,它存储要计算的角度(以度为单位)。
  2. 初始化另一个变量terms ,它存储我们可以近似 sin(x) 值的项数。
  3. 声明一个全局函数findSinx
  4. 声明一个可变电流。它以弧度存储角度。
  5. current初始化一个变量answer 。它将存储我们的最终答案。
  6. current初始化另一个变量temp
  7. i = 1 迭代到i = terms 。在每一步将 temp 更新为 temp 为 ((-temp) * current * current) / ((2 * i) * (2 * i + 1)) 并回答为 answer + temp。
  8. 最终,从findSinX函数返回答案。
  9. 打印答案。

该公式可以计算 x 的所有实数值的正弦值。

例子:

C#

// C# program to illustrate how we can 
// calculate the value of sin(x)
// using Maclaurin's method
using System;
  
class GFG{
  
static double findSinX(int angleInDegree, int terms)
{
      
    // Converting angle in degree into radian 
    double current = Math.PI * angleInDegree / 180f;
      
    // Declaring variable to calculate final answer
    double answer = current;
    double temp = current;
      
    // Loop till number of steps provided by the user
    for(int i = 1; i <= terms; i++)
    {
          
        // Updating temp and answer accordingly
        temp = ((-temp) * current * current) / 
                    ((2 * i) * (2 * i + 1));
        answer = answer + temp;
    }
  
    // Return the final answer
    return answer;
}
  
// Driver code
static public void Main()
{
      
    // Angle in degree
    int angleInDegree1 = 45;
    
    // Number of steps 
    int terms1 = 10;
  
    // Calling function to calculate sine of angle
    double answer1 = findSinX(angleInDegree1, terms1);
  
    // Print the final answer
    Console.WriteLine("The value of sin({0}) = {1}", 
                      angleInDegree1, answer1);
      
    // Angle in degree
    int angleInDegree2 = 90;
      
    // Number of steps
    int terms2 = 20;
  
    // Calling function to calculate sine of angle      
    double result2 = findSinX(angleInDegree2, terms2);
  
    // Print the final answer
    Console.WriteLine("The value of sin({0}) = {1}", 
                      angleInDegree2, result2);
      
    // Angle in degree
    int angleInDegree3 = 135;
  
    // Number of steps
    int terms3 = 30;
  
    // Calling function to calculate sine of angle          
    double result3 = findSinX(angleInDegree3, terms3);
  
    // Print the final answer
    Console.WriteLine("The value of sin({0}) = {1}", 
                      angleInDegree3, result3);
      
    // Angle in degree
    int angleInDegree4 = 180;
    
    // Number of steps
    int terms4 = 40;
      
    // Calling function to calculate sine of angle
    double result4 = findSinX(angleInDegree4, terms4);
  
    // Print the final answer
    Console.WriteLine("The value of sin({0}) = {1}", 
                      angleInDegree4, result4);
}
}
输出
The value of sin(45) = 0.707106781186547
The value of sin(90) = 1
The value of sin(135) = 0.707106781186548
The value of sin(180) = 2.34898825287367E-16