.NET - System.Math 静态类

创建时间:
2014-06-14 17:33
最近更新:
2018-08-04 12:14

Keywords

(pow / 幂: 一个数自乘若干次的表示形式)

Resource - MSDN

  1. System.Math 类 - Provides constants and static methods for trigonometric, logarithmic, and other common mathematical functions (为三角函数、对数函数和其他通用数学函数提供常数和静态方法). public static class Math.
  2. System.Math.Sqrt 方法 - Returns the square root of a specified number (返回指定数字的平方根). public static double Sqrt(double d).

常用代码

//返回指定数字的平方根 (开平方):
Math.Sqrt();

//返回指定数字的指定次幂:
Math.Pow();
int count = (int)Math.Pow(10, 4);

System.Math.Pow()

  1. System.Math.Pow 方法 - Returns a specified number raised to the specified power (返回指定数字的指定次幂). public static double Pow(double x, double y).

[MethodImplAttribute(MethodImplOptions.InternalCall)]

System.Math 类中约半数方法被定义为 [MethodImpl(MethodImplOptions.InternalCall)]。该特性的作用 (网摘): 用于说明该方法的具体实现可以从 CLR 内部找到。

MSDN

  1. System.Runtime.CompilerServices.MethodImplAttribute 类 - 指定如何实现方法的详细信息。
  2. System.Runtime.CompilerServices.MethodImplOptions 枚举 - 定义如何实现方法的详细信息。This enumeration is used with the System.Runtime.CompilerServices.MethodImplAttribute attribute.
  3. System.Runtime.CompilerServices.MethodImplOptions.InternalCall- 该调用为内部调用,也就是说它调用了在公共语言运行时中实现的方法。

Source Code of System.Math from JetBrains dotPeek 1.0 at 2018-08-04

Complete Copy:

// Type: System.Math
// Assembly: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll

using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Security;

namespace System
{
  [__DynamicallyInvokable]
  public static class Math
  {
    private static double doubleRoundLimit = 1E+16.0;
    private static double[] roundPower10Double = new double[16]
    {
      1.0,
      10.0,
      100.0,
      1000.0,
      10000.0,
      100000.0,
      1000000.0,
      10000000.0,
      100000000.0,
      1000000000.0,
      10000000000.0,
      100000000000.0,
      1000000000000.0,
      10000000000000.0,
      100000000000000.0,
      1E+15.0
    };
    private const int maxRoundingDigits = 15;
    [__DynamicallyInvokable]
    public const double PI = 3.14159265358979;
    [__DynamicallyInvokable]
    public const double E = 2.71828182845905;

    static Math()
    {
    }

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Acos(double d);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Asin(double d);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Atan(double d);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Atan2(double y, double x);

    [__DynamicallyInvokable]
    public static Decimal Ceiling(Decimal d)
    {
      return Decimal.Ceiling(d);
    }

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Ceiling(double a);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Cos(double d);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Cosh(double value);

    [__DynamicallyInvokable]
    public static Decimal Floor(Decimal d)
    {
      return Decimal.Floor(d);
    }

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Floor(double d);

    [SecuritySafeCritical]
    private static unsafe double InternalRound(double value, int digits, MidpointRounding mode)
    {
      if (Math.Abs(value) < Math.doubleRoundLimit)
      {
        double num1 = Math.roundPower10Double[digits];
        value *= num1;
        if (mode == MidpointRounding.AwayFromZero)
        {
          double num2 = Math.SplitFractionDouble(&value);
          if (Math.Abs(num2) >= 0.5)
            value += (double) Math.Sign(num2);
        }
        else
          value = Math.Round(value);
        value /= num1;
      }
      return value;
    }

    [SecuritySafeCritical]
    private static unsafe double InternalTruncate(double d)
    {
      Math.SplitFractionDouble(&d);
      return d;
    }

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Sin(double a);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Tan(double a);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Sinh(double value);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Tanh(double value);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Round(double a);

    [__DynamicallyInvokable]
    public static double Round(double value, int digits)
    {
      if (digits < 0 || digits > 15)
        throw new ArgumentOutOfRangeException("digits", Environment.GetResourceString("ArgumentOutOfRange_RoundingDigits"));
      else
        return Math.InternalRound(value, digits, MidpointRounding.ToEven);
    }

    [__DynamicallyInvokable]
    public static double Round(double value, MidpointRounding mode)
    {
      return Math.Round(value, 0, mode);
    }

    [__DynamicallyInvokable]
    public static double Round(double value, int digits, MidpointRounding mode)
    {
      if (digits < 0 || digits > 15)
        throw new ArgumentOutOfRangeException("digits", Environment.GetResourceString("ArgumentOutOfRange_RoundingDigits"));
      if (mode >= MidpointRounding.ToEven && mode <= MidpointRounding.AwayFromZero)
        return Math.InternalRound(value, digits, mode);
      string key = "Argument_InvalidEnumValue";
      object[] objArray = new object[2];
      int index1 = 0;
      // ISSUE: variable of a boxed type
      __Boxed<MidpointRounding> local = (Enum) mode;
      objArray[index1] = (object) local;
      int index2 = 1;
      string str = "MidpointRounding";
      objArray[index2] = (object) str;
      throw new ArgumentException(Environment.GetResourceString(key, objArray), "mode");
    }

    [__DynamicallyInvokable]
    public static Decimal Round(Decimal d)
    {
      return Decimal.Round(d, 0);
    }

    [__DynamicallyInvokable]
    public static Decimal Round(Decimal d, int decimals)
    {
      return Decimal.Round(d, decimals);
    }

    [__DynamicallyInvokable]
    public static Decimal Round(Decimal d, MidpointRounding mode)
    {
      return Decimal.Round(d, 0, mode);
    }

    [__DynamicallyInvokable]
    public static Decimal Round(Decimal d, int decimals, MidpointRounding mode)
    {
      return Decimal.Round(d, decimals, mode);
    }

    [SecurityCritical]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static double SplitFractionDouble(double* value);

    [__DynamicallyInvokable]
    public static Decimal Truncate(Decimal d)
    {
      return Decimal.Truncate(d);
    }

    [__DynamicallyInvokable]
    public static double Truncate(double d)
    {
      return Math.InternalTruncate(d);
    }

    [SecuritySafeCritical]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Sqrt(double d);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Log(double d);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Log10(double d);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Exp(double d);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Pow(double x, double y);

    [__DynamicallyInvokable]
    public static double IEEERemainder(double x, double y)
    {
      if (double.IsNaN(x))
        return x;
      if (double.IsNaN(y))
        return y;
      double d = x % y;
      if (double.IsNaN(d))
        return double.NaN;
      if (d == 0.0 && double.IsNegative(x))
        return double.NegativeZero;
      double num = d - Math.Abs(y) * (double) Math.Sign(x);
      if (Math.Abs(num) == Math.Abs(d))
      {
        double a = x / y;
        if (Math.Abs(Math.Round(a)) > Math.Abs(a))
          return num;
        else
          return d;
      }
      else if (Math.Abs(num) < Math.Abs(d))
        return num;
      else
        return d;
    }

    [CLSCompliant(false)]
    [__DynamicallyInvokable]
    public static sbyte Abs(sbyte value)
    {
      if ((int) value >= 0)
        return value;
      else
        return Math.AbsHelper(value);
    }

    private static sbyte AbsHelper(sbyte value)
    {
      if ((int) value == (int) sbyte.MinValue)
        throw new OverflowException(Environment.GetResourceString("Overflow_NegateTwosCompNum"));
      else
        return -value;
    }

    [__DynamicallyInvokable]
    public static short Abs(short value)
    {
      if ((int) value >= 0)
        return value;
      else
        return Math.AbsHelper(value);
    }

    private static short AbsHelper(short value)
    {
      if ((int) value == (int) short.MinValue)
        throw new OverflowException(Environment.GetResourceString("Overflow_NegateTwosCompNum"));
      else
        return -value;
    }

    [__DynamicallyInvokable]
    public static int Abs(int value)
    {
      if (value >= 0)
        return value;
      else
        return Math.AbsHelper(value);
    }

    private static int AbsHelper(int value)
    {
      if (value == int.MinValue)
        throw new OverflowException(Environment.GetResourceString("Overflow_NegateTwosCompNum"));
      else
        return -value;
    }

    [__DynamicallyInvokable]
    public static long Abs(long value)
    {
      if (value >= 0L)
        return value;
      else
        return Math.AbsHelper(value);
    }

    private static long AbsHelper(long value)
    {
      if (value == long.MinValue)
        throw new OverflowException(Environment.GetResourceString("Overflow_NegateTwosCompNum"));
      else
        return -value;
    }

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static float Abs(float value);

    [SecuritySafeCritical]
    [__DynamicallyInvokable]
    [MethodImpl(MethodImplOptions.InternalCall)]
    public static double Abs(double value);

    [__DynamicallyInvokable]
    public static Decimal Abs(Decimal value)
    {
      return Decimal.Abs(value);
    }

    [CLSCompliant(false)]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static sbyte Max(sbyte val1, sbyte val2)
    {
      if ((int) val1 < (int) val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static byte Max(byte val1, byte val2)
    {
      if ((int) val1 < (int) val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static short Max(short val1, short val2)
    {
      if ((int) val1 < (int) val2)
        return val2;
      else
        return val1;
    }

    [CLSCompliant(false)]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static ushort Max(ushort val1, ushort val2)
    {
      if ((int) val1 < (int) val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static int Max(int val1, int val2)
    {
      if (val1 < val2)
        return val2;
      else
        return val1;
    }

    [CLSCompliant(false)]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static uint Max(uint val1, uint val2)
    {
      if (val1 < val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static long Max(long val1, long val2)
    {
      if (val1 < val2)
        return val2;
      else
        return val1;
    }

    [CLSCompliant(false)]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static ulong Max(ulong val1, ulong val2)
    {
      if (val1 < val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static float Max(float val1, float val2)
    {
      if ((double) val1 > (double) val2 || float.IsNaN(val1))
        return val1;
      else
        return val2;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static double Max(double val1, double val2)
    {
      if (val1 > val2 || double.IsNaN(val1))
        return val1;
      else
        return val2;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static Decimal Max(Decimal val1, Decimal val2)
    {
      return Decimal.Max(val1, val2);
    }

    [CLSCompliant(false)]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static sbyte Min(sbyte val1, sbyte val2)
    {
      if ((int) val1 > (int) val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static byte Min(byte val1, byte val2)
    {
      if ((int) val1 > (int) val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static short Min(short val1, short val2)
    {
      if ((int) val1 > (int) val2)
        return val2;
      else
        return val1;
    }

    [CLSCompliant(false)]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static ushort Min(ushort val1, ushort val2)
    {
      if ((int) val1 > (int) val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static int Min(int val1, int val2)
    {
      if (val1 > val2)
        return val2;
      else
        return val1;
    }

    [CLSCompliant(false)]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static uint Min(uint val1, uint val2)
    {
      if (val1 > val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static long Min(long val1, long val2)
    {
      if (val1 > val2)
        return val2;
      else
        return val1;
    }

    [CLSCompliant(false)]
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static ulong Min(ulong val1, ulong val2)
    {
      if (val1 > val2)
        return val2;
      else
        return val1;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static float Min(float val1, float val2)
    {
      if ((double) val1 < (double) val2 || float.IsNaN(val1))
        return val1;
      else
        return val2;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static double Min(double val1, double val2)
    {
      if (val1 < val2 || double.IsNaN(val1))
        return val1;
      else
        return val2;
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
    [__DynamicallyInvokable]
    public static Decimal Min(Decimal val1, Decimal val2)
    {
      return Decimal.Min(val1, val2);
    }

    [__DynamicallyInvokable]
    public static double Log(double a, double newBase)
    {
      if (double.IsNaN(a))
        return a;
      if (double.IsNaN(newBase))
        return newBase;
      if (newBase == 1.0 || a != 1.0 && (newBase == 0.0 || double.IsPositiveInfinity(newBase)))
        return double.NaN;
      else
        return Math.Log(a) / Math.Log(newBase);
    }

    [CLSCompliant(false)]
    [__DynamicallyInvokable]
    public static int Sign(sbyte value)
    {
      if ((int) value < 0)
        return -1;
      return (int) value > 0 ? 1 : 0;
    }

    [__DynamicallyInvokable]
    public static int Sign(short value)
    {
      if ((int) value < 0)
        return -1;
      return (int) value > 0 ? 1 : 0;
    }

    [__DynamicallyInvokable]
    public static int Sign(int value)
    {
      if (value < 0)
        return -1;
      return value > 0 ? 1 : 0;
    }

    [__DynamicallyInvokable]
    public static int Sign(long value)
    {
      if (value < 0L)
        return -1;
      return value > 0L ? 1 : 0;
    }

    [__DynamicallyInvokable]
    public static int Sign(float value)
    {
      if ((double) value < 0.0)
        return -1;
      if ((double) value > 0.0)
        return 1;
      if ((double) value == 0.0)
        return 0;
      else
        throw new ArithmeticException(Environment.GetResourceString("Arithmetic_NaN"));
    }

    [__DynamicallyInvokable]
    public static int Sign(double value)
    {
      if (value < 0.0)
        return -1;
      if (value > 0.0)
        return 1;
      if (value == 0.0)
        return 0;
      else
        throw new ArithmeticException(Environment.GetResourceString("Arithmetic_NaN"));
    }

    [__DynamicallyInvokable]
    public static int Sign(Decimal value)
    {
      if (value < Decimal.Zero)
        return -1;
      return value > Decimal.Zero ? 1 : 0;
    }

    public static long BigMul(int a, int b)
    {
      return (long) a * (long) b;
    }

    public static int DivRem(int a, int b, out int result)
    {
      result = a % b;
      return a / b;
    }

    public static long DivRem(long a, long b, out long result)
    {
      result = a % b;
      return a / b;
    }
  }
}