C# Reference ยท Numbers and Math
Math.Abs() and Math.Sign() methods
Reference for Math.Abs and Math.Sign in C#: absolute value for every numeric type, the int.MinValue overflow, NaN behavior and checking the sign of a number.
Math.Abs(x) returns the absolute value of x: its distance from zero, without the sign. Math.Abs(-7) is 7, and Math.Abs(7) is 7. Math.Sign(x) tells you which side of zero a number is on: it returns -1 for negative numbers, 0 for zero and 1 for positive numbers.
Typical uses are distances (Math.Abs(a - b)), tolerance checks for floating-point comparison, and direction logic where you only care whether a value is negative, zero or positive. Both methods have overloads for all signed numeric types.
int distance = Math.Abs(3 - 10); // 7 int direction = Math.Sign(-42); // -1
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 int Abs(int value); public static long Abs(long value); public static double Abs(double value); public static decimal Abs(decimal value); // also sbyte, short, float, nint public static int Sign(int value); public static int Sign(long value); public static int Sign(double value); public static int Sign(decimal value); // also sbyte, short, float, nint
| Name | Description |
|---|---|
value |
The number to examine. There are no overloads for unsigned types, because they are never negative. |
Returns: Abs returns a non-negative number of the same type as the argument. Sign always returns an int: -1, 0 or 1.
Errors / undefined behavior: Math.Abs of the most negative value of an integer type (int.MinValue, long.MinValue, and so on) throws OverflowException, because the positive result is one larger than the type's maximum. Math.Sign(double.NaN) throws ArithmeticException. Math.Abs(double.NaN) is NaN, and Abs of either infinity is PositiveInfinity.
Usage notes
- The
int.MinValuetrap. Two's complement integers have one more negative value than positive ones. Widen tolongbefore callingAbswhen the input could be the minimum, or validate beforehand. - Sign is not a comparison with zero for floats.
Math.Sign(-0.0)is0, andMath.Sign(NaN)throws. If NaN is possible, test it first withdouble.IsNaN. - Tolerance checks.
Math.Abs(a - b) < epsilonis the usual float comparison. Choose an epsilon that fits your magnitudes; for large values scale it relative to them. See Comparing floating-point values. - Hash codes and indexes. Do not use
Math.Abs(obj.GetHashCode())to get a non-negative index. The hash can beint.MinValue, which makesAbsthrow. Mask the sign bit with& int.MaxValueor useuintarithmetic. - Unsigned differences. For
uintvalues,Math.Abs(a - b)does not compile anda - bwraps around. Compare first and subtract the smaller, or cast tolong. - Negative zero.
Math.Abs(-0.0)is0(positive zero). The sign bit is cleared, which is occasionally useful to normalize output. - Generic math.
int.Abs,double.Abs,T.SignandT.IsNegativegive the same results and work inside generic methods constrained toINumber<T>.T.CopySigntransfers the sign of one number to another. - Performance.
Abson floating-point types compiles to a bit mask. The integer versions include an overflow check that costs a single compare.