Mathematical Functions in C#

The System.Math class in C# provides methods and properties to perform mathematical operations, trigonometric calculations, logarithmic functions, and other common mathematical computations. All methods in the Math class are static, meaning you can call them directly without creating an instance.

Common Mathematical Methods

The following table shows some of the most commonly used methods in the Math class −

Method Description
Abs() Returns the absolute value of a number (works with decimal, double, int, etc.)
Ceiling() Returns the smallest integer greater than or equal to the specified number
Floor() Returns the largest integer less than or equal to the specified number
Round() Rounds a value to the nearest integer or specified decimal places
Max() Returns the larger of two numbers
Min() Returns the smaller of two numbers
Pow() Returns a specified number raised to the specified power
Sqrt() Returns the square root of a specified number

Using Math.Abs() for Absolute Values

The Math.Abs() method returns the absolute value of a number, removing any negative sign −

using System;

class Program {
   static void Main() {
      int val1 = 250;
      int val2 = -150;
      double val3 = -45.67;

      Console.WriteLine("Original values:");
      Console.WriteLine(val1);
      Console.WriteLine(val2);
      Console.WriteLine(val3);

      Console.WriteLine("\nAbsolute values:");
      Console.WriteLine(Math.Abs(val1));
      Console.WriteLine(Math.Abs(val2));
      Console.WriteLine(Math.Abs(val3));
   }
}

The output of the above code is −

Original values:
250
-150
-45.67

Absolute values:
250
150
45.67

Using Basic Mathematical Operations

Here's an example demonstrating various mathematical operations −

using System;

class Program {
   static void Main() {
      double number = 16.75;
      
      Console.WriteLine("Number: " + number);
      Console.WriteLine("Ceiling: " + Math.Ceiling(number));
      Console.WriteLine("Floor: " + Math.Floor(number));
      Console.WriteLine("Round: " + Math.Round(number));
      Console.WriteLine("Square root: " + Math.Sqrt(number));
      Console.WriteLine("Power of 2: " + Math.Pow(number, 2));
      Console.WriteLine("Max of 10 and 20: " + Math.Max(10, 20));
      Console.WriteLine("Min of 10 and 20: " + Math.Min(10, 20));
   }
}

The output of the above code is −

Number: 16.75
Ceiling: 17
Floor: 16
Round: 17
Square root: 4.09267849755573
Power of 2: 280.5625
Max of 10 and 20: 20
Min of 10 and 20: 10

Trigonometric Functions

The Math class includes trigonometric functions like Sin(), Cos(), Tan(), and their inverse functions. These methods work with radians −

using System;

class Program {
   static void Main() {
      double angleInRadians = Math.PI / 4; // 45 degrees in radians
      
      Console.WriteLine("Angle: " + angleInRadians + " radians");
      Console.WriteLine("Sin: " + Math.Sin(angleInRadians));
      Console.WriteLine("Cos: " + Math.Cos(angleInRadians));
      Console.WriteLine("Tan: " + Math.Tan(angleInRadians));
      
      // Inverse trigonometric functions
      Console.WriteLine("\nInverse functions:");
      Console.WriteLine("Asin(0.5): " + Math.Asin(0.5));
      Console.WriteLine("Acos(0.5): " + Math.Acos(0.5));
      Console.WriteLine("Atan(1): " + Math.Atan(1));
   }
}

The output of the above code is −

Angle: 0.785398163397448 radians
Sin: 0.7071067811865476
Cos: 0.7071067811865476
Tan: 1

Inverse functions:
Asin(0.5): 0.5235987755982989
Acos(0.5): 1.0471975511965979
Atan(1): 0.7853981633974483

Mathematical Constants

The Math class also provides important mathematical constants −

using System;

class Program {
   static void Main() {
      Console.WriteLine("Pi: " + Math.PI);
      Console.WriteLine("E (Euler's number): " + Math.E);
      
      // Using constants in calculations
      double radius = 5.0;
      double area = Math.PI * Math.Pow(radius, 2);
      Console.WriteLine("Area of circle with radius " + radius + ": " + area);
   }
}

The output of the above code is −

Pi: 3.141592653589793
E (Euler's number): 2.718281828459045
Area of circle with radius 5: 78.53981633974483

Conclusion

The System.Math class in C# provides a comprehensive set of static methods for mathematical operations including absolute values, rounding, trigonometric functions, logarithmic calculations, and mathematical constants. These methods are essential for performing precise mathematical computations in C# applications.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements