Skip to content
Tutorials Center
My learning

Topics

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.

C#
int distance = Math.Abs(3 - 10);    // 7
int direction = Math.Sign(-42);     // -1

Try it

C# Absolute value and sign

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.

C# Overflow and NaN

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

C#
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.MinValue trap. Two's complement integers have one more negative value than positive ones. Widen to long before calling Abs when the input could be the minimum, or validate beforehand.
  • Sign is not a comparison with zero for floats. Math.Sign(-0.0) is 0, and Math.Sign(NaN) throws. If NaN is possible, test it first with double.IsNaN.
  • Tolerance checks. Math.Abs(a - b) < epsilon is 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 be int.MinValue, which makes Abs throw. Mask the sign bit with & int.MaxValue or use uint arithmetic.
  • Unsigned differences. For uint values, Math.Abs(a - b) does not compile and a - b wraps around. Compare first and subtract the smaller, or cast to long.
  • Negative zero. Math.Abs(-0.0) is 0 (positive zero). The sign bit is cleared, which is occasionally useful to normalize output.
  • Generic math. int.Abs, double.Abs, T.Sign and T.IsNegative give the same results and work inside generic methods constrained to INumber<T>. T.CopySign transfers the sign of one number to another.
  • Performance. Abs on floating-point types compiles to a bit mask. The integer versions include an overflow check that costs a single compare.