Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Numbers and Math

The Math class

Reference for System.Math in C#: constants, rounding, powers, roots, logarithms, trigonometry, min and max, plus overflow and NaN behavior.

System.Math is a static class with the everyday mathematical functions and two constants: Math.PI and Math.E. Most methods work on double; many have overloads for float (in the sibling class MathF), decimal, int, long and other integer types. Because the class is static, you call the members directly: Math.Sqrt(2).

Floating-point methods follow IEEE 754 rules. They do not throw for impossible inputs. They return NaN (not a number) or infinity, and you check for that with double.IsNaN and double.IsInfinity. Integer methods such as Math.Abs(int.MinValue) can throw OverflowException instead.

C#
double hypotenuse = Math.Sqrt(Math.Pow(3, 2) + Math.Pow(4, 2));   // 5

Try it

C# A tour of Math

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# NaN, infinity and overflow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static class Math
{
    public const double PI = 3.14159265358979323846;
    public const double E = 2.7182818284590452354;
    public const double Tau = 6.283185307179586;

    public static double Sqrt(double d);
    public static double Pow(double x, double y);
    public static double Abs(double value);          // overloads for all numeric types
    public static double Round(double a);
    public static double Floor(double d);
    public static double Ceiling(double a);
    public static double Log(double d);
    public static double Sin(double a);
    public static double Max(double val1, double val2);   // and overloads for every numeric type
    public static int Clamp(int value, int min, int max);
}
Group Members
Constants PI, E, Tau
Rounding Round, Floor, Ceiling, Truncate
Sign and size Abs, Sign, Min, Max, MinMagnitude, MaxMagnitude, Clamp
Powers and roots Pow, Sqrt, Cbrt, ScaleB, ReciprocalSqrtEstimate
Logs and exponentials Exp, Log, Log2, Log10, ILogB
Trigonometry Sin, Cos, Tan, Asin, Acos, Atan, Atan2, Sinh, Cosh, Tanh, SinCos
Integer arithmetic DivRem, BigMul, FusedMultiplyAdd (floating point)

Returns: the mathematical result as the same numeric type as the argument (double for double arguments).

Errors / undefined behavior: floating-point methods return NaN for domain errors (Sqrt(-1), Log(-1), Asin(2)), -Infinity for Log(0) and +Infinity on overflow. Integer overloads throw OverflowException (Abs(int.MinValue)) or DivideByZeroException (DivRem). Passing NaN generally returns NaN.

Usage notes

  • Angles are in radians. Math.Sin(Math.PI / 2) is 1. Convert with degrees * Math.PI / 180, or use double.DegreesToRadians (.NET 7+).
  • Pow is a floating-point function. It returns double, so Math.Pow(3, 39) loses precision beyond 2^53 and casting to long may give a different value than the exact integer power (the second example shows it). For exact integers, multiply in a loop or use BigInteger.Pow.
  • Rounding methods differ. Floor goes toward negative infinity, Ceiling toward positive infinity, Truncate toward zero, and Round to the nearest value with ties going to even by default. See Math.Round().
  • Never compare floats with == after math. Use a tolerance; see Comparing floating-point values.
  • NaN is contagious. Any arithmetic with NaN yields NaN, and NaN == NaN is false. Test with double.IsNaN.
  • decimal has fewer functions. Math offers Abs, Floor, Ceiling, Round, Truncate, Min, Max, Sign and Clamp for decimal, but no Sqrt, Pow or trigonometry. Convert to double for those, accepting lost precision.
  • MathF and generic math. MathF mirrors Math for float. Generic math interfaces (double.Sqrt, double.Hypot, int.Abs) expose the same operations as static members on the types.
  • Not random, not thread-hostile. All methods are pure functions and thread-safe. For random numbers see Random.
  • Results can differ in the last digits. Transcendental functions (Sin, Exp, Pow) depend on the platform's math library, so avoid printing all 17 digits in tests that must match across machines.