📜  C#| Math.Abs()方法|套装– 2

📅  最后修改于: 2021-05-30 00:02:01             🧑  作者: Mango

C#| Math.Abs()方法|套装– 1

在C#中, Abs()是Math类方法,用于返回指定数字的绝对值。通过将不同类型的参数传递给此方法,可以使其过载。重载列表中共有7种方法。

  1. Math.Abs(十进制)
  2. Math.Abs(Double)
  3. Math.Abs(Int16)
  4. Math.Abs(Int32)
  5. Math.Abs(Int64)
  6. Math.Abs(SByte)
  7. Math.Abs(Single)

    Math.Abs(Int64)

    此方法用于返回64位带符号整数的绝对值。

    句法:

    public static long Abs (long val);

    范围:

    返回类型:返回一个64位有符号整数,表示r ,使得0≤r≤Int64.MaxValue

    异常:如果val的值等于Int64.MinValue,则此方法将提供OverflowException

    例子:

    // C# Program to illustrate the
    // Math.Abs(Int64) Method
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // Taking long values
            long[] val = {Int64.MaxValue, 78345482, -4687985, 0};
      
            // using foreach loop
            foreach(long value in val)
      
                // Displaying the result
                Console.WriteLine("Absolute value of {0} = {1}",
                                        value, Math.Abs(value));
        }
    }
    

    输出:

    Absolute value of 9223372036854775807 = 9223372036854775807
    Absolute value of 78345482 = 78345482
    Absolute value of -4687985 = 4687985
    Absolute value of 0 = 0
    

    Math.Abs(SByte)

    此方法用于返回8位带符号整数的绝对值。

    句法:

    public static sbyte Abs (sbyte val);

    范围:

    返回类型:返回一个8位带符号整数,表示r ,使得0≤r≤SByte.MaxValue

    异常:如果val的值等于SByte.MinValue,则此方法将给出OverflowException

    例子:

    // C# Program to illlustrate the
    // Math.Abs(SByte) Method
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // Taking SByte values
            sbyte[] sb = {SByte.MaxValue, 45, -41, 0};
      
            // using foreach loop
            foreach(sbyte value in sb)
      
                // Displaying the result
                Console.WriteLine("Absolute value of {0} = {1}",
                                        value, Math.Abs(value));
        }
    }
    

    输出:

    Absolute value of 127 = 127
    Absolute value of 45 = 45
    Absolute value of -41 = 41
    Absolute value of 0 = 0
    

    Math.Abs(Single)

    此方法用于返回单精度浮点数的绝对值。

    句法:

    public static float Abs (float val);

    范围:

    返回类型:返回一个单精度浮点数,例如r ,即0≤r≤Single.MaxValue

    笔记:

    • 如果val等于NegativeInfinityPositiveInfinity ,则返回值将为PositiveInfinity
    • 如果val等于NaN,则返回值为NaN

    例子:

    // C# Program to illlustrate the
    // Math.Abs(Single) Method
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            float nan = float.NaN;
      
            // Taking float values
            float[] fl = {float.MinValue, 127.58f, 0.000f,
                        7556.48e10f, nan, float.MaxValue};
      
            // using foreach loop
            foreach(float value in fl)
      
                // Displaying the result
                Console.WriteLine("Absolute value of {0} = {1}",
                                        value, Math.Abs(value));
        }
    }
    

    输出:

    Absolute value of -3.402823E+38 = 3.402823E+38
    Absolute value of 127.58 = 127.58
    Absolute value of 0 = 0
    Absolute value of 7.55648E+13 = 7.55648E+13
    Absolute value of NaN = NaN
    Absolute value of 3.402823E+38 = 3.402823E+38
    

    参考: https://docs.microsoft.com/zh-cn/dotnet/api/system.math.abs?view=netframework-4.7.2