C# Math.MaxMagnitude() – Examples

In this tutorial, we will learn about the C# Math.MaxMagnitude() method, and learn how to use this method to get the maximum of two given numbers based on magnitude, with the help of examples.

MaxMagnitude(Double, Double)

Math.MaxMagnitude(x, y) returns the larger magnitude of two double-precision floating-point numbers x, y.

ADVERTISEMENT

Syntax

The syntax of MaxMagnitude() method is

Math.MaxMagnitude(Double x, Double y)

where

ParameterDescription
xThe first of two double-precision floating-point numbers to compare.
yThe second of two double-precision floating-point numbers to compare.

Return Value

The method returns Double value, x or y.

Example 1 – MaxMagnitude(x, y)

In this example, we will

C# Program

using System;
 
class Example {
    static void Main(string[] args) {
        Double x, y, result;

        x = 10;
        y = 4;
        result = Math.MaxMagnitude(x, y);
        Console.WriteLine($"MaxMagnitude({x}, {y}) = {result}");

        x = 79;
        y = -853;
        result = Math.MaxMagnitude(x, y);
        Console.WriteLine($"MaxMagnitude({x}, {y}) = {result}");

        x = 10;
        y = 10;
        result = Math.MaxMagnitude(x, y);
        Console.WriteLine($"MaxMagnitude({x}, {y}) = {result}");

        x = 10;
        y = -10;
        result = Math.MaxMagnitude(x, y);
        Console.WriteLine($"MaxMagnitude({x}, {y}) = {result}");
    }
}

Output

MaxMagnitude(10, 4) = 10
MaxMagnitude(79, -853) = -853
MaxMagnitude(10, 10) = 10

MaxMagnitude(10, 4) returns 10, since magnitude of 10 is greater than that of 4.

MaxMagnitude(79, -853) returns -853, since magnitude of -853 is greater than that of 79.

MaxMagnitude(10, 10) returns 10, since both the arguments are same, and it does not matter which the method returns.

Conclusion

In this C# Tutorial, we have learnt the syntax of C# Math.MaxMagnitude() method, and also learnt how to use this method with the help of C# example programs.