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.
double hypotenuse = Math.Sqrt(Math.Pow(3, 2) + Math.Pow(4, 2)); // 5
Try it
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.
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
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)is1. Convert withdegrees * Math.PI / 180, or usedouble.DegreesToRadians(.NET 7+). Powis a floating-point function. It returnsdouble, soMath.Pow(3, 39)loses precision beyond 2^53 and casting tolongmay give a different value than the exact integer power (the second example shows it). For exact integers, multiply in a loop or useBigInteger.Pow.- Rounding methods differ.
Floorgoes toward negative infinity,Ceilingtoward positive infinity,Truncatetoward zero, andRoundto the nearest value with ties going to even by default. SeeMath.Round(). - Never compare floats with
==after math. Use a tolerance; see Comparing floating-point values. NaNis contagious. Any arithmetic withNaNyieldsNaN, andNaN == NaNisfalse. Test withdouble.IsNaN.decimalhas fewer functions.MathoffersAbs,Floor,Ceiling,Round,Truncate,Min,Max,SignandClampfordecimal, but noSqrt,Powor trigonometry. Convert todoublefor those, accepting lost precision.MathFand generic math.MathFmirrorsMathforfloat. 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.