Math 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为三角、对数和其他常见数学函数提供常量和静态方法。
public ref class Math abstract sealedpublic ref class Math sealedpublic static class Mathpublic sealed class Mathtype Math = classPublic Class MathPublic NotInheritable Class Math- 继承
- 
				Math
示例
以下示例使用 Math 类中的多个数学和三角函数来计算梯形的内部角度。
/// <summary>
/// The following class represents simple functionality of the trapezoid.
/// </summary>
using namespace System;
public ref class MathTrapezoidSample
{
private:
   double m_longBase;
   double m_shortBase;
   double m_leftLeg;
   double m_rightLeg;
public:
   MathTrapezoidSample( double longbase, double shortbase, double leftLeg, double rightLeg )
   {
      m_longBase = Math::Abs( longbase );
      m_shortBase = Math::Abs( shortbase );
      m_leftLeg = Math::Abs( leftLeg );
      m_rightLeg = Math::Abs( rightLeg );
   }
private:
   double GetRightSmallBase()
   {
      return (Math::Pow( m_rightLeg, 2.0 ) - Math::Pow( m_leftLeg, 2.0 ) + Math::Pow( m_longBase, 2.0 ) + Math::Pow( m_shortBase, 2.0 ) - 2 * m_shortBase * m_longBase) / (2 * (m_longBase - m_shortBase));
   }
public:
   double GetHeight()
   {
      double x = GetRightSmallBase();
      return Math::Sqrt( Math::Pow( m_rightLeg, 2.0 ) - Math::Pow( x, 2.0 ) );
   }
   double GetSquare()
   {
      return GetHeight() * m_longBase / 2.0;
   }
   double GetLeftBaseRadianAngle()
   {
      double sinX = GetHeight() / m_leftLeg;
      return Math::Round( Math::Asin( sinX ), 2 );
   }
   double GetRightBaseRadianAngle()
   {
      double x = GetRightSmallBase();
      double cosX = (Math::Pow( m_rightLeg, 2.0 ) + Math::Pow( x, 2.0 ) - Math::Pow( GetHeight(), 2.0 )) / (2 * x * m_rightLeg);
      return Math::Round( Math::Acos( cosX ), 2 );
   }
   double GetLeftBaseDegreeAngle()
   {
      double x = GetLeftBaseRadianAngle() * 180 / Math::PI;
      return Math::Round( x, 2 );
   }
   double GetRightBaseDegreeAngle()
   {
      double x = GetRightBaseRadianAngle() * 180 / Math::PI;
      return Math::Round( x, 2 );
   }
};
int main()
{
   MathTrapezoidSample^ trpz = gcnew MathTrapezoidSample( 20.0,10.0,8.0,6.0 );
   Console::WriteLine( "The trapezoid's bases are 20.0 and 10.0, the trapezoid's legs are 8.0 and 6.0" );
   double h = trpz->GetHeight();
   Console::WriteLine( "Trapezoid height is: {0}", h.ToString() );
   double dxR = trpz->GetLeftBaseRadianAngle();
   Console::WriteLine( "Trapezoid left base angle is: {0} Radians", dxR.ToString() );
   double dyR = trpz->GetRightBaseRadianAngle();
   Console::WriteLine( "Trapezoid right base angle is: {0} Radians", dyR.ToString() );
   double dxD = trpz->GetLeftBaseDegreeAngle();
   Console::WriteLine( "Trapezoid left base angle is: {0} Degrees", dxD.ToString() );
   double dyD = trpz->GetRightBaseDegreeAngle();
   Console::WriteLine( "Trapezoid left base angle is: {0} Degrees", dyD.ToString() );
}
/// <summary>
/// The following class represents simple functionality of the trapezoid.
/// </summary>
using System;
namespace MathClassCS
{
    class MathTrapezoidSample
    {
        private double m_longBase;
        private double m_shortBase;
        private double m_leftLeg;
        private double m_rightLeg;
        public MathTrapezoidSample(double longbase, double shortbase, double leftLeg, double rightLeg)
        {
            m_longBase = Math.Abs(longbase);
            m_shortBase = Math.Abs(shortbase);
            m_leftLeg = Math.Abs(leftLeg);
            m_rightLeg = Math.Abs(rightLeg);
        }
        private double GetRightSmallBase()
        {
            return (Math.Pow(m_rightLeg,2.0) - Math.Pow(m_leftLeg,2.0) + Math.Pow(m_longBase,2.0) + Math.Pow(m_shortBase,2.0) - 2* m_shortBase * m_longBase)/ (2*(m_longBase - m_shortBase));
        }
        public double GetHeight()
        {
            double x = GetRightSmallBase();
            return Math.Sqrt(Math.Pow(m_rightLeg,2.0) - Math.Pow(x,2.0));
        }
        public double GetSquare()
        {
            return GetHeight() * m_longBase / 2.0;
        }
        public double GetLeftBaseRadianAngle()
        {
            double sinX = GetHeight()/m_leftLeg;
            return Math.Round(Math.Asin(sinX),2);
        }
        public double GetRightBaseRadianAngle()
        {
            double x = GetRightSmallBase();
            double cosX = (Math.Pow(m_rightLeg,2.0) + Math.Pow(x,2.0) - Math.Pow(GetHeight(),2.0))/(2*x*m_rightLeg);
            return Math.Round(Math.Acos(cosX),2);
        }
        public double GetLeftBaseDegreeAngle()
        {
            double x = GetLeftBaseRadianAngle() * 180/ Math.PI;
            return Math.Round(x,2);
        }
        public double GetRightBaseDegreeAngle()
        {
            double x = GetRightBaseRadianAngle() * 180/ Math.PI;
            return Math.Round(x,2);
        }
        static void Main(string[] args)
        {
            MathTrapezoidSample trpz = new MathTrapezoidSample(20.0, 10.0, 8.0, 6.0);
            Console.WriteLine("The trapezoid's bases are 20.0 and 10.0, the trapezoid's legs are 8.0 and 6.0");
            double h = trpz.GetHeight();
            Console.WriteLine("Trapezoid height is: " + h.ToString());
            double dxR = trpz.GetLeftBaseRadianAngle();
            Console.WriteLine("Trapezoid left base angle is: " + dxR.ToString() + " Radians");
            double dyR = trpz.GetRightBaseRadianAngle();
            Console.WriteLine("Trapezoid right base angle is: " + dyR.ToString() + " Radians");
            double dxD = trpz.GetLeftBaseDegreeAngle();
            Console.WriteLine("Trapezoid left base angle is: " + dxD.ToString() + " Degrees");
            double dyD = trpz.GetRightBaseDegreeAngle();
            Console.WriteLine("Trapezoid left base angle is: " + dyD.ToString() + " Degrees");
        }
    }
}
open System
/// The following class represents simple functionality of the trapezoid.
type MathTrapezoidSample(longbase, shortbase, leftLeg, rightLeg) =
    member _.GetRightSmallBase() =
        (Math.Pow(rightLeg, 2.) - Math.Pow(leftLeg, 2.) + Math.Pow(longbase, 2.) + Math.Pow(shortbase, 2.) - 2. * shortbase * longbase) / (2. * (longbase - shortbase))
    member this.GetHeight() =
        let x = this.GetRightSmallBase()
        Math.Sqrt(Math.Pow(rightLeg, 2.) - Math.Pow(x, 2.))
    member this.GetSquare() =
        this.GetHeight() * longbase / 2.
    member this.GetLeftBaseRadianAngle() =
        let sinX = this.GetHeight() / leftLeg
        Math.Round(Math.Asin sinX,2)
    member this.GetRightBaseRadianAngle() =
        let x = this.GetRightSmallBase()
        let cosX = (Math.Pow(rightLeg, 2.) + Math.Pow(x, 2.) - Math.Pow(this.GetHeight(), 2.))/(2. * x * rightLeg)
        Math.Round(Math.Acos cosX, 2)
    member this.GetLeftBaseDegreeAngle() =
        let x = this.GetLeftBaseRadianAngle() * 180. / Math.PI
        Math.Round(x, 2)
    member this.GetRightBaseDegreeAngle() =
        let x = this.GetRightBaseRadianAngle() * 180. / Math.PI
        Math.Round(x, 2)
let trpz = MathTrapezoidSample(20., 10., 8., 6.)
printfn "The trapezoid's bases are 20.0 and 10.0, the trapezoid's legs are 8.0 and 6.0"
let h = trpz.GetHeight()
printfn $"Trapezoid height is: {h}"
let dxR = trpz.GetLeftBaseRadianAngle()
printfn $"Trapezoid left base angle is: {dxR} Radians"
let dyR = trpz.GetRightBaseRadianAngle()
printfn $"Trapezoid right base angle is: {dyR} Radians"
let dxD = trpz.GetLeftBaseDegreeAngle()
printfn $"Trapezoid left base angle is: {dxD} Degrees"
let dyD = trpz.GetRightBaseDegreeAngle()
printfn $"Trapezoid left base angle is: {dyD} Degrees"
'The following class represents simple functionality of the trapezoid.
Class MathTrapezoidSample
    Private m_longBase As Double
    Private m_shortBase As Double
    Private m_leftLeg As Double
    Private m_rightLeg As Double
    Public Sub New(ByVal longbase As Double, ByVal shortbase As Double, ByVal leftLeg As Double, ByVal rightLeg As Double)
        m_longBase = Math.Abs(longbase)
        m_shortBase = Math.Abs(shortbase)
        m_leftLeg = Math.Abs(leftLeg)
        m_rightLeg = Math.Abs(rightLeg)
    End Sub
    Private Function GetRightSmallBase() As Double
        GetRightSmallBase = (Math.Pow(m_rightLeg, 2) - Math.Pow(m_leftLeg, 2) + Math.Pow(m_longBase, 2) + Math.Pow(m_shortBase, 2) - 2 * m_shortBase * m_longBase) / (2 * (m_longBase - m_shortBase))
    End Function
    Public Function GetHeight() As Double
        Dim x As Double = GetRightSmallBase()
        GetHeight = Math.Sqrt(Math.Pow(m_rightLeg, 2) - Math.Pow(x, 2))
    End Function
    Public Function GetSquare() As Double
        GetSquare = GetHeight() * m_longBase / 2
    End Function
    Public Function GetLeftBaseRadianAngle() As Double
        Dim sinX As Double = GetHeight() / m_leftLeg
        GetLeftBaseRadianAngle = Math.Round(Math.Asin(sinX), 2)
    End Function
    Public Function GetRightBaseRadianAngle() As Double
        Dim x As Double = GetRightSmallBase()
        Dim cosX As Double = (Math.Pow(m_rightLeg, 2) + Math.Pow(x, 2) - Math.Pow(GetHeight(), 2)) / (2 * x * m_rightLeg)
        GetRightBaseRadianAngle = Math.Round(Math.Acos(cosX), 2)
    End Function
    Public Function GetLeftBaseDegreeAngle() As Double
        Dim x As Double = GetLeftBaseRadianAngle() * 180 / Math.PI
        GetLeftBaseDegreeAngle = Math.Round(x, 2)
    End Function
    Public Function GetRightBaseDegreeAngle() As Double
        Dim x As Double = GetRightBaseRadianAngle() * 180 / Math.PI
        GetRightBaseDegreeAngle = Math.Round(x, 2)
    End Function
    Public Shared Sub Main()
        Dim trpz As MathTrapezoidSample = New MathTrapezoidSample(20, 10, 8, 6)
        Console.WriteLine("The trapezoid's bases are 20.0 and 10.0, the trapezoid's legs are 8.0 and 6.0")
        Dim h As Double = trpz.GetHeight()
        Console.WriteLine("Trapezoid height is: " + h.ToString())
        Dim dxR As Double = trpz.GetLeftBaseRadianAngle()
        Console.WriteLine("Trapezoid left base angle is: " + dxR.ToString() + " Radians")
        Dim dyR As Double = trpz.GetRightBaseRadianAngle()
        Console.WriteLine("Trapezoid right base angle is: " + dyR.ToString() + " Radians")
        Dim dxD As Double = trpz.GetLeftBaseDegreeAngle()
        Console.WriteLine("Trapezoid left base angle is: " + dxD.ToString() + " Degrees")
        Dim dyD As Double = trpz.GetRightBaseDegreeAngle()
        Console.WriteLine("Trapezoid left base angle is: " + dyD.ToString() + " Degrees")
    End Sub
End Class
字段
| E | 表示由常量指定的自然对数基数, | 
| PI | 表示圆的周长与其直径的比率,由常量指定,π。 | 
| Tau | 表示一个轮次的弧度数,由常量 τ 指定。 | 
方法
| Abs(Decimal) | 返回 Decimal 数字的绝对值。 | 
| Abs(Double) | 返回双精度浮点数的绝对值。 | 
| Abs(Int16) | 返回 16 位有符号整数的绝对值。 | 
| Abs(Int32) | 返回 32 位有符号整数的绝对值。 | 
| Abs(Int64) | 返回 64 位有符号整数的绝对值。 | 
| Abs(IntPtr) | 返回本机有符号整数的绝对值。 | 
| Abs(SByte) | 返回 8 位有符号整数的绝对值。 | 
| Abs(Single) | 返回单精度浮点数的绝对值。 | 
| Acos(Double) | 返回其余弦值是指定数字的角度。 | 
| Acosh(Double) | 返回其双曲余弦值是指定数字的角度。 | 
| Asin(Double) | 返回正弦值是指定数字的角度。 | 
| Asinh(Double) | 返回其双曲正弦值是指定数字的角度。 | 
| Atan(Double) | 返回其正切值为指定数字的角度。 | 
| Atan2(Double, Double) | 返回其正切值是两个指定数字的商的角度。 | 
| Atanh(Double) | 返回其双曲正切值为指定数字的角度。 | 
| BigMul(Int32, Int32) | 生成两个 32 位数字的完整乘积。 | 
| BigMul(Int64, Int64) | 生成两个 64 位数字的完整乘积。 | 
| BigMul(Int64, Int64, Int64) | 生成两个 64 位数字的完整乘积。 | 
| BigMul(UInt32, UInt32) | 生成两个无符号 32 位数字的完整乘积。 | 
| BigMul(UInt64, UInt64) | 生成两个无符号 64 位数字的完整积。 | 
| BigMul(UInt64, UInt64, UInt64) | 生成两个无符号 64 位数字的完整积。 | 
| BitDecrement(Double) | 返回与指定值进行比较的最大值。 | 
| BitIncrement(Double) | 返回与指定值进行比较的最小值。 | 
| Cbrt(Double) | 返回指定数字的多维数据集根。 | 
| Ceiling(Decimal) | 返回大于或等于指定十进制数的最小整型值。 | 
| Ceiling(Double) | 返回大于或等于指定双精度浮点数的最小整型值。 | 
| Clamp(Byte, Byte, Byte) | 返回  | 
| Clamp(Decimal, Decimal, Decimal) | 返回  | 
| Clamp(Double, Double, Double) | 返回  | 
| Clamp(Int16, Int16, Int16) | 返回  | 
| Clamp(Int32, Int32, Int32) | 返回  | 
| Clamp(Int64, Int64, Int64) | 返回  | 
| Clamp(IntPtr, IntPtr, IntPtr) | 返回  | 
| Clamp(SByte, SByte, SByte) | 返回  | 
| Clamp(Single, Single, Single) | 返回  | 
| Clamp(UInt16, UInt16, UInt16) | 返回  | 
| Clamp(UInt32, UInt32, UInt32) | 返回  | 
| Clamp(UInt64, UInt64, UInt64) | 返回  | 
| Clamp(UIntPtr, UIntPtr, UIntPtr) | 返回  | 
| CopySign(Double, Double) | 返回一个值,其大小为  | 
| Cos(Double) | 返回指定角度的余弦值。 | 
| Cosh(Double) | 返回指定角度的双曲余弦值。 | 
| DivRem(Byte, Byte) | 生成两个无符号 8 位数字的商和余数。 | 
| DivRem(Int16, Int16) | 生成两个有符号 16 位数字的商和余数。 | 
| DivRem(Int32, Int32) | 生成两个有符号 32 位数字的商和其余部分。 | 
| DivRem(Int32, Int32, Int32) | 计算两个 32 位有符号整数的商,并返回输出参数中的余数。 | 
| DivRem(Int64, Int64) | 生成两个有符号 64 位数字的商和其余部分。 | 
| DivRem(Int64, Int64, Int64) | 计算两个 64 位有符号整数的商,并返回输出参数中的余数。 | 
| DivRem(IntPtr, IntPtr) | 生成两个有符号本机大小数字的商和其余部分。 | 
| DivRem(SByte, SByte) | 生成两个有符号 8 位数字的商和余数。 | 
| DivRem(UInt16, UInt16) | 生成两个无符号 16 位数字的商和余数。 | 
| DivRem(UInt32, UInt32) | 生成两个无符号 32 位数字的商和余数。 | 
| DivRem(UInt64, UInt64) | 生成两个无符号 64 位数字的商和余数。 | 
| DivRem(UIntPtr, UIntPtr) | 生成两个无符号本机大小数字的商和余数。 | 
| Exp(Double) | 返回  | 
| Floor(Decimal) | 返回小于或等于指定十进制数的最大整型值。 | 
| Floor(Double) | 返回小于或等于指定的双精度浮点数的最大整型值。 | 
| FusedMultiplyAdd(Double, Double, Double) | 返回 (x * y) + z,舍入为一个三元运算。 | 
| IEEERemainder(Double, Double) | 返回由另一个指定数字除法产生的余数。 | 
| ILogB(Double) | 返回指定数字的基数 2 整数对数。 | 
| Log(Double) | 返回指定数字的自然(基  | 
| Log(Double, Double) | 返回指定基数中指定数字的对数。 | 
| Log10(Double) | 返回指定数字的基数 10 对数。 | 
| Log2(Double) | 返回指定数字的基数 2 对数。 | 
| Max(Byte, Byte) | 返回两个 8 位无符号整数中的较大值。 | 
| Max(Decimal, Decimal) | 返回两个十进制数字中的较大值。 | 
| Max(Double, Double) | 返回两个双精度浮点数中的较大值。 | 
| Max(Int16, Int16) | 返回两个 16 位有符号整数中的较大值。 | 
| Max(Int32, Int32) | 返回两个 32 位有符号整数中的较大值。 | 
| Max(Int64, Int64) | 返回两个 64 位有符号整数中的较大值。 | 
| Max(IntPtr, IntPtr) | 返回两个本机有符号整数中的较大值。 | 
| Max(SByte, SByte) | 返回两个 8 位有符号整数中的较大值。 | 
| Max(Single, Single) | 返回两个单精度浮点数中的较大值。 | 
| Max(UInt16, UInt16) | 返回两个 16 位无符号整数中的较大值。 | 
| Max(UInt32, UInt32) | 返回两个 32 位无符号整数中的较大值。 | 
| Max(UInt64, UInt64) | 返回两个 64 位无符号整数中的较大值。 | 
| Max(UIntPtr, UIntPtr) | 返回两个本机无符号整数中的较大值。 | 
| MaxMagnitude(Double, Double) | 返回两个双精度浮点数的较大数量级。 | 
| Min(Byte, Byte) | 返回两个 8 位无符号整数中的较小值。 | 
| Min(Decimal, Decimal) | 返回两个十进制数的较小值。 | 
| Min(Double, Double) | 返回两个双精度浮点数的较小值。 | 
| Min(Int16, Int16) | 返回两个 16 位有符号整数的较小值。 | 
| Min(Int32, Int32) | 返回两个 32 位有符号整数的较小值。 | 
| Min(Int64, Int64) | 返回两个 64 位有符号整数的较小值。 | 
| Min(IntPtr, IntPtr) | 返回两个本机有符号整数的较小值。 | 
| Min(SByte, SByte) | 返回两个 8 位有符号整数的较小值。 | 
| Min(Single, Single) | 返回两个单精度浮点数中的较小值。 | 
| Min(UInt16, UInt16) | 返回两个 16 位无符号整数中的较小值。 | 
| Min(UInt32, UInt32) | 返回两个 32 位无符号整数中的较小值。 | 
| Min(UInt64, UInt64) | 返回两个 64 位无符号整数中的较小值。 | 
| Min(UIntPtr, UIntPtr) | 返回两个本机无符号整数的较小值。 | 
| MinMagnitude(Double, Double) | 返回两个双精度浮点数的较小数量级。 | 
| Pow(Double, Double) | 返回一个指定的数字,该数字将引发到指定的幂。 | 
| ReciprocalEstimate(Double) | 返回指定数字的倒数的估计值。 | 
| ReciprocalSqrtEstimate(Double) | 返回指定数字的倒数平方根的估计值。 | 
| Round(Decimal) | 将十进制值舍入到最接近的整数值,并将中点值舍入为最接近的偶数。 | 
| Round(Decimal, Int32) | 将十进制值舍入为指定的小数位数,并将中点值舍入为最接近的偶数。 | 
| Round(Decimal, Int32, MidpointRounding) | 使用指定的舍入约定将十进制值舍入为指定的小数位数。 | 
| Round(Decimal, MidpointRounding) | 使用指定的舍入约定将十进制值舍入一个整数。 | 
| Round(Double) | 将双精度浮点值舍入到最接近的整数值,并将中点值舍入到最接近的偶数。 | 
| Round(Double, Int32) | 将双精度浮点值舍入到指定的小数位数,并将中点值舍入到最接近的偶数。 | 
| Round(Double, Int32, MidpointRounding) | 使用指定的舍入约定将双精度浮点值舍入到指定数量的小数位数。 | 
| Round(Double, MidpointRounding) | 使用指定的舍入约定将双精度浮点值舍入为整数。 | 
| ScaleB(Double, Int32) | 高效返回 x * 2^n 计算。 | 
| Sign(Decimal) | 返回一个整数,指示十进制数的符号。 | 
| Sign(Double) | 返回一个整数,指示双精度浮点数的符号。 | 
| Sign(Int16) | 返回一个整数,指示 16 位有符号整数的符号。 | 
| Sign(Int32) | 返回一个整数,指示 32 位有符号整数的符号。 | 
| Sign(Int64) | 返回一个整数,指示 64 位有符号整数的符号。 | 
| Sign(IntPtr) | 返回一个整数,指示本机大小的有符号整数的符号。 | 
| Sign(SByte) | 返回一个整数,指示 8 位有符号整数的符号。 | 
| Sign(Single) | 返回一个整数,指示单精度浮点数的符号。 | 
| Sin(Double) | 返回指定角度的正弦值。 | 
| SinCos(Double) | 返回指定角度的正弦和余弦值。 | 
| Sinh(Double) | 返回指定角度的双曲正弦值。 | 
| Sqrt(Double) | 返回指定数字的平方根。 | 
| Tan(Double) | 返回指定角度的正切值。 | 
| Tanh(Double) | 返回指定角度的双曲正切值。 | 
| Truncate(Decimal) | 计算指定十进制数的整型部分。 | 
| Truncate(Double) | 计算指定双精度浮点数的整型部分。 |