Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | complex
m (+link to operator=)
m (fmt)
 
(12 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{cpp/title|n=literals::complex_literals::|operator""i, operator""if, operator""il }}
+
{{cpp/title|n=literals::complex_literals::|operator""i, operator""if, operator""il}}
 
{{cpp/numeric/complex/navbar}}
 
{{cpp/numeric/complex/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | complex}}
+
{{dcl header|complex}}
{{dcl | num=1 | since=c++14 | 1=
+
{{dcla|num=1|since=c++14|1=
constexpr complex<long double> operator""il(long double arg);
+
constexpr complex<double> operator""i( long double arg );
constexpr complex<long double> operator""il(unsigned long long arg);
+
constexpr complex<double> operator""i( unsigned long long arg );
 
}}
 
}}
{{dcl | num=2 | since=c++14 | 1=
+
{{dcla|num=2|since=c++14|1=
constexpr complex<double> operator""i(long double arg);
+
constexpr complex<float> operator""if( long double arg );
constexpr complex<double> operator""i(unsigned long long arg);
+
constexpr complex<float> operator""if( unsigned long long arg );
 
}}
 
}}
{{dcl | num=3 | since=c++14 | 1=
+
{{dcla|num=3|since=c++14|1=
constexpr complex<float> operator""if(long double arg);
+
constexpr complex<long double> operator""il( long double arg );
constexpr complex<float> operator""if(unsigned long long arg);
+
constexpr complex<long double> operator""il( unsigned long long arg );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 19: Line 19:
 
Forms a {{lc|std::complex}} literal representing an imaginary number.
 
Forms a {{lc|std::complex}} literal representing an imaginary number.
  
@1@ forms a literal {{c|std::complex<double>}} with the real part zero and imaginary part {{tt|arg}}
+
@1@ Forms a literal {{c|std::complex<double>}} with the real part zero and imaginary part {{c|arg}}.
@2@ forms a literal {{c|std::complex<float>}} with the real part zero and imaginary part {{tt|arg}}
+
@2@ Forms a literal {{c|std::complex<float>}} with the real part zero and imaginary part {{c|arg}}.
@3@ forms a literal {{c|std::complex<long double>}} with the real part zero and imaginary part {{tt|arg}}
+
@3@ Forms a literal {{c|std::complex<long double>}} with the real part zero and imaginary part {{c|arg}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | arg | the value of the imaginary number}}
+
{{par|arg|the value of the imaginary number}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
The {{lc|std::complex}} literal with the real part zero and imaginary part {{tt|arg}}
+
The {{lc|std::complex}} literal with the real part zero and imaginary part {{c|arg}}.
 +
 
 +
===Notes===
 +
These operators are declared in the namespace {{c|std::literals::complex_literals}}, where both {{tt|literals}} and {{tt|complex_literals}} are inline namespaces. Access to these operators can be gained with either:
 +
* {{c|using namespace std::literals}},
 +
* {{c|using namespace std::complex_literals}}, or
 +
* {{c|using namespace std::literals::complex_literals}}.
 +
 
 +
Even though {{c|if}} is a [[cpp/keywords/if|keyword]] in C++, it is a {{spar|ud-suffix}} of the [[cpp/language/user literal|literal operator]] of the form {{c|operator ""if}} and in the literal expressions such as {{c|1if}} or {{c|1.0if}} because it is not separated by whitespace and is not a standalone token.
 +
 
 +
{{feature test macro|__cpp_lib_complex_udls|User-Defined Literals for {{lc|std::complex}}|value=201309L|std=C++14}}
  
 
===Possible implementation===
 
===Possible implementation===
{{eq fun
+
{{eq impl
| 1=
+
|title1=operator""i|ver1=1|1=
constexpr std::complex<double> operator""i(unsigned long long d);
+
constexpr std::complex<double> operator""i(unsigned long long d)
 
{
 
{
     return std::complex<double>{0.0, static_cast<double>(d)}.
+
     return std::complex<double> {0.0, static_cast<double>(d)};
 
}
 
}
constexpr std::complex<long double> operator""i(double d);
+
 
 +
constexpr std::complex<double> operator""i(long double d)
 
{
 
{
     return std::complex<double>{0.0, d}.
+
     return std::complex<double> {0.0, static_cast<double>(d)};
 +
}
 +
|title2=operator""if|ver2=2|2=
 +
constexpr std::complex<float> operator""if(unsigned long long d)
 +
{
 +
    return std::complex<float> {0.0f, static_cast<float>(d)};
 
}
 
}
}}
 
  
===Notes===
+
constexpr std::complex<float> operator""if(long double d)
These operators are declared in the namespace {{c|std::literals::complex_literals}}, where both {{tt|literals}} and {{tt|complex_literals}} are inline namespaces. Access to these operators can be gained with {{c|using namespace std::literals}} and {{c|using namespace std::literals::complex_literals}}.
+
{
 +
    return std::complex<float> {0.0f, static_cast<float>(d)};
 +
}
 +
|title3=operator""il|ver3=3|3=
 +
constexpr std::complex<long double> operator""il(unsigned long long d)
 +
{
 +
    return std::complex<long double> {0.0L, static_cast<long double>(d)};
 +
}
  
Even though {{c|if}} is a [[cpp/keywords/if|keyword]] in C++, it is a {{spar|ud-suffix}} of the [[cpp/language/user_literal|literal operator]] of the form {{c|operator ""if}} and in the literal expressions such as {{c|1if}} or {{c|1.0if}} because it is not separated by whitespace and is not a standalone token.
+
constexpr std::complex<long double> operator""il(long double d)
 +
{
 +
    return std::complex<long double> {0.0L, d};
 +
}
 +
}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
#include <iostream>
+
 
#include <complex>
 
#include <complex>
 +
#include <iostream>
  
 
int main()
 
int main()
 
{
 
{
     using namespace std::literals::complex_literals;
+
     using namespace std::complex_literals;
 +
 
 
     std::complex<double> c = 1.0 + 1i;
 
     std::complex<double> c = 1.0 + 1i;
     std::cout << "abs" << c << " = " << abs(c) << '\n';
+
     std::cout << "abs" << c << " = " << std::abs(c) << '\n';
 +
 
 +
    std::complex<float> z = 3.0f + 4.0if;
 +
    std::cout << "abs" << z << " = " << std::abs(z) << '\n';
 
}
 
}
| output=
+
|output=
 
abs(1,1) = 1.41421
 
abs(1,1) = 1.41421
 +
abs(3,4) = 5
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/complex/dsc complex}}
+
{{dsc inc|cpp/numeric/complex/dsc complex}}
{{dsc inc | cpp/numeric/complex/dsc operator{{=}}}}
+
{{dsc inc|cpp/numeric/complex/dsc operator{{=}}}}
{{dsc see c | c/numeric/complex/I}}
+
{{dsc see c|c/numeric/complex/I}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/numeric/complex/operator""i]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/numeric/complex/operator""i]]
+
[[fr:cpp/numeric/complex/operator""i]]
+
[[it:cpp/numeric/complex/operator""i]]
+
[[ja:cpp/numeric/complex/operator""i]]
+
[[pt:cpp/numeric/complex/operator""i]]
+
[[ru:cpp/numeric/complex/operator""i]]
+
[[zh:cpp/numeric/complex/operator""i]]
+

Latest revision as of 06:51, 22 April 2023

 
 
 
 
Defined in header <complex>
constexpr complex<double> operator""i( long double arg );
constexpr complex<double> operator""i( unsigned long long arg );
(1) (since C++14)
constexpr complex<float> operator""if( long double arg );
constexpr complex<float> operator""if( unsigned long long arg );
(2) (since C++14)
constexpr complex<long double> operator""il( long double arg );
constexpr complex<long double> operator""il( unsigned long long arg );
(3) (since C++14)

Forms a std::complex literal representing an imaginary number.

1) Forms a literal std::complex<double> with the real part zero and imaginary part arg.
2) Forms a literal std::complex<float> with the real part zero and imaginary part arg.
3) Forms a literal std::complex<long double> with the real part zero and imaginary part arg.

Contents

[edit] Parameters

arg - the value of the imaginary number

[edit] Return value

The std::complex literal with the real part zero and imaginary part arg.

[edit] Notes

These operators are declared in the namespace std::literals::complex_literals, where both literals and complex_literals are inline namespaces. Access to these operators can be gained with either:

  • using namespace std::literals,
  • using namespace std::complex_literals, or
  • using namespace std::literals::complex_literals.

Even though if is a keyword in C++, it is a ud-suffix of the literal operator of the form operator ""if and in the literal expressions such as 1if or 1.0if because it is not separated by whitespace and is not a standalone token.

Feature-test macro Value Std Feature
__cpp_lib_complex_udls 201309L (C++14) User-Defined Literals for std::complex

[edit] Possible implementation

operator""i
constexpr std::complex<double> operator""i(unsigned long long d)
{
    return std::complex<double> {0.0, static_cast<double>(d)};
}
 
constexpr std::complex<double> operator""i(long double d)
{
    return std::complex<double> {0.0, static_cast<double>(d)};
}
operator""if
constexpr std::complex<float> operator""if(unsigned long long d)
{
    return std::complex<float> {0.0f, static_cast<float>(d)};
}
 
constexpr std::complex<float> operator""if(long double d)
{
    return std::complex<float> {0.0f, static_cast<float>(d)};
}
operator""il
constexpr std::complex<long double> operator""il(unsigned long long d)
{
    return std::complex<long double> {0.0L, static_cast<long double>(d)};
}
 
constexpr std::complex<long double> operator""il(long double d)
{
    return std::complex<long double> {0.0L, d};
}

[edit] Example

#include <complex>
#include <iostream>
 
int main()
{
    using namespace std::complex_literals;
 
    std::complex<double> c = 1.0 + 1i;
    std::cout << "abs" << c << " = " << std::abs(c) << '\n';
 
    std::complex<float> z = 3.0f + 4.0if;
    std::cout << "abs" << z << " = " << std::abs(z) << '\n';
}

Output:

abs(1,1) = 1.41421
abs(3,4) = 5

[edit] See also

constructs a complex number
(public member function) [edit]
assigns the contents
(public member function) [edit]