Home
C#
Math.Abs: Absolute Value
This page was last reviewed on Oct 11, 2023.
Dot Net Perls
Math.Abs. An absolute value is not negative. It is the same as the original value but with no negative sign. With absolute values, we can avoid accessing negative indexes in arrays.
Method info. The Math.Abs method in .NET provides a tested absolute value function. It deals with certain edge cases (like already-positive numbers).
int
double
Required input, output. Consider the values -1000 and 20. When we convert them into their absolute values, we get 1000 and 20—the minus sign is eliminated.
-1000 Abs() 1000 20 Abs() 20
Example. It is usually trivial to convert variables to positive values from negative values. The Math.Abs method provides some error-checking that may be useful.
Tip The C# compiler does overload resolution based on the parameter type. It infers the appropriate method to use.
Overload
Tip 2 You could cast the variables before passing them as parameters to force the compiler to use a different overload.
Cast
Info This example shows the absolute value of a negative integer, a positive integer, a negative double and a positive double.
Result The example program prints 8 numbers. It prints the numbers, and their absolute values.
Console.WriteLine
using System; // Compute 2 absolute values. int value1 = -1000; int value2 = 20; int abs1 = Math.Abs(value1); int abs2 = Math.Abs(value2); // Write integral results. Console.WriteLine(value1); Console.WriteLine(abs1); Console.WriteLine(value2); Console.WriteLine(abs2); // End. Console.WriteLine(); // Compute 2 double absolute values. double value3 = -100.123; double value4 = 20.20; double abs3 = Math.Abs(value3); double abs4 = Math.Abs(value4); // Write double results. Console.WriteLine(value3); Console.WriteLine(abs3); Console.WriteLine(value4); Console.WriteLine(abs4);
-1000 1000 20 20 -100.123 100.123 20.2 20.2
Internals. Math.Abs() on value types (int, short, long) will use a conditional check that tests if the number if already positive. If the number is positive, it is returned unchanged.
Detail An overflow condition is checked and the unary negation operator is applied, which results in a positive number.
Tip If the overflow condition is not useful, you can enhance performance by checking for positive numbers yourself.
Notes, other types. For number types such as decimal, double and float, other implementations are called. Math.Abs is a wrapper method in these cases.
Tip For double and float, it is important to use Math.Abs instead of checking for positive yourself.
float
Discussion. From the hardware perspective, it is easier to flip the sign bit on a signed integer type. You can apply the unary minus (negation) operator.
Tip You can take the absolute value of a number that is always negative by simply using the unary negation operator.
Summary. We tested Math.Abs, an absolute value function. This method accepts decimal, double, Int16, Int32, Int64 and float types (and all keywords that are aliased to those).
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 11, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.