Math Class in C#

The Math class in C# provides static methods and constants for trigonometric, logarithmic, and other mathematical functions. This class is part of the System namespace and offers essential mathematical operations without requiring instantiation.

The Math class includes important mathematical constants like Math.E and Math.PI, along with numerous methods for calculations such as power, trigonometric, and logarithmic functions.

Math Constants

Math.E

The Math.E field represents the natural logarithmic base, specified by the mathematical constant e

public const double E = 2.71828182845905;
using System;

public class Demo {
    public static void Main() {
        double d = Math.E;
        Console.WriteLine("Math.E = " + d);
        Console.WriteLine("e^2 = " + Math.Pow(Math.E, 2));
    }
}

The output of the above code is −

Math.E = 2.71828182845905
e^2 = 7.38905609893065

Math.PI

The Math.PI field represents the ratio of a circle's circumference to its diameter, specified by the mathematical constant ? −

public const double PI = 3.14159265358979;
using System;

public class Demo {
    public static void Main() {
        double d = Math.PI;
        Console.WriteLine("Math.PI = " + d);
        
        double radius = 5;
        double circumference = 2 * Math.PI * radius;
        Console.WriteLine("Circumference of circle with radius 5: " + circumference);
    }
}

The output of the above code is −

Math.PI = 3.14159265358979
Circumference of circle with radius 5: 31.4159265358979

Common Math Methods

Math Class Methods Power Trigonometric Rounding Pow() Sqrt() Sin(), Cos() Acos(), Asin() Round() Floor(), Ceiling() ... All methods are static - no instantiation needed Access via Math.MethodName()

Math.Acos()

The Math.Acos() method returns the angle (in radians) whose cosine is the specified number −

public static double Acos(double val);

The parameter val represents a cosine value that must be between -1 and 1 (inclusive).

using System;

public class Demo {
    public static void Main() {
        double val1 = 0.0;
        double val2 = 1.0;
        double val3 = -1.0;
        double invalidVal = Double.PositiveInfinity;
        
        Console.WriteLine("Acos(0.0) = {0} radians", Math.Acos(val1));
        Console.WriteLine("Acos(1.0) = {0} radians", Math.Acos(val2));
        Console.WriteLine("Acos(-1.0) = {0} radians", Math.Acos(val3));
        Console.WriteLine("Acos(Infinity) = {0}", Math.Acos(invalidVal));
    }
}

The output of the above code is −

Acos(0.0) = 1.5707963267949 radians
Acos(1.0) = 0 radians
Acos(-1.0) = 3.14159265358979 radians
Acos(Infinity) = NaN

Math.Pow()

The Math.Pow() method computes a number raised to the power of another number −

public static double Pow(double baseValue, double exponent);

Here, baseValue is the number to be raised to a power, and exponent specifies the power.

using System;

public class Demo {
    public static void Main() {
        Console.WriteLine("2^3 = " + Math.Pow(2, 3));
        Console.WriteLine("5^0 = " + Math.Pow(5, 0));
        Console.WriteLine("0^5 = " + Math.Pow(0, 5));
        Console.WriteLine("4^0.5 = " + Math.Pow(4, 0.5)); // Square root
        Console.WriteLine("(-2)^3 = " + Math.Pow(-2, 3));
    }
}

The output of the above code is −

2^3 = 8
5^0 = 1
0^5 = 0
4^0.5 = 2
(-2)^3 = -8

Additional Math Methods

Example

using System;

public class MathDemo {
    public static void Main() {
        double number = 16.75;
        
        Console.WriteLine("Original number: " + number);
        Console.WriteLine("Square root: " + Math.Sqrt(number));
        Console.WriteLine("Absolute value: " + Math.Abs(-number));
        Console.WriteLine("Ceiling: " + Math.Ceiling(number));
        Console.WriteLine("Floor: " + Math.Floor(number));
        Console.WriteLine("Round: " + Math.Round(number));
        Console.WriteLine("Max(10, 20): " + Math.Max(10, 20));
        Console.WriteLine("Min(10, 20): " + Math.Min(10, 20));
    }
}

The output of the above code is −

Original number: 16.75
Square root: 4.09268292354518
Absolute value: 16.75
Ceiling: 17
Floor: 16
Round: 17
Max(10, 20): 20
Min(10, 20): 10

Conclusion

The Math class in C# provides essential mathematical constants and static methods for various calculations. It offers trigonometric functions, power operations, rounding methods, and other mathematical utilities that are commonly needed in programming applications without requiring object instantiation.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements