.NET Math Library
The .NET framework includes a class named "Math", which provides a number of standard mathematical functions, using static methods, and mathematical values, using simple constants. This article describes all of the Math class members.
An essential part of developing software is performing mathematical operations. To help, the .NET framework provides the Math class. This includes a number of standard mathematical functions and constants so that you do not need to implement or define them yourself. The functions are defined as a set of static methods, some of which work with many of the built-in numeric data types and some which limit the data types that can be used as parameters or returned from the methods.
The Math class' members can be roughly divided into the following seven categories:
- Sign Operations. Methods that detect whether a value is positive or negative, or make negative values into their positive equivalents.
- Multiplication and Division. Methods that multiply or divide values and calculate the remainder of a division.
- Rounding. Methods that convert floating point values to integers and round values to a specified number of decimal places.
- Exponential and Logarithmic Functions. Methods that work with logarithms, raise values to specified powers and find the square root of numbers.
- Trigonometry. Methods that provide trigonometric functions, which calculate the geometry of triangles and the relationships between their sides and angles.
- Comparison. Methods that compare two values to find which is larger or smaller.
- Constants. Constants that provide standard values such as Pi (π) and the natural logarithm base (e), also known as Euler's number.
The sign operations detect or modify the sign of a number. ie. Whether the value is positive or negative.
Abs calculates the absolute value of its input parameter. This is the magnitude of the number without regard to its sign. When applied to a positive number or zero, the return value is the same as the provided argument. For negative numbers, the sign is removed in the result, making a positive value. This method works with decimal, double, short, int, long, sbyte and float values.
The code below finds the distance between two integer values by subtracting one from the other and using the Abs function to ensure a positive result.
static void Main()
d = Distance(1, 10); // d = 9
d = Distance(10, 1); // d = 9
d = Distance(5, -5); // d = 10
d = Distance(-5, 5); // d = 10
d = Distance(-5, -5); // d = 0
static int Distance(int a, int b)
return Math.Abs(a - b);
Sign allows you to determine whether a value is positive, negative or zero. When applied to a positive number, the return value is 1. For negative numbers, the result is -1 and if the argument provide is zero, the result is also zero. This method works with decimal, double, short, int, long, sbyte and float values.
The code below compares two values using the Math.Sign method and subtraction. When the first parameter value is larger than the second, the result is 1. If the second value is larger the method returns -1 and if the values are identical, zero is returned. This type of comparison is used in the .NET framework when sorting data.
static void Main()
s = Compare(1, 10); // s = -1
s = Compare(10, 1); // s = 1
s = Compare(5, -5); // s = 1
s = Compare(-5, 5); // s = -1
s = Compare(-5, -5); // s = 0
static int Compare(int a, int b)
return Math.Sign(a - b);
Multiplication and Division
The multiplication and division methods of the Math class can be used to perform simple multiplication and division, as well as allowing the remainders of division operations to be calculated.
BigMul performs a multiplication of two 32-bit integer values and returns a 64-bit integer, ensuring that there is no possibility of an overflow. The method removes the need to cast one or both integers to longs before the calculation.
long max2 = Math.BigMul(int.MaxValue, int.MaxValue); max2 = 4611686014132420609
long min2 = Math.BigMul(int.MinValue, int.MinValue); min2 = 4611686018427387904
DivRem performs integer division for either 32-bit or 64-bit integer values. The first parameter accepts the dividend and the second the divisor. The quotient is returned by the method, using the same type as that used in the arguments. In addition to providing the quotient, a third, output parameter is used to return the remainder.
When working with negative numbers, there are generally two possible quotients and remainders for any calculation. For example, when dividing -10 by 3, the result could be a quotient of -3 and a remainder of -1 (-3 * 3 + 1 = 10). It could also be expressed as a quotient of -4 and a remainder of 2 (-4 * 3 - 2 = 10). The DivRem function uses the former, as the quotient is always rounded towards zero and the remainder is calculated using the modulus operator.
The following sample code shows some calculations and the generated quotient and remainder for each.
int remainder, quotient;
quotient = Math.DivRem(10, 3, out remainder); // quotient = 3, remainder = 1
quotient = Math.DivRem(10, -3, out remainder); // quotient = -3, remainder = 1
quotient = Math.DivRem(-10, 3, out remainder); // quotient = -3, remainder = -1
quotient = Math.DivRem(-10, -3, out remainder); // quotient = 3, remainder = -1
7 October 2011