C# Math Methods

About This Index
This page is the C# Math Methods index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs. The examples use small numbers whose answers are exact, such as Math.Sqrt(16) or Math.Pow(2, 10), and they round or format any other value, so the output you see is the output you get.
Learn what the Math class is for on /c-sharp/math. Use this page when you need a specific API such as Sqrt(), Round(), or Pow().
Math.Sin, Math.Cos, and Math.Tan expect radians. Convert degrees with degrees * Math.PI / 180. Also remember that a double can hold NaN and Infinity, which is how many Math methods report a bad input instead of throwing an exception.
How to Call a Math Method
Every Math method is static, so you call it on the class name. There is nothing to create first:
using System;
double root = Math.Sqrt(16); // 4
double power = Math.Pow(2, 10); // 1024
double rounded = Math.Round(3.14159, 2); // 3.14
int bigger = Math.Max(7, 12); // 12
int size = Math.Abs(-5); // 5
double pi = Math.PI; // 3.141592653589793 (a constant, no parentheses) C# Math Methods Table
All method guides in this C# tutorial series (43 tutorials), grouped by purpose.
Basic
7 methodsEveryday helpers: size of a number, its sign, the bigger or smaller of two numbers, and keeping a value inside a range.
| Method | Description | Tutorial |
|---|---|---|
Abs() | Return the absolute value of a number: its distance from zero, never negative. | Try it |
Sign() | Return -1, 0, or 1 to tell whether a number is negative, zero, or positive. | Try it |
Max() | Return the larger of two numbers. | Try it |
Min() | Return the smaller of two numbers. | Try it |
Clamp() | Keep a value between a minimum and a maximum. | Try it |
MaxMagnitude() | Return whichever of two numbers is farther from zero, keeping its sign. | Try it |
MinMagnitude() | Return whichever of two numbers is closer to zero, keeping its sign. | Try it |
Rounding
4 methodsTurn a number with decimals into a whole number, or round to a chosen number of decimal places.
| Method | Description | Tutorial |
|---|---|---|
Round() | Round a number to the nearest whole number or to a chosen number of decimal places. | Try it |
Floor() | Round a number down to the nearest whole number, toward negative infinity. | Try it |
Ceiling() | Round a number up to the nearest whole number, toward positive infinity. | Try it |
Truncate() | Drop the decimal part of a number, always moving toward zero. | Try it |
Powers and roots
5 methodsRaise to a power, take roots, use e to a power, or multiply by a power of two.
| Method | Description | Tutorial |
|---|---|---|
Pow() | Raise a number to a power: Pow(2, 10) is 1024. | Try it |
Sqrt() | Return the square root of a number: Sqrt(16) is 4. | Try it |
Cbrt() | Return the cube root of a number, including negative numbers. | Try it |
Exp() | Return e raised to a power, where e is about 2.71828. | Try it |
ScaleB() | Multiply a number by 2 raised to an int power, quickly and exactly. | Try it |
Logarithms
4 methodsFind the power a base must be raised to, using base e, base 10, base 2, or any base. ILogB returns the binary exponent as an int.
| Method | Description | Tutorial |
|---|---|---|
Log() | Return the natural logarithm of a number, or the logarithm in any base you choose. | Try it |
Log10() | Return the base-10 logarithm: how many times you multiply 10 to reach a number. | Try it |
Log2() | Return the base-2 logarithm: how many times you double 1 to reach a number. | Try it |
ILogB() | Return the binary exponent of a number as an int: the power of two just below it. | Try it |
Trigonometry
8 methodsSine, cosine, tangent, and their inverses. All angles are in radians, not degrees.
| Method | Description | Tutorial |
|---|---|---|
Sin() | Return the sine of an angle given in radians. | Try it |
Cos() | Return the cosine of an angle given in radians. | Try it |
Tan() | Return the tangent of an angle given in radians. | Try it |
SinCos() | Return the sine and cosine of the same angle together, as a tuple. | Try it |
Asin() | Return the angle (in radians) whose sine is a given number. | Try it |
Acos() | Return the angle (in radians) whose cosine is a given number. | Try it |
Atan() | Return the angle (in radians) whose tangent is a given number. | Try it |
Atan2() | Return the angle (in radians) of the point (x, y), using both signs to find the right quarter of the circle. | Try it |
Hyperbolic
6 methodsHyperbolic sine, cosine, tangent, and their inverses, built from the exponential function.
| Method | Description | Tutorial |
|---|---|---|
Sinh() | Return the hyperbolic sine of a number: (e to the x minus e to the minus x) divided by 2. | Try it |
Cosh() | Return the hyperbolic cosine of a number: (e to the x plus e to the minus x) divided by 2. | Try it |
Tanh() | Return the hyperbolic tangent of a number, a smooth value squeezed between -1 and 1. | Try it |
Asinh() | Return the inverse hyperbolic sine: the number whose Sinh is the input. | Try it |
Acosh() | Return the inverse hyperbolic cosine: the non-negative number whose Cosh is the input. | Try it |
Atanh() | Return the inverse hyperbolic tangent for inputs between -1 and 1. | Try it |
Integer and division
3 methodsQuotient and remainder together, a full-size product of two integers, and the IEEE remainder.
| Method | Description | Tutorial |
|---|---|---|
DivRem() | Get the quotient and the remainder of a whole-number division in one call. | Try it |
BigMul() | Multiply two integers and keep the full answer, even when it is too big for the original type. | Try it |
IEEERemainder() | Return x minus y times the nearest whole number of divisions, so the result can be negative. | Try it |
Floating-point details
6 methodsAdvanced tools that work on the bits of a double: the next number up or down, copying a sign, one-step multiply-add, and fast estimates.
| Method | Description | Tutorial |
|---|---|---|
BitIncrement() | Return the very next double above a number: the smallest step up that a double can show. | Try it |
BitDecrement() | Return the very next double below a number: the smallest step down that a double can show. | Try it |
CopySign() | Return the size of one number with the sign of another number. | Try it |
FusedMultiplyAdd() | Compute x times y plus z with only one rounding step, for a more accurate answer. | Try it |
ReciprocalEstimate() | Return a quick estimate of 1 divided by a number. It is only approximate. | Try it |
ReciprocalSqrtEstimate() | Return a quick estimate of 1 divided by the square root of a number. It is only approximate. | Try it |
Frequently Asked Questions
Start with Abs()
Learn the simplest Math method first: drop the minus sign from a number.
12 people found this page helpful
