Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

C# | Math.Abs() Method | Set – 2

  • Last Updated : 01 Feb, 2019

C# | Math.Abs() Method | Set – 1

In C#, Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it. There are total 7 methods in its overload list.

  1. Math.Abs(Decimal)
  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)



    This method is used to return the absolute value of a 64-bit signed integer.

    Syntax:

    public static long Abs (long val);

    Parameter:

    val: It is the required number which is greater than Int64.MinValue, but less than or equal to Int64.MaxValue of type System.Int64.

    Return Type: It returns a 64-bit signed integer say r, such that 0 ≤ r ≤ Int64.MaxValue.

    Exception: This method will give OverflowException if the value of val is equals to Int64.MinValue.

    Example:




    // 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));
        }
    }

    Output:



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

    Math.Abs(SByte)

    This method is used to return the absolute value of an 8-bit signed integer.

    Syntax:

    public static sbyte Abs (sbyte val);

    Parameter:

    val: It is the required number which is greater than SByte.MinValue, but less than or equal to SByte.MaxValue of type System.SByte.

    Return Type: It returns a 8-bit signed integer say r, such that 0 ≤ r ≤ SByte.MaxValue.

    Exception: This method will give OverflowException if the value of val is equals to SByte.MinValue.

    Example:




    // 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));
        }
    }

    Output:

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

    Math.Abs(Single)



    This method is used to return the absolute value of a single-precision floating-point number.

    Syntax:

    public static float Abs (float val);

    Parameter:

    val: It is the required number which is greater than or equal to Single.MinValue, but less than or equal to MaxValue of type System.Single.

    Return Type: It returns a single-precision floating-point number say r, such that 0 ≤ r ≤ Single.MaxValue.

    Note:

    • If val is equal to NegativeInfinity or PositiveInfinity, the return value will be PositiveInfinity.
    • If the val is equal to NaN then return value will be NaN.

    Example:




    // 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));
        }
    }

    Output:

    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
    

    Reference: https://docs.microsoft.com/en-us/dotnet/api/system.math.abs?view=netframework-4.7.2




    My Personal Notes arrow_drop_up
Recommended Articles
Page :