In C#, Math.MaxMagnitude(x, y) looks at the size of two numbers, ignoring their signs, and returns the one with the bigger size. The returned number keeps its own sign. Math.MaxMagnitude(-8, 5) is -8, because 8 is bigger than 5.
Think of two thermometers, one reading 8 degrees below zero and one reading 5 above. Which reading is "stronger"? The -8, since it is farther from zero. A plain Max() would say 5 is larger, because it compares signed values. MaxMagnitude compares distance from zero instead. Its partner is MinMagnitude().
This is a static member of the static class System.Math. You call it with the class name, such as Math.MaxMagnitude(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. MaxMagnitude 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 with the larger absolute value, sign included.
C#
double a = Math.MaxMagnitude(-8, 5); // -8
double b = Math.MaxMagnitude(3, -7); // -7
double c = Math.MaxMagnitude(4, 4); // 4
Call
What it does
Math.MaxMagnitude(-8, 5)
Returns -8. Its size, 8, is bigger than 5.
Math.MaxMagnitude(3, -3)
Same size, so the positive one wins: 3.
Math.MaxMagnitude(double.NaN, 1)
Returns NaN.
Math.MaxMagnitude(double.NegativeInfinity, 100)
Returns -Infinity, which has the biggest size of all.
Do not mix this up with Math.Max. Max(-8, 5) is 5 (bigger value). MaxMagnitude(-8, 5) is -8 (bigger size). If both numbers have the same size, the positive one is returned. The method works on double; integers are widened to double automatically.
Library declarations
C#
public static double MaxMagnitude(double x, double y);
Parameters
Parameter
Description
x
Required. The first number to compare.
y
Required. The second number to compare.
Return Value
Return Value
Description
double
The one of x and y with the larger absolute value, with its original sign. If the sizes are equal, the positive number is returned. If either input is NaN, the result is NaN.
C# Examples
Example 1 — Which Number Is Farther from Zero?
Call MaxMagnitude with mixed signs and see that the returned number keeps its sign.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.MaxMagnitude(-8, 5));
Console.WriteLine(Math.MaxMagnitude(3, -7));
Console.WriteLine(Math.MaxMagnitude(4, 4));
Console.WriteLine(Math.MaxMagnitude(-6, -2));
}
}
Output
-8
-7
4
-6
How It Works
-8 versus 5. Ignoring signs, 8 is bigger than 5, so the answer is the original number, -8.
3 versus -7. The size 7 beats 3, and -7 keeps its minus sign.
Same number. Both are 4, so the answer is 4.
Both negative. The size 6 is bigger than 2, so -6 is returned. Here Math.Max would have said -2.
Example 2 — Find the Biggest Swing
Loop over temperature changes and keep the one with the largest size, then compare it with a plain Max.
Two trackers.biggest follows the largest size. highest follows the largest signed value. Both start at the first change, 2.
MaxMagnitude loop. It sees 2, -9, 5, -3. The size 9 is the largest, so biggest ends as -9, sign included.
Max loop. A plain Max compares signed values, so -9 loses to 5 and highest ends as 5.
Which one to use. Use MaxMagnitude when you care about the strongest change in either direction, such as the biggest error, up or down.
Example 3 — Equal Sizes, NaN, and Infinity
See which number wins a tie, and how NaN and negative infinity behave.
C#
using System;
class Program {
static void Main() {
Console.WriteLine(Math.MaxMagnitude(3, -3));
Console.WriteLine(Math.MaxMagnitude(-3, 3));
Console.WriteLine(Math.MaxMagnitude(double.NaN, 1));
Console.WriteLine(Math.MaxMagnitude(double.NegativeInfinity, 100));
}
}
Output
3
3
NaN
-Infinity
How It Works
A tie. 3 and -3 have the same size. The rule says the positive number is returned, so you get 3.
Order does not matter. Swapping the arguments gives the same answer, 3. The tie is settled by sign, not by position.
NaN. If either input is NaN, the result is NaN. Use double.IsNaN to detect it.
Infinity. Negative infinity has an endless size, bigger than 100, so it is returned with its minus sign: -Infinity.
Frequently Asked Questions
It returns whichever of two numbers has the larger absolute value, and it keeps that number's sign. Math.MaxMagnitude(-8, 5) is -8.
Max compares signed values, so Max(-8, 5) is 5. MaxMagnitude compares distance from zero, so MaxMagnitude(-8, 5) is -8.
The positive number is returned. Math.MaxMagnitude(3, -3) and Math.MaxMagnitude(-3, 3) both return 3.
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.MaxMagnitude(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.