📜  C#|数学课

📅  最后修改于: 2021-05-29 20:06:09             🧑  作者: Mango

在C#中, Math类位于System命名空间下。它用于为对数,三角函数和其他有用的数学函数提供静态方法和常量。它是一个静态类,并且继承了对象类。

public static class Math

领域

字段是在类或结构中声明的变量。这些被视为其包含类型的成员。字段可以是静态或实例字段。数学类包含两个字段,即EPI

  1. Math.E字段:此字段表示由常数e指定的自然对数底数。
  2. Math.PI字段:它表示圆的周长与其直径的比率,由常数PI(π)指定。

例子:

// C# program to demonstrate the
// value of Math Class Fields
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);
  
        // To find PI constant values
        double pi_value = Math.PI;
  
        // Print result
        Console.WriteLine("Math.PI = " + pi_value);
    }
}

输出:

Math.E = 2.71828182845905
Math.PI = 3.14159265358979

您可以从C#中阅读有关字段的更多信息。带有示例的数学类字段

方法

Method Description
Abs() Returns the absolute value of a specified number.
Acos() Returns the angle whose cosine is the specified number.
Acosh() Returns the Inverse hyperbolic cosine of the specified number.
Asin() Returns the angle whose sine is the specified number.
Asinh() Returns the Inverse hyperbolic sine of the specified number.
Atan() Returns the angle whose tangent is the specified number.
Atan2() Returns the angle whose tangent is the quotient of two specified numbers.
Atanh() Returns the Inverse hyperbolic tangent of the specified number.
BigMul() Produces the full product of two 32-bit numbers.
Cbrt() Returns the cube root of a specified value.
Ceiling() Returns the smallest integral value greater than or equal to the specified number.
Clamp() It is used to restrict a value to a given range.
Cos() Returns the cosine of the specified angle.
Cosh() Returns the hyperbolic cosine of the specified angle.
DivRem() Calculates the quotient of two numbers and also returns the remainder in an output parameter.
Exp() Returns e raised to the specified power.
Floor() Returns the largest integral value less than or equal to the specified number.
IEEERemainder() Returns the remainder resulting from the division of a specified number by another specified number.
Log() Returns the logarithm of a specified number.
Log10() Returns the base 10 logarithm of a specified number.
Max() Returns the larger of two specified numbers.
Min() Returns the smaller of two numbers.
Pow() Returns a specified number raised to the specified power.
Round() Rounds a value to the nearest integer or to the specified number of fractional digits.
Sign() Returns an integer that indicates the sign of a number.
Sin() Returns the sine of the specified angle.
Sinh() Returns the hyperbolic sine of the specified angle.
Sqrt() Returns the square root of a specified number.
Tan() Returns the tangent of the specified angle.
Tanh() Returns the hyperbolic tangent of the specified angle.
Truncate() Calculates the integral part of a number.

例子:

// C# program to illustrate the
// Math class methods
using System;
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // using Floor() Method
        Console.WriteLine("Floor value of 123.123: "
                             + Math.Floor(123.123));
  
        // using Asin() Method
        Console.WriteLine("Asin value of 0.35: "
                             + Math.Asin(0.35));
  
        // using Sqrt() Method
        Console.WriteLine("Square Root of 81: "
                              + Math.Sqrt(81));
  
        // using Round() Method
        Console.WriteLine("Round value of 14.6534: "
                             + Math.Round(14.6534));
    }
}

输出:

Floor value of 123.123: 123
Asin value of 0.35: 0.35757110364551
Square Root of 81: 9
Round value of 14.6534: 15