📜  C#|数学类字段及示例

📅  最后修改于: 2021-05-29 16:52:59             🧑  作者: Mango

在C#中,Math类为对数,三角函数和其他数学函数提供常量和静态方法。数学课有两个字段,如下所示:

  • 数学领域
  • Math.PI字段

数学领域

该字段表示由常数e指定的自然对数底数。

句法:

public const double E

程序1:演示Math类中的Math.E字段

// C# program to demonstrate the
// Constant values of Math.E function
using System;
  
class GFG {
  
    // Main method
    static void Main()
    {
  
        // To find E constant values
        double e = Math.E;
  
        // Print result
        Console.WriteLine("Math.E  = " + e);
    }
}
输出:
Math.E  = 2.71828182845905

程序2:让我们看一个示例,将Math.E与从幂级数中计算出的值进行比较。

// C# program to demonstrate the to
// find the values of Math.E field
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Initialize the data
        double fact = 1.0;
        double PS = 0.0;
  
        // for loop run until the absolute
        // values condition satified
        for (int n = 0; n < 10 && 
             Math.Abs(Math.E - PS) > 1.0E-15; n++) 
        {
              
            // Calculate the factorial
            if (n > 0)
                // update factorial
                fact *= (double)n;
  
            // Calculate the power series.
            PS += 1.0 / fact;
              
            // Display the power series result
            Console.WriteLine(PS);
            Console.WriteLine(Math.E - PS);
        }
    }
}
输出:
1
1.71828182845905
2
0.718281828459045
2.5
0.218281828459045
2.66666666666667
0.0516151617923786
2.70833333333333
0.00994849512571205
2.71666666666667
0.00161516179237875
2.71805555555556
0.000226272903489644
2.71825396825397
2.7860205076724E-05
2.71827876984127
3.05861777505356E-06
2.71828152557319
3.02885852843104E-07

Math.PI字段

它表示圆的周长与其直径的比率,由常数PI(π)指定。

句法:

public const double PI

程序1:演示Math类中的Math.PI字段

// C# program to demonstrate the
// Constant values of Math.PI function
using System;
  
class GFG
{
    // Main method 
    static void Main()
    {
  
        // To find PI constant values
        double pi_value = Math.PI; 
  
        // Print result
        Console.WriteLine("Math.PI = " + pi_value);
    }
} 
输出:
Math.PI = 3.14159265358979

程序2:演示将不同数据类型与Math.PI常数进行乘法运算。

// C# program to demonstrate the
// Constant values of Math.PI function
using System;
  
class GFG
{
    // Main method 
    static void Main()
    {
        // Multiply int ,float,negative number with
        // Math.PI and display  result
        Console.WriteLine(1 * Math.PI );
          
        Console.WriteLine(2 * Math.PI );
          
        Console.WriteLine(1.5 * Math.PI );
          
        Console.WriteLine(-3.10 * Math.PI );
    }
} 
输出:
3.14159265358979
6.28318530717959
4.71238898038469
-9.73893722612836

程序3:让我们演示对PI的访问并找到圆柱体的体积。

// C# program to demonstrate the
// Constant values of Math.PI function
using System;
  
class GFG {
      
    // Main method 
    static void Main()
    {
          
          // input radius value
          double radius = 6;
            
          // input length value
           double length = 9;
             
           // calculate the area using PI
           double area = radius * radius * Math.PI;
             
           // Calculate volume
           double volume = area * length;
             
           // print area and volume of cylinder 
           Console.WriteLine("Area of cylinder is : " + area);
           Console.WriteLine("Volume of cylinder is : " + volume);
  }
}
输出:
Area of cylinder is : 113.097335529233
Volume of cylinder is : 1017.87601976309

参考:

  • https://msdn.microsoft.com/zh-CN/library/system.math.e(v=vs.110).aspx
  • https://msdn.microsoft.com/zh-CN/library/system.math.pi(v=vs.110).aspx