C# Math MinMagnitude() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.MinMagnitude(x, y) compares the size of two numbers, ignoring signs, and returns the one with the smaller size. The result keeps its own sign. Math.MinMagnitude(-8, 5) is 5, because 5 is closer to zero than 8.

Think of two darts: one landed 8 cm to the left of the bullseye, one 5 cm to the right. The closer dart is the 5. A plain Min() would say -8 is smaller, because it compares signed values. MinMagnitude compares distance from zero. Its partner is MaxMagnitude().

This is a static member of the static class System.Math. You call it with the class name, such as Math.MinMagnitude(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. MinMagnitude was added in .NET Core 3.0 for double. It is not in classic .NET Framework.

Syntax

Pass two double numbers. You get back the one closer to zero, sign included.

C#
double a = Math.MinMagnitude(-8, 5);   // 5
double b = Math.MinMagnitude(3, -7);   // 3
double c = Math.MinMagnitude(-2, 9);   // -2
CallWhat it does
Math.MinMagnitude(-8, 5)Returns 5. Its size, 5, is smaller than 8.
Math.MinMagnitude(3, -3)Same size, so the negative one is returned: -3.
Math.MinMagnitude(double.NaN, 1)Returns NaN.
Math.MinMagnitude(double.PositiveInfinity, 100)Returns 100. Infinity is as far from zero as possible.

Do not mix this up with Math.Min. Min(-8, 5) is -8 (smaller value). MinMagnitude(-8, 5) is 5 (smaller size). On a tie, the negative number is returned, which is the opposite of MaxMagnitude. Integers are widened to double automatically.

Library declarations
C#
public static double MinMagnitude(double x, double y);

Parameters

ParameterDescription
xRequired. The first number to compare.
yRequired. The second number to compare.

Return Value

Return ValueDescription
doubleThe one of x and y with the smaller absolute value, with its original sign. If the sizes are equal, the negative number is returned. If either input is NaN, the result is NaN.

C# Examples

Example 1 — Which Number Is Closer to Zero?

Call MinMagnitude with mixed signs and see that the returned number keeps its sign.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.MinMagnitude(-8, 5));
        Console.WriteLine(Math.MinMagnitude(3, -7));
        Console.WriteLine(Math.MinMagnitude(-2, 9));
        Console.WriteLine(Math.MinMagnitude(0, -4));
    }
}

How It Works

  1. -8 versus 5. Ignoring signs, 5 is smaller than 8, so the answer is 5.
  2. 3 versus -7. The size 3 is smaller than 7, so you get 3.
  3. -2 versus 9. The size 2 is smaller than 9, and -2 keeps its minus sign.
  4. Zero. Zero has the smallest possible size, so 0 beats -4.

Example 2 — Find the Smallest Error

Loop over measurement errors, keep the one closest to zero, and compare it with a plain Min.

C#
using System;

class Program {
    static void Main() {
        double[] errors = { -4, 7, -2, 9 };
        double closest = errors[0];
        double lowest = errors[0];

        foreach (double error in errors) {
            closest = Math.MinMagnitude(closest, error);
            lowest = Math.Min(lowest, error);
        }

        Console.WriteLine(closest);
        Console.WriteLine(lowest);
    }
}

How It Works

  1. Two trackers. closest follows the smallest size. lowest follows the smallest signed value. Both start at -4.
  2. MinMagnitude loop. The sizes are 4, 7, 2, and 9. The smallest size is 2, so closest ends as -2.
  3. Min loop. A plain Min compares signed values, so it keeps -4, the lowest number in the list.
  4. Which one to use. When a measurement error is the question, the best reading is the one nearest zero, no matter which side it is on. That is the job of MinMagnitude.

Example 3 — Equal Sizes, NaN, and Infinity

See which number wins a tie, and how NaN and infinity behave.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.MinMagnitude(3, -3));
        Console.WriteLine(Math.MinMagnitude(-3, 3));
        Console.WriteLine(Math.MinMagnitude(double.NaN, 1));
        Console.WriteLine(Math.MinMagnitude(double.PositiveInfinity, 100));
    }
}

How It Works

  1. A tie. 3 and -3 have the same size. The rule says the negative number is returned, so you get -3.
  2. Order does not matter. Swapping the arguments gives -3 again. The tie is settled by sign, not by position.
  3. NaN. If either input is NaN, the result is NaN.
  4. Infinity. Positive infinity is as far from zero as a number can get, so 100 is the one closer to zero and is returned.

Frequently Asked Questions

It returns whichever of two numbers has the smaller absolute value, and it keeps that number's sign. Math.MinMagnitude(-8, 5) is 5.
Min compares signed values, so Min(-8, 5) is -8. MinMagnitude compares distance from zero, so MinMagnitude(-8, 5) is 5.
The negative number is returned. Math.MinMagnitude(3, -3) and Math.MinMagnitude(-3, 3) both return -3. This is the reverse of MaxMagnitude, where the positive number wins.
It was added in .NET Core 3.0, so it is in modern .NET. It is not in classic .NET Framework, where you can compare Math.Abs values yourself.
No. Math is a static class, so you call Math.MinMagnitude(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: Pow()

Raise a number to a power: Pow(2, 10) is 1024.

Pow() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful