ZetCode

C# Math

last modified October 16, 2026

In this article we cover common mathematical functions found in the System.Math class.

The static System.Math class provides constants and static methods for trigonometric, logarithmic, and other common mathematical functions. Most methods work with double values; methods such as Abs, Max, Round, or Floor also have overloads for other numeric types, including decimal.

C# Math.Abs

The Math.Abs function returns the absolute value of a specified number. The absolute value of a number is the number without its sign.

Program.cs
int[] vals = [-1, 2, 0, -3, -2, 9, 7];

foreach (var e in vals)
{
    Console.Write($"{Math.Abs(e)} ");
}

Console.WriteLine();

Console.WriteLine(vals.Sum());
Console.WriteLine(vals.Sum(Math.Abs));

We have an array of integers. First, we print the absolute value of all integers. Then we calculate the sum of all values and the sum of all absolute values.

$ dotnet run
1 2 0 3 2 9 7 
12
24

Note that Math.Abs(int.MinValue) throws an OverflowException, because the positive value does not fit into int.

C# Math.Ceiling & Math.Floor

The Math.Ceiling function returns the smallest integral value that is greater than or equal to x. The Math.Floor function returns the largest integral value less than or equal to x. The Math.Truncate function removes the fractional part.

Program.cs
decimal[] vals = [6.07m, 6.64m, 0.12m, -0.12m, -6.13m, -6.65m];

Console.WriteLine($"{"Number",7} {"Ceiling",8} {"Floor",6} {"Truncate",9}");

foreach (var e in vals)
{
    Console.WriteLine($"{e,7} {Math.Ceiling(e),8} {Math.Floor(e),6} {Math.Truncate(e),9}");
}

In the example, we use the Math.Ceiling, Math.Floor, and Math.Truncate functions on an array of decimal numbers. For negative numbers, Floor and Truncate give different results.

$ dotnet run
 Number  Ceiling  Floor  Truncate
   6.07        7      6         6
   6.64        7      6         6
   0.12        1      0         0
  -0.12        0     -1         0
  -6.13       -6     -7        -6
  -6.65       -6     -7        -6

C# Math.Round

The Math.Round function rounds a value to the nearest integer or to the specified number of fractional digits. By default, a midpoint value is rounded to the nearest even number; this is called banker's rounding. The MidpointRounding parameter changes the strategy.

Program.cs
double[] vals = [2.5, 3.5, -2.5, 2.567];

foreach (var v in vals)
{
    Console.WriteLine($"{v,6}: {Math.Round(v),3} {Math.Round(v, MidpointRounding.AwayFromZero),3} {Math.Round(v, 2),6}");
}

We round the values with the default strategy, away from zero, and to two decimal places.

$ dotnet run
   2.5:   2   3    2.5
   3.5:   4   4    3.5
  -2.5:  -2  -3   -2.5
 2.567:   3   3   2.57

C# Math.Pow, Math.Sqrt, & Math.Cbrt

The Math.Pow function returns a specified number raised to the specified power. The Math.Sqrt function returns the square root of a specified number. The Math.Cbrt function returns the cube root of a specified number.

Program.cs
int x = 3;
int y = 81;

Console.WriteLine(Math.Pow(x, 5));
Console.WriteLine(Math.Sqrt(y));
Console.WriteLine(Math.Cbrt(y));
Console.WriteLine(Math.Cbrt(27));
Console.WriteLine(Math.Sqrt(-1));

In the example, we use the power, square root, and cube root functions. The square root of a negative number is not a real number; the function returns NaN.

Note that the cube root of 27 is not exactly 3. Floating point functions return the closest representable approximation; results should be rounded or compared with a tolerance.

$ dotnet run
243
9
4.326748710922225
3.0000000000000004
NaN

C# Math.Max, Math.Min, & Math.Clamp

The Math.Max and Math.Min functions return the larger and the smaller of two numbers. The Math.Clamp function restricts a value to the given range.

Program.cs
int[] vals = [-12, 5, 47, 101, 80];

foreach (var v in vals)
{
    Console.Write($"{Math.Clamp(v, 0, 100)} ");
}

Console.WriteLine();

Console.WriteLine(Math.Max(3, 7));
Console.WriteLine(Math.Min(3.5, -1.2));

We clamp the values to the range 0 to 100; for instance, a percentage cannot be negative or greater than 100.

$ dotnet run
0 5 47 100 80 
7
-1.2

C# Math.BigMul

The Math.BigMul function multiplies two 32-bit integers and returns the full 64-bit result. A plain multiplication of two int values would overflow.

Program.cs
long res = Math.BigMul(int.MaxValue, int.MaxValue);
Console.WriteLine(res);

long res2 = (long) int.MaxValue * int.MaxValue;
Console.WriteLine(res2);

int res3 = unchecked(int.MaxValue * int.MaxValue);
Console.WriteLine(res3);

The example calculates the product of two large integers. Converting one operand to long gives the same result; the plain int multiplication overflows and gives a wrong value.

$ dotnet run
4611686014132420609
4611686014132420609
1

C# Math.DivRem

The Math.DivRem function calculates both the quotient and the remainder in one call.

Program.cs
int x = 10;
int y = 3;

var (quotient, remainder) = Math.DivRem(x, y);
Console.WriteLine($"Quotient: {quotient} Remainder: {remainder}");

Console.WriteLine($"Quotient: {x / y} Remainder: {x % y}");

The DivRem overload without an out parameter returns a tuple, which we deconstruct into two variables. The same values can be computed with the / and % operators.

$ dotnet run
Quotient: 3 Remainder: 1
Quotient: 3 Remainder: 1

C# trigonometric functions

The trigonometric functions, such as Math.Sin, Math.Cos, or Math.Tan, take angles in radians. To convert degrees to radians, we multiply them by π/180.

Program.cs
int[] degrees = [0, 30, 45, 90];

foreach (var d in degrees)
{
    double rad = d * Math.PI / 180;
    Console.WriteLine($"{d,3}° sin: {Math.Sin(rad):F4} cos: {Math.Cos(rad):F4}");
}

double angle = Math.Atan2(1, 1) * 180 / Math.PI;
Console.WriteLine($"Atan2(1, 1) = {angle}°");

We print the sine and cosine of four angles. The Math.Atan2 function returns the angle of the point (x, y) from the positive x axis.

$ dotnet run
  0° sin: 0.0000 cos: 1.0000
 30° sin: 0.5000 cos: 0.8660
 45° sin: 0.7071 cos: 0.7071
 90° sin: 1.0000 cos: 0.0000
Atan2(1, 1) = 45°

Because of the floating point representation, the cosine of 90° is not exactly zero; formatting with F4 hides the tiny error.

C# logarithms and exponentials

The Math.Log function returns the natural logarithm or the logarithm in a given base. Math.Log10 and Math.Log2 compute the base 10 and base 2 logarithms. Math.Exp returns e raised to the specified power.

Program.cs
Console.WriteLine(Math.E);
Console.WriteLine(Math.Log(Math.E));
Console.WriteLine(Math.Log10(1000));
Console.WriteLine(Math.Log2(1024));
Console.WriteLine(Math.Log(81, 3));
Console.WriteLine(Math.Exp(1));

We print the Euler's number and compute several logarithms.

$ dotnet run
2.718281828459045
1
3
10
4
2.718281828459045

C# Math.PI

The number π is a mathematical constant that is the ratio of a circle's circumference to its diameter. The Math.Tau constant is equal to 2π.

Program.cs
double radius = 5;

double circumference = Math.Tau * radius;
Console.WriteLine(circumference);

double area = Math.PI * Math.Pow(radius, 2);
Console.WriteLine(area);

With the help of the Math.Tau and Math.PI constants, we compute the circumference and area of a circle.

$ dotnet run
31.41592653589793
78.53981633974483

Source

Math class

In this article we have covered common mathematical functions found in the System.Math class.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all C# tutorials.