This is an old version, view current version.

4.4 Complex comparison operators

Complex numbers are equal if and only if both their real and imaginary components are equal. That is, the conditional

z1 == z2

is equivalent to

get_real(z1) == get_real(z2) && get_imag(z1) == get_imag(z2)

As with other complex functions, if one of the arguments is of type real or int, it will be promoted to type complex before comparison. For example, if z is of type complex, then z == 0 will be true if z has real component equal to 0.0 and complex component equal to 0.0.

Warning: As with real values, it is usually a mistake to compare complex numbers for equality because their parts are implemented using floating-point arithmetic, which suffers from precision errors, rendering algebraically equivalent expressions not equal after evaluation.

int operator==(complex x, complex y)
Return 1 if x is equal to y and 0 otherwise, \[ (x \,\text{==}\, y) \ = \ \text{operator==}(x,y) \ = \ \begin{cases} 1 & \text{if $x = y$}, \ \text{and} \\ 0 & \text{otherwise.} \end{cases} \]
Available since 2.28

int operator!=(complex x, complex y)
Return 1 if x is not equal to y and 0 otherwise, \[ (x \,\text{!=}\, y) \ = \ \text{operator!=}(x,y) \ = \ \begin{cases} 1 & \text{if $x \neq y$}, \ \text{and} \\ 0 & \text{otherwise.} \end{cases} \]
Available since 2.28