Complex.Equals 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳一個表示兩個複數是否相等的值。
多載
| 名稱 | Description |
|---|---|
| Equals(Object) |
回傳一個值,表示目前實例與指定物件的值是否相同。 |
| Equals(Complex) |
回傳一個值,表示當前實例與指定的複數值是否相同。 |
Equals(Object)
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- Complex.cs
回傳一個值,表示目前實例與指定物件的值是否相同。
public:
override bool Equals(System::Object ^ obj);
public override bool Equals(object obj);
public override bool Equals(object? obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean
參數
- obj
- Object
要比較的物件。
傳回
true若參數obj為物件或類型,且Complex其值等於當前Complex物件;否則, Complexfalse 。
備註
若兩個複數的實部相等,虛部相等,則稱複數相等。 此 Equals(Object) 方法等價於以下表達式:
return this.Real.Equals(((Complex) value).Real) &&
this.Imaginary.Equals(((Complex) value).Imaginary);
this.Real.Equals((value :?> Complex).Real)
&& this.Imaginary.Equals((value :?> Complex).Imaginary)
Return Me.Real.Equals(CType(value, Complex).Real) AndAlso
Me.Imaginary.Equals(CType(value, Complex).Imaginary)
若參數 obj 不是 Complex 物件,而是定義隱含轉換的資料型態, Equals(Object) 方法會 obj 先轉換成 Complex 實部等於 的 obj 值、虛部為零的物件,然後再進行比較。 以下範例說明,透過發現複數與雙精度浮點數值相等來說明此點。
double n1 = 16.33;
System.Numerics.Complex c1 =
new System.Numerics.Complex(16.33, 0);
Console.WriteLine(c1.Equals(n1)); // Returns true.
let n1 = 16.33;
let c1 = System.Numerics.Complex(16.33, 0)
printfn $"{c1.Equals n1}" // Returns true.
Dim n1 As Double = 16.33
Dim c1 As New System.Numerics.Complex(16.33, 0)
Console.WriteLine(c1.Equals(n1)) ' Returns True.
給呼叫者的注意事項
使用此 Equals(Complex) 方法時需謹慎,因為兩個看似等價的值,可能因其實數與虛數分量的精度不同而被視為不相等。 若 obj 必須先轉換為 a Double ,則可加劇問題。 以下範例比較一個複數,其實分量似乎等 Single 於該 Single 值的值。 如輸出所示,等式比較結果為 False。
using System;
using System.Numerics;
public class Example
{
public static void Main()
{
float n1 = 1.430718e-12f;
Complex c1 = new Complex(1.430718e-12, 0);
Console.WriteLine("{0} = {1}: {2}", c1, n1, c1.Equals(n1));
}
}
// The example displays the following output:
// (1.430718E-12, 0) = 1.430718E-12: False
open System.Numerics
let n1 = 1.430718e-12f
let c1 = Complex(1.430718e-12, 0);
printfn $"{c1} = {n1}: {c1.Equals n1}"
// The example displays the following output:
// (1.430718E-12, 0) = 1.430718E-12: False
Imports System.Numerics
Module Example
Public Sub Main()
Dim n1 As Single = 1.430718e-12
Dim c1 As New Complex(1.430718e-12, 0)
Console.WriteLine("{0} = {1}: {2}", c1, n1, c1.Equals(n1))
End Sub
End Module
' The example displays the following output:
' (1.430718E-12, 0) = 1.430718E-12: False
一種推薦的方法是定義兩個值之間的可接受差值(例如其中一方實數與虛數分量的0.01%),而非比較兩者的相等值。 如果兩個值之間的絕對值小於或等於該邊界,則差異很可能是因為精度差異,因此兩值很可能相等。 以下範例使用此技術,比較前一個範例中發現不相等的兩個值。 現在它認為兩者是平等的。
using System.Numerics;
public class Example
{
public static void Main()
{
float n1 = 1.430718e-12f;
Complex c1 = new Complex(1.430718e-12, 0);
double difference = .0001;
// Compare the values
bool result = (Math.Abs(c1.Real - n1) <= c1.Real * difference) &
c1.Imaginary == 0;
Console.WriteLine("{0} = {1}: {2}", c1, n1, result);
}
}
// The example displays the following output:
// (1.430718E-12, 0) = 1.430718E-12: True
open System.Numerics
let n1 = 1.430718e-12f
let c1 = Complex(1.430718e-12, 0);
let difference = 0.0001f;
// Compare the values
let result = (abs (c1.Real - float n1) <= c1.Real * float difference) && c1.Imaginary = 0;
printfn $"{c1} = {n1}: {result}"
// The example displays the following output:
// (1.430718E-12, 0) = 1.430718E-12: True
Imports System.Numerics
Module Example
Public Sub Main()
Dim n1 As Single = 1.430718e-12
Dim c1 As New Complex(1.430718e-12, 0)
Dim difference As Double = .0001
' Compare the values
Dim result As Boolean = (Math.Abs(c1.Real - n1) <= c1.Real * difference) And
c1.Imaginary = 0
Console.WriteLine("{0} = {1}: {2}", c1, n1, result)
End Sub
End Module
' The example displays the following output:
' (1.430718E-12, 0) = 1.430718E-12: True
適用於
Equals(Complex)
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- Complex.cs
- 來源:
- Complex.cs
回傳一個值,表示當前實例與指定的複數值是否相同。
public:
virtual bool Equals(System::Numerics::Complex value);
public bool Equals(System.Numerics.Complex value);
override this.Equals : System.Numerics.Complex -> bool
Public Function Equals (value As Complex) As Boolean
參數
- value
- Complex
複數數比較。
傳回
true若此複數與value值相同;否則,。 false
實作
備註
該 Equals(Complex) 方法提供 IEquatable<T> 結構的實作 Complex 。 它的表現略優 Equals(Object) 於方法,因為它不需要將參數轉換成複數。
若兩個複數的實部相等,虛部相等,則稱複數相等。 此 Equals(Complex) 方法等價於以下表達式:
return this.Real.Equals(value) && this.Imaginary.Equals(value);
this.Real.Equals value && this.Imaginary.Equals value
Return Me.Real.Equals(value.Real) AndAlso Me.Imaginary.Equals(value.Imaginary)
給呼叫者的注意事項
使用此 Equals(Complex) 方法時需謹慎,因為兩個看似等價的值,可能因其實數與虛數分量的精度不同而被視為不相等。 以下範例報告 (3.33333, 0.142857) 和 (10/3, 1/7) 並不相等。
System.Numerics.Complex c1 = new System.Numerics.Complex(3.33333, .142857);
System.Numerics.Complex c2 = new System.Numerics.Complex(10/3.0, 1.0/7);
Console.WriteLine("{0} = {1}: {2}", c1, c2, c1.Equals(c2));
// The example displays the following output:
// (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): False
let c1 = System.Numerics.Complex(3.33333, 0.142857)
let c2 = System.Numerics.Complex(10. / 3., 1. / 7.)
printfn $"{c1} = {c2}: {c1.Equals c2}"
// The example displays the following output:
// (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): False
Dim c1 As New System.Numerics.Complex(3.33333, .142857)
Dim c2 As New System.Numerics.Complex(10/3, 1/7)
Console.WriteLine("{0} = {1}: {2}", c1, c2, c1.Equals(c2))
' The example displays the following output:
' (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): False
一種推薦的方法是定義兩個值之間的可接受差值(例如其中一方實數與虛數分量的0.01%),而非比較兩者的相等值。 如果兩個值之間的絕對值小於或等於該邊界,則該差異很可能是因為精度差異,因此兩者很可能相等。 以下範例使用此技術比較前一個範例中發現不相等的兩個複數值。 它會發現兩個複數相等。
System.Numerics.Complex c1 = new System.Numerics.Complex(3.33333, .142857);
System.Numerics.Complex c2 = new System.Numerics.Complex(10/3.0, 1.0/7);
double difference = .0001;
// Compare the values
bool result = (Math.Abs(c1.Real - c2.Real) <= c1.Real * difference) &
(Math.Abs(c1.Imaginary - c2.Imaginary) <= c1.Imaginary * difference);
Console.WriteLine("{0} = {1}: {2}", c1, c2, result);
// The example displays the following output:
// (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): True
let c1 = System.Numerics.Complex(3.33333, 0.142857)
let c2 = System.Numerics.Complex(10. / 3., 1. / 7.)
let difference = 0.0001
// Compare the values
let result =
(Math.Abs(c1.Real - c2.Real) <= c1.Real * difference)
&& (Math.Abs(c1.Imaginary - c2.Imaginary) <= c1.Imaginary * difference)
printfn $"{c1} = {c2}: {result}"
// The example displays the following output:
// (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): True
Dim c1 As New System.Numerics.Complex(3.33333, .142857)
Dim c2 As New System.Numerics.Complex(10/3.0, 1.0/7)
Dim difference As Double = .0001
' Compare the values
Dim result As Boolean = (Math.Abs(c1.Real - c2.Real) <= c1.Real * difference) And
(Math.Abs(c1.Imaginary - c2.Imaginary) <= c1.Imaginary * difference)
Console.WriteLine("{0} = {1}: {2}", c1, c2, result)
' The example displays the following output:
' (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): True