Complex.Abs Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

Syntax

'Declaration
Public Shared Function Abs ( _
    value As Complex _
) As Double
public static double Abs(
    Complex value
)

Parameters

Return Value

Type: System.Double
The absolute value of value.

Remarks

The absolute value of a complex number is equivalent to its Magnitude property. The absolute value of a real number a + bi is calculated as follows:

  • If b = 0, the result is 0.

  • If a > b, the result is a *Math.Sqrt(1 + b2/a2).

  • If b > a, the result is b * Math.Sqrt(1 + a2/b2).

If the calculation of the absolute value results in an overflow, the method returns either Double.PositiveInfinity or Double.NegativeInfinity. If either the Real or Imaginary property is Double.NaN and the other property is neither Double.PositiveInfinity nor Double.NegativeInfinity, the method returns Double.NaN.

Examples

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

Imports System.Numerics

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim complex1 As New Complex(2.0, 3.0)
      outputBlock.Text += String.Format("|{0}| = {1:N2}", complex1, Complex.Abs(complex1)) & vbCrLf
      outputBlock.Text += String.Format("Equal to Magnitude: {0}",  
                        Complex.Abs(complex1).Equals(complex1.Magnitude)) & vbCrLf
   End Sub
End Module
' The example displays the following output:
'       |(2, 3)| = 3.61
'       Equal to Magnitude: True
using System;
using System.Numerics;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Complex complex1 = new Complex(2.0, 3.0);
      outputBlock.Text += String.Format("|{0}| = {1:N2}", complex1, Complex.Abs(complex1)) + "\n";
      outputBlock.Text += String.Format("Equal to Magnitude: {0}",
                        Complex.Abs(complex1).Equals(complex1.Magnitude)) + "\n";
   }
}
// The example displays the following output:
//       |(2, 3)| = 3.60555127546399
//       Equal to Magnitude: True

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.