In C#, System.Math is a static class full of ready-made number tools. Think of the extra buttons on a calculator: square root, power, round, sine, logarithm. You press one by writing its name after Math., for example Math.Sqrt(16), Math.Pow(2, 10), or Math.Round(3.7).
Because the class is static, you never write new Math(). Add using System; at the top of the file and call the methods directly. Most methods take and return a double. A few (like Abs(), Max(), and Min()) also have versions for int, long, and other number types.
Math.Sqrt(x) returns a new number and leaves x alone. To keep the answer, store it: double r = Math.Sqrt(x);.
Syntax
C# Syntax
Common first steps:
C#
using System;
double root = Math.Sqrt(81); // 9
double cube = Math.Pow(3, 3); // 27
double down = Math.Floor(2.9); // 2
double up = Math.Ceiling(2.1); // 3
int small = Math.Min(4, 9); // 4
double circle = Math.PI * 5 * 5; // area of a circle with radius 5
Reference
Constants, Math, MathF, and decimal
The class has two constants, and there are close relatives for other number types:
Name
What it is
Notes
Math.PI
Pi, about 3.141592653589793
A const double. No parentheses.
Math.E
Euler's number, about 2.718281828459045
The base of the natural logarithm Log() and of Exp().
Math
Methods for double (and some for int, long, decimal)
The everyday choice.
MathF
The same idea for float
Less precise, less memory. Written as MathF.Sqrt(2f).
decimal
Exact decimal money-style numbers
Only some Math methods accept it, such as Abs, Round, Floor, Ceiling, Truncate, Max, Min, and Sign. There is no decimal Sqrt or Sin.
Reminder
Angles Are in Radians
A full circle is 360 degrees, which is 2 * Math.PI radians. The angle methods (Sin(), Cos(), Tan(), Atan2()) use radians only.
Degrees
Radians
Code
0
0
0 * Math.PI / 180
90
pi / 2
90 * Math.PI / 180
180
pi
Math.PI
360
2 * pi
2 * Math.PI
Printing decimals.
Console.WriteLine uses the computer's culture, so a value such as 2.5 may show as 2,5 on some systems. The examples here print whole numbers or round first, so they look the same everywhere.
Code
C# Examples
Example 1 — Square Root, Power, Round, and Max
A first program that calls four everyday Math methods and prints each answer.
C#
using System;
class Program {
static void Main() {
double root = Math.Sqrt(25);
double power = Math.Pow(2, 10);
double rounded = Math.Round(3.7);
int bigger = Math.Max(10, 20);
Console.WriteLine(root);
Console.WriteLine(power);
Console.WriteLine(rounded);
Console.WriteLine(bigger);
}
}
Output
5
1024
4
20
How It Works
No object needed.Math is a static class, so you write Math.Sqrt(25) directly. Nothing is created with new.
Square root and power. The square root of 25 is 5, and 2 to the power 10 is 1024. Both methods work on double numbers and return a double.
Round to a whole number.Math.Round(3.7) gives 4, because 3.7 is closer to 4 than to 3. The answer is still a double, so a whole number prints without a decimal part.
Max works on ints too.Math.Max(10, 20) has an int version, so it returns the int 20, not a double.
Example 2 — Convert Degrees to Radians
Trigonometry methods want radians. Convert 90 and 0 degrees first, then call Sin and Cos, rounding the answers to 4 decimals.
C#
using System;
class Program {
static void Main() {
double ninety = 90 * Math.PI / 180;
double zero = 0 * Math.PI / 180;
Console.WriteLine(Math.Round(Math.Sin(ninety), 4));
Console.WriteLine(Math.Round(Math.Cos(ninety), 4));
Console.WriteLine(Math.Round(Math.Sin(zero), 4));
Console.WriteLine(Math.Round(Math.Cos(zero), 4));
}
}
Output
1
0
0
1
How It Works
Degrees to radians. Multiplying by Math.PI / 180 turns degrees into radians. 90 degrees becomes about 1.5708 radians, which is half of pi.
Sin and Cos. At 90 degrees the sine is 1 and the cosine is 0. At 0 degrees it is the other way round.
Why Round. Pi cannot be stored exactly, so Math.Cos(ninety) is a tiny number near 0.00000000000000006, not a clean 0. Math.Round(..., 4) removes that noise.
Plain output. After rounding, the values 1 and 0 are whole numbers, so they print as 1 and 0.
Example 3 — Bad Input Gives NaN or Infinity
Many Math methods do not throw an exception for bad numbers. They return NaN or Infinity, and you can test for them.
C#
using System;
class Program {
static void Main() {
double root = Math.Sqrt(-1);
double log = Math.Log(0);
double divide = 1.0 / 0;
Console.WriteLine(root);
Console.WriteLine(log);
Console.WriteLine(divide);
Console.WriteLine(double.IsNaN(root));
Console.WriteLine(double.IsInfinity(log));
}
}
Output
NaN
-Infinity
Infinity
True
True
How It Works
NaN means "not a number". There is no real square root of -1, so Math.Sqrt(-1) returns NaN and the program keeps running.
Infinity. The logarithm of 0 is minus infinity, and dividing a double by 0 gives infinity. Doubles do not throw a divide-by-zero error (integers do).
Test with IsNaN. Never compare with == double.NaN, because NaN is not equal to anything, even itself. Use double.IsNaN(value).
IsInfinity.double.IsInfinity is True for both positive and negative infinity, so it catches the -Infinity from Math.Log(0).
Continue
Next: C# Math Methods Index
After you know what the Math class is for, open the method index for a full tutorial on each Math API.
Math is a static class in the System namespace. It holds methods such as Abs, Sqrt, Pow, Round, Floor, Sin, and Log, and constants such as Math.PI and Math.E. You call them with the class name, like Math.Sqrt(16).
Radians. Methods like Sin, Cos, and Tan expect an angle in radians. Convert degrees with degrees * Math.PI / 180, and convert back with radians * 180 / Math.PI.
Math works with double (and has a few int, long, and decimal versions). MathF is a matching static class for float, which has less precision but uses less memory. For everyday code, use Math.
Floating-point numbers follow the IEEE 754 rules, which have special values: NaN (not a number) and Infinity. Methods return them for impossible results instead of throwing. Use double.IsNaN(value) to check.
Open the C# Math Methods index. It lists Abs, Sign, Max, Min, Clamp, the rounding methods, powers and roots, logarithms, trigonometry, hyperbolic methods, DivRem, BigMul, and the advanced floating-point helpers.