This topic has not yet been rated - Rate this topic

Complex.Magnitude Property

Gets the magnitude (or absolute value) of a complex number.

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)
public double Magnitude { get; }

Property Value

Type: System.Double
The magnitude of the current instance.

The Magnitude property is equivalent to the absolute value of a complex number. It specifies the distance from the origin (the intersection of the x-axis and the y-axis in the Cartesian coordinate system) to the two-dimensional point represented by a complex number. The absolute value is calculated as follows:

| a + bi | = Math.Sqrt (a2 + b2)

If the calculation of the absolute value results in an overflow, this property returns either Double.PositiveInfinity or Double.NegativeInfinity.

The Magnitude and the Phase properties define the position of a point that represents a complex number in the polar coordinate system.

You can instantiate a complex number based on its polar coordinates instead of its Cartesian coordinates by calling the FromPolarCoordinates method.

The following example calculates the absolute value of a complex number and demonstrates that it is equivalent to the value of the Magnitude property.


using System;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      Complex complex1 = new Complex(2.0, 3.0);
      Console.WriteLine("|{0}| = {1:N2}", complex1, Complex.Abs(complex1));
      Console.WriteLine("Equal to Magnitude: {0}", 
                        Complex.Abs(complex1).Equals(complex1.Magnitude)); 
   }
}
// The example displays the following output:
//       |(2, 3)| = 3.60555127546399
//       Equal to Magnitude: True


.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
We can't avoid all numerical nastiness...
But yet I see an opportunity for improvement. a^2 + b^2 will result in possibly needless overflow if a and b are big. A numerical analyst would observe:
  a^2 + b^2 = max(|a|,|b|)^2 * (1 + (min(|a|,|b|)/max(|a|,|b|))^2)
So for z=a+bi, i = principal sqrt(-1), |z|=sqrt(a^2+b^2) of course but also
  |z| = max(|a|,|b|) * sqrt(1 + (min(|a|,|b|)/max(|a|,|b|))^2)

When |a| = |b|, this simplifies to |z| = |a|*sqrt(2) = |b|*sqrt(2)

When a = b = 0, |z| = 0. This should definitely be checked before doing the computation, or else this computation will divide by zero!

This is nice, since it will gracefully round toward 0 if |a| and |b| are small. Yet if |a| and |b| are large but not floating-point infinity, this computation will work if multiplying max(|a|,|b|) by a number no larger than sqrt(2) won't cause overflow!

EDIT 8/3/2011. Instead of max(a,b) or min(a,b), I realized they should be max(|a|,|b|) and min(|a|, |b|), respectively. I also failed to mention the case of complex 0 having magnitude 0 will fail in my formula because it would cause a divide by 0 error.