Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | complex
(+example)
m (Use new list syntax)
Line 54: Line 54:
 
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.
  
{{li begin}}
+
1-3Returns the sum of its arguments
{{li|1-3}} Returns the sum of its arguments
+
4-6Returns the result of subtracting {{tt|rhs}} from {{tt|lhs}}
{{li|4-6}} Returns the result of subtracting {{tt|rhs}} from {{tt|lhs}}
+
7-9Multiplies its arguments
{{li|7-9}} Multiplies its arguments
+
10-12Divides {{tt|lhs}} by {{tt|rhs}}
{{li|10-12}} Divides {{tt|lhs}} by {{tt|rhs}}
+
{{li end}}
+
  
 
===Parameters===
 
===Parameters===
Line 67: Line 65:
  
 
===Return value===
 
===Return value===
{{li begin}}
+
1-3{{c|1= complex<T>(lhs) += rhs }}
{{li|1-3}} {{c|1= complex<T>(lhs) += rhs }}
+
4-6{{c|1= complex<T>(lhs) -= rhs }}
{{li|4-6}} {{c|1= complex<T>(lhs) -= rhs }}
+
7-9{{c|1= complex<T>(lhs) *= rhs }}
{{li|7-9}} {{c|1= complex<T>(lhs) *= rhs }}
+
10-12{{c|1= complex<T>(lhs) /= rhs }}
{{li|10-12}} {{c|1= complex<T>(lhs) /= rhs }}
+
{{li end}}
+
  
 
===Example===
 
===Example===

Revision as of 13:22, 30 August 2012

 
 
 
 

Template:ddcl list begin <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator+( const complex<T>& lhs, const complex<T>& rhs);
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator+( const complex<T>& lhs, const T& rhs);
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator+( const T& lhs, const complex<T>& rhs);
</td>

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator-( const complex<T>& lhs, const complex<T>& rhs);
</td>

<td > (4) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator-( const complex<T>& lhs, const T& rhs);
</td>

<td > (5) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator-( const T& lhs, const complex<T>& rhs);
</td>

<td > (6) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator*( const complex<T>& lhs, const complex<T>& rhs);
</td>

<td > (7) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator*( const complex<T>& lhs, const T& rhs);
</td>

<td > (8) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator*( const T& lhs, const complex<T>& rhs);
</td>

<td > (9) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator/( const complex<T>& lhs, const complex<T>& rhs);
</td>

<td > (10) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator/( const complex<T>& lhs, const T& rhs);
</td>

<td > (11) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class T >
complex<T> operator/( const T& lhs, const complex<T>& rhs);
</td>

<td > (12) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list 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.

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

Parameters

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

Return value

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

Example

#include <iostream>
#include <complex>
int main()
{
    std::complex<double> c2(2, 0);
    std::complex<double> ci(0, 1);
 
    std::cout << ci << " + " << c2 << " = " << ci+c2 << '\n'
              << ci << " * " << ci << " = " << ci*ci << '\n'
              << ci << " + " << c2 << " / " << ci << " = " << ci+c2/ci << '\n'
              << 1  << " / " << ci << " = " << 1./ci << '\n';
 
//    std::cout << 1.f/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)

See also

Template:cpp/numeric/complex/dcl list operator arithTemplate:cpp/numeric/complex/dcl list operator arith2