C# Math

Beginner
7 min read
Updated: Nov 2026
3 examples
Math

Definition and Usage

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.

The Math Methods index lists every method guide in this series.

Math never changes your variable.

Math.Sqrt(x) returns a new number and leaves x alone. To keep the answer, store it: double r = Math.Sqrt(x);.

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

Constants, Math, MathF, and decimal

The class has two constants, and there are close relatives for other number types:

NameWhat it isNotes
Math.PIPi, about 3.141592653589793A const double. No parentheses.
Math.EEuler's number, about 2.718281828459045The base of the natural logarithm Log() and of Exp().
MathMethods for double (and some for int, long, decimal)The everyday choice.
MathFThe same idea for floatLess precise, less memory. Written as MathF.Sqrt(2f).
decimalExact decimal money-style numbersOnly some Math methods accept it, such as Abs, Round, Floor, Ceiling, Truncate, Max, Min, and Sign. There is no decimal Sqrt or Sin.

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.

DegreesRadiansCode
000 * Math.PI / 180
90pi / 290 * Math.PI / 180
180piMath.PI
3602 * pi2 * 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.

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);
    }
}

How It Works

  1. No object needed. Math is a static class, so you write Math.Sqrt(25) directly. Nothing is created with new.
  2. 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.
  3. 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.
  4. 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));
    }
}

How It Works

  1. Degrees to radians. Multiplying by Math.PI / 180 turns degrees into radians. 90 degrees becomes about 1.5708 radians, which is half of pi.
  2. Sin and Cos. At 90 degrees the sine is 1 and the cosine is 0. At 0 degrees it is the other way round.
  3. 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.
  4. 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));
    }
}

How It Works

  1. NaN means "not a number". There is no real square root of -1, so Math.Sqrt(-1) returns NaN and the program keeps running.
  2. 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).
  3. Test with IsNaN. Never compare with == double.NaN, because NaN is not equal to anything, even itself. Use double.IsNaN(value).
  4. IsInfinity. double.IsInfinity is True for both positive and negative infinity, so it catches the -Infinity from Math.Log(0).

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.

Start withWhy
Sqrt()Find the square root and see what a negative input does
Round()Learn why 2.5 rounds to 2, and how to round halves up
Pow()Raise a number to a power
Clamp()Keep a value between a minimum and a maximum

Full list: C# Math Methods index (/c-sharp/math/method).

Frequently Asked Questions

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.

Browse every Math method

Open the methods index for categorized guides with examples and FAQs.

Math Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful