C#.Net Math Class and Its Methods

C#.Net Math Class and Its Methods: Learn C# Math Class's Methods with Example, this post contains list of C#.Net Math class's methods. Last updated : August 28, 2023

C#.Net Math Class

Math is a pre-defined class in C#.Net, which has many methods to perform mathematical operations without using operators for that.

In C# programming language, Math class is used for the mathematical operations, Math class provides various constants and static methods for logarithmic, trigonometric, and other mathematic functions.

This post contains some of the common and most popular Math class's methods which may help you to perform related operations in C# program.

These are following important methods are used in c#:

  1. Math.Pow()
  2. Math.Sqrt()
  3. Math.Max()
  4. Math.Min()
  5. Math.Ceiling()
  6. Math.Floor()
  7. Math.Abs()
  8. Math.Round()
  9. Math.DivRem()
  10. Math.IEEERemainder()
  11. Math.BigMul()
  12. Math.Sign()
  13. Math.Sin()
  14. Math.Cos()
  15. Math.Cosh()
  16. Math.Sinh()
  17. Math.Tan()
  18. Math.Tanh()
  19. Math.Asin()
  20. Math.Acos()
  21. Math.Atan()
  22. Math.Atan2()
  23. Math.Log()
  24. Math.Log10()
  25. Math.Truncate()
  26. Math.Exp()

1) Math.Pow()

This method is used to calculate power of given number.

public static double Pow (double x, double y);

2) Math.Sqrt()

This method is used to calculate square root of given number.

public static double Sqrt (double d);

3) Math.Max()

This method is used to find largest number from two given number.

Math.Max(Decimal, Decimal)

4) Math.Min()

This method is used to find smallest number from two given number.

Math.Min(Decimal, Decimal)

5) Math.Ceiling()

This method is used to ceil given floating point number.

Math.Ceiling(Decimal)

6) Math.Floor()

This method is used to floor given floating point number.

Math.Floor(Double)

7) Math.Abs() Method

Thi method is used to return the absolute value of a given value. It means if we pass a positive value then it returns a positive value and if pass a negative value then it will also return a positive value.

Math.Abs(Number)

8) Math.Round() Method

This method is used to round off floating-point numbers to the nearest integer value. This method is overloaded 8 times.

Decimal     Math.Round(Decimal)

9) Math.DivRem() Method

This method is used to calculate the quotient of two given numbers and it also returns the remainder in the third output parameter.

int Math.DivRem(int dividend, int divisor, out int remainder);

10) Math.IEEERemainder() Method

This method is used to get the remainder of double values.

double Math.IEEERemainder(double dividend, double divisor);

11) Math.BigMul() Method

This method is used to return long values after the multiplication of the maximum value of an integer. Normally we multiply two integers with maximum value then it does not produce actual output. So we need to BigMul() method of Math class.

long Math.BigMul(int Val1, int Val2);

12) Math.Sign() Method

This method is used to indicate the sign of the given number. This method is overloaded 7 times.

int Math.Sign(Decimal)

13) Math.Sin() Method

This method is used to return the Sine of the specified angle.

double Math.Sin(double angle);

14) Math.Cos() Method

This method is used to return the cosine of the specified angle.

double Math.Cos(double D);

15) Math.Cosh() Method

This method is used to return the hyperbolic cosine of the specified angle.

double Math.Cosh(double D);

16) Math.Sinh() Method

This method is used to return the hyperbolic Sine of a specified angle.

double Math.Sinh(double angle);

17) Math.Tan() Method

This method is used to return the tangent of a specified angle.

double Math.Tan(double angle);

18) Math.Tanh() Method

This method is used to return hyperbolic tangent of the specified angle.

double Math.Tanh(double angle);

19) Math.Asin() Method

This method is used to return the angle whose sin is the specified number measured in radians.

double Math.Asin(double D);

20) Math.Acos() Method

This method is used to return an angle whose cosine is the specified number measured in radians.

double Math.Acos(double D);

21) Math.Atan() Method

This method is used to return an angle whose tangent is the specified number measured in radians.

double Math.Atan(double D);

22) Math.Atan2() Method

This method is used to return an angle whose tangent is the quotient of two specified numbers measured in radians.

double Math.Atan2(double Y, double X);

23) Math.Log() Method

This method is used to get the logarithm of a specified number with a given base. This method is overload two times. If we pass a single parameter then it will return the logarithm of the given number using base e. if we pass two parameters then the first parameter is used for number and the second parameter is for the base.

double Math.Log(double number);
double Math.Log(double number, double base);

24) Math.Log10() Method

This method is used to get a base 10 logarithm of the specified number.

double Math.Log10(double number);

25) Math.Truncate() Method

This method is used to return an integral part of the given number, this method is overloaded two times then the given number can be a double or decimal number.

double Math.Truncate(double num);
decimal Math.Truncate(decimal num);

26) Math.Exp() Method

This method is used to get the exponent raised to the given power.

double Math.Exp(double D);

C#.Net Math Class Example

Consider the below example of some of the important and most commonly used methods of C#.Net Math class:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double num    = 0;
            double power = 0;
            double result = 0;

           
            Console.Write("Enter Number : ");
            num = Convert.ToDouble(Console.ReadLine());

            Console.Write("Enter Power : ");
            power = Convert.ToDouble(Console.ReadLine());

            result = Math.Pow(num, power);
            Console.WriteLine("Result : " + result);

            result = Math.Sqrt(16);
            Console.WriteLine("Result : " + result);

            result = Math.Max(10.2, 10.5);
            Console.WriteLine("Result : " + result);

            result = Math.Min(10.2, 10.5);
            Console.WriteLine("Result : " + result);

            result = Math.Ceiling(10.2);
            Console.WriteLine("Result : " + result);

            result = Math.Floor(10.2);
            Console.WriteLine("Result : " + result);

        }
    }
}

The output of the above program is:

Enter Number : 2
Enter Power : 3
Result : 8
Result : 4
Result : 10.5
Result : 10.2
Result : 11
Result : 10

C#.Net Math Class Reference

Reference: Math Class

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.