Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/complex/operator arith3"

From cppreference.com
< cpp‎ | numeric‎ | complex
m (Use new list syntax)
m (fmt)
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{cpp/numeric/complex/title|operator+{{dcl small|(binary)}}, operator-{{dcl small|(binary)}}, operator*, operator/}}
+
{{title|operator+,-{{small|()}}}}
 
{{cpp/numeric/complex/navbar}}
 
{{cpp/numeric/complex/navbar}}
{{ddcl list begin}}
+
{{begin}}
{{ddcl list item | num=1 | 1=
+
{{|num=1|=
 
template< class T >
 
template< class T >
complex<T> operator+( const complex<T>& lhs, const complex<T>& rhs);
+
complex<T> operator+( const complex<T>& lhs,
 +
const
 +
 +
 +
 +
complex<T>& rhs );
 
}}
 
}}
{{ddcl list item | num=2 | 1=
+
{{|num=2|=
 
template< class T >
 
template< class T >
complex<T> operator+( const complex<T>& lhs, const T& rhs);
+
complex<T> operator+( const complex<T>& lhs,
 +
 +
 +
 +
 +
const T& rhs );
 
}}
 
}}
{{ddcl list item | num=3 | 1=
+
{{|num=3|
 +
 +
 +
 +
=
 
template< class T >
 
template< class T >
complex<T> operator+( const T& lhs, const complex<T>& rhs);
+
complex<T> operator+( const T& lhs,
 +
const complex<T>& rhs );
 
}}
 
}}
{{ddcl list item | num=4 | 1=
+
{{|num=4|=
 
template< class T >
 
template< class T >
complex<T> operator-( const complex<T>& lhs, const complex<T>& rhs);
+
complex<T> operator-( const complex<T>& lhs,
 +
const
 +
 +
 +
 +
complex<T>& rhs );
 
}}
 
}}
{{ddcl list item | num=5 | 1=
+
{{|num=5|=
 
template< class T >
 
template< class T >
complex<T> operator-( const complex<T>& lhs, const T& rhs);
+
complex<T> operator-( const complex<T>& lhs,
 +
 +
 +
 +
 +
const T& rhs );
 
}}
 
}}
{{ddcl list item | num=6 | 1=
+
{{|num=6|
 +
 +
 +
 +
=
 
template< class T >
 
template< class T >
complex<T> operator-( const T& lhs, const complex<T>& rhs);
+
complex<T> operator-( const T& lhs,
 +
const complex<T>& rhs );
 
}}
 
}}
{{ddcl list item | num=7 | 1=
+
{{|num=7|=
 
template< class T >
 
template< class T >
complex<T> operator*( const complex<T>& lhs, const complex<T>& rhs);
+
complex<T> operator*( const complex<T>& lhs,
 +
const
 +
 +
 +
 +
complex<T>& rhs );
 
}}
 
}}
{{ddcl list item | num=8 | 1=
+
{{|num=8|=
 
template< class T >
 
template< class T >
complex<T> operator*( const complex<T>& lhs, const T& rhs);
+
complex<T> operator*( const complex<T>& lhs,
 +
 +
 +
 +
 +
const T& rhs );
 
}}
 
}}
{{ddcl list item | num=9 | 1=
+
{{|num=9|
 +
 +
 +
 +
=
 
template< class T >
 
template< class T >
complex<T> operator*( const T& lhs, const complex<T>& rhs);
+
complex<T> operator*( const T& lhs,
 +
const complex<T>& rhs );
 
}}
 
}}
{{ddcl list item | num=10 | 1=
+
{{|num=10|=
 
template< class T >
 
template< class T >
complex<T> operator/( const complex<T>& lhs, const complex<T>& rhs);
+
complex<T> operator/( const complex<T>& lhs,
 +
const
 +
 +
 +
 +
complex<T>& rhs );
 
}}
 
}}
{{ddcl list item | num=11 | 1=
+
{{|num=11|=
 
template< class T >
 
template< class T >
complex<T> operator/( const complex<T>& lhs, const T& rhs);
+
complex<T> operator/( const complex<T>& lhs,
 +
 +
 +
 +
 +
const T& rhs );
 
}}
 
}}
{{ddcl list item | num=12 | 1=
+
{{|num=12|
 +
 +
 +
 +
=
 
template< class T >
 
template< class T >
complex<T> operator/( const T& lhs, const complex<T>& rhs);
+
complex<T> operator/( const T& lhs,
 +
const complex<T>& rhs );
 
}}
 
}}
{{ddcl list end}}
+
{{end}}
  
 
Implements the binary operators for complex arithmetic and for mixed complex/scalar arithmetic. Scalar arguments are treated as complex numbers with the real part equal to the argument and the imaginary part set to zero.
 
Implements the binary operators for complex arithmetic and for mixed complex/scalar arithmetic. Scalar arguments are treated as complex numbers with the real part equal to the argument and the imaginary part set to zero.
  
@1-3@ Returns the sum of its arguments
+
@1-3@ Returns the sum of its arguments
@4-6@ Returns the result of subtracting {{tt|rhs}} from {{tt|lhs}}
+
@4-6@ Returns the result of subtracting {{|rhs}} from {{|lhs}}
@7-9@ Multiplies its arguments
+
@7-9@ Multiplies its arguments
@10-12@ Divides {{tt|lhs}} by {{tt|rhs}}
+
@10-12@ Divides {{|lhs}} by {{|rhs}}
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{begin}}
{{param list item | lhs, rhs | the arguments: either both complex numbers or one complex and one scalar of matching type ({{c|float}}, {{c|double}}, {{c|long double}})}}
+
{{|lhs, rhs|the arguments: either both complex numbers or one complex and one scalar of matching type ({{c|float}}, {{c|double}}, {{c|long double}})}}
{{param list end}}
+
{{end}}
  
 
===Return value===
 
===Return value===
@1-3@ {{c|1= complex<T>(lhs) += rhs }}
+
@1-3@ {{c|1= complex<T>(lhs) += rhs}}
@4-6@ {{c|1= complex<T>(lhs) -= rhs }}
+
@4-6@ {{c|1= complex<T>(lhs) -= rhs}}
@7-9@ {{c|1= complex<T>(lhs) *= rhs }}
+
@7-9@ {{c|1= complex<T>(lhs) *= rhs}}
@10-12@ {{c|1= complex<T>(lhs) /= rhs }}
+
@10-12@ {{c|1= complex<T>(lhs) /= rhs}}
 +
 
 +
 +
 +
 
 +
  
 
===Example===
 
===Example===
{{example |
+
{{example
| code=
+
|code=
#include <iostream>
+
 
#include <complex>
 
#include <complex>
 +
 +
 
int main()
 
int main()
 
{
 
{
     std::complex<double> c2(2, 0);
+
     std::complex<double> c2(2, 0);
     std::complex<double> ci(0, 1);
+
     std::complex<double> ci(0, 1);
  
     std::cout << ci << " + " << c2 << " = " << ci+c2 << '\n'
+
     std::cout << ci << " + " << c2 << " = " << ci + c2 << '\n'
               << ci << " * " << ci << " = " << ci*ci << '\n'
+
               << ci << " * " << ci << " = " << ci * ci << '\n'
               << ci << " + " << c2 << " / " << ci << " = " << ci+c2/ci << '\n'
+
               << ci << " + " << c2 << " / " << ci << " = " << ci + c2 / ci << '\n'
               << 1  << " / " << ci << " = " << 1./ci << '\n';
+
               << 1  << " / " << ci << " = " << 1./ ci << '\n';
  
//    std::cout << 1.f/ci; // compile error
+
//    std::cout << 1./ ci; // compile error
//    std::cout << 1/ci; // compile error
+
//    std::cout << 1 / ci; // compile error
 
}
 
}
| output=
+
|output=
 
(0,1) + (2,0) = (2,1)
 
(0,1) + (2,0) = (2,1)
 
(0,1) * (0,1) = (-1,0)
 
(0,1) * (0,1) = (-1,0)
Line 96: Line 162:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{begin}}
{{dcl list template | cpp/numeric/complex/dcl list operator_arith}}
+
{{|cpp/numeric/complex/operator_arith}}
{{dcl list template | cpp/numeric/complex/dcl list operator_arith2}}
+
{{|cpp/numeric/complex/operator_arith2}}
{{dcl list end}}
+
{{end
 +
 
 +
}}

Latest revision as of 13:51, 2 July 2023

 
 
 
 
(1)
template< class T >

std::complex<T> operator+( const std::complex<T>& lhs,

                           const std::complex<T>& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator+( const std::complex<T>& lhs,

                                     const std::complex<T>& rhs );
(since C++20)
(2)
template< class T >

std::complex<T> operator+( const std::complex<T>& lhs,

                           const T& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator+( const std::complex<T>& lhs,

                                     const T& rhs );
(since C++20)
(3)
template< class T >

std::complex<T> operator+( const T& lhs,

                           const std::complex<T>& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator+( const T& lhs,

                                     const std::complex<T>& rhs );
(since C++20)
(4)
template< class T >

std::complex<T> operator-( const std::complex<T>& lhs,

                           const std::complex<T>& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator-( const std::complex<T>& lhs,

                                     const std::complex<T>& rhs );
(since C++20)
(5)
template< class T >

std::complex<T> operator-( const std::complex<T>& lhs,

                           const T& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator-( const std::complex<T>& lhs,

                                     const T& rhs );
(since C++20)
(6)
template< class T >

std::complex<T> operator-( const T& lhs,

                           const std::complex<T>& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator-( const T& lhs,

                                     const std::complex<T>& rhs );
(since C++20)
(7)
template< class T >

std::complex<T> operator*( const std::complex<T>& lhs,

                           const std::complex<T>& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator*( const std::complex<T>& lhs,

                                     const std::complex<T>& rhs );
(since C++20)
(8)
template< class T >

std::complex<T> operator*( const std::complex<T>& lhs,

                           const T& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator*( const std::complex<T>& lhs,

                                     const T& rhs );
(since C++20)
(9)
template< class T >

std::complex<T> operator*( const T& lhs,

                           const std::complex<T>& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator*( const T& lhs,

                                     const std::complex<T>& rhs );
(since C++20)
(10)
template< class T >

std::complex<T> operator/( const std::complex<T>& lhs,

                           const std::complex<T>& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator/( const std::complex<T>& lhs,

                                     const std::complex<T>& rhs );
(since C++20)
(11)
template< class T >

std::complex<T> operator/( const std::complex<T>& lhs,

                           const T& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator/( const std::complex<T>& lhs,

                                     const T& rhs );
(since C++20)
(12)
template< class T >

std::complex<T> operator/( const T& lhs,

                           const std::complex<T>& rhs );
(until C++20)
template< class T >

constexpr std::complex<T> operator/( const T& lhs,

                                     const std::complex<T>& rhs );
(since C++20)

Implements the binary operators for complex arithmetic and for mixed complex/scalar arithmetic. Scalar arguments are treated as complex numbers with the real part equal to the argument and the imaginary part set to zero.

1-3) Returns the sum of its arguments.
4-6) Returns the result of subtracting rhs from lhs.
7-9) Multiplies its arguments.
10-12) Divides lhs by rhs.

Contents

[edit] Parameters

lhs, rhs - the arguments: either both complex numbers or one complex and one scalar of matching type (float, double, long double)

[edit] Return value

1-3) std::complex<T>(lhs) += rhs
4-6) std::complex<T>(lhs) -= rhs
7-9) std::complex<T>(lhs) *= rhs
10-12) std::complex<T>(lhs) /= rhs

[edit] Notes

Because template argument deduction does not consider implicit conversions, these operators cannot be used for mixed integer/complex arithmetic. In all cases, the scalar must have the same type as the underlying type of the complex number.

The GCC flag "-fcx-limited-range" (included by "-ffast-math") changes the behavior of complex multiply/division by removing checks for floating point edge cases. This impacts loop vectorization.

[edit] Example

#include <complex>
#include <iostream>
 
int main()
{
    std::complex<double> c2(2.0, 0.0);
    std::complex<double> ci(0.0, 1.0);
 
    std::cout << ci << " + " << c2 << " = " << ci + c2 << '\n'
              << ci << " * " << ci << " = " << ci * ci << '\n'
              << ci << " + " << c2 << " / " << ci << " = " << ci + c2 / ci << '\n'
              << 1  << " / " << ci << " = " << 1.0 / ci << '\n';
 
//    std::cout << 1.0f / ci; // compile error
//    std::cout << 1 / ci; // compile error
}

Output:

(0,1) + (2,0) = (2,1)
(0,1) * (0,1) = (-1,0)
(0,1) + (2,0) / (0,1) = (0,-1)
1 / (0,1) = (0,-1)

[edit] See also

compound assignment of two complex numbers or a complex and a scalar
(public member function) [edit]
applies unary operators to complex numbers
(function template) [edit]