Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/complex/abs"

From cppreference.com
< cpp‎ | numeric‎ | complex
(+detail,example)
m (fmt, headers sorted)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{cpp/title|abs{{small|(std::complex)}}}}
 
{{cpp/title|abs{{small|(std::complex)}}}}
 
{{cpp/numeric/complex/navbar}}
 
{{cpp/numeric/complex/navbar}}
{{ddcl | header=complex | 1=
+
{{ddcl|header=complex|1=
 
template< class T >  
 
template< class T >  
 
T abs( const complex<T>& z );
 
T abs( const complex<T>& z );
 
}}
 
}}
  
Returns the magnitude of the complex number {{tt|z}}.
+
Returns the magnitude of the complex number {{c|z}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | z | complex value}}
+
{{par|z|complex value}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
If no errors occur, returns the absolute value (also known as norm, modulus, or magnitude) of {{tt|z}}.
+
If no errors occur, returns the absolute value (also known as norm, modulus, or magnitude) of {{c|z}}.
  
Errors and special cases are handled as if the function is implemented as {{c|std::hypot(std::real(z), std::imag(z))}}
+
Errors and special cases are handled as if the function is implemented as {{c|std::hypot(std::real(z), std::imag(z))}}.
  
===Examples===
+
===Example===
 
{{example|
 
{{example|
 
|code=
 
|code=
#include <iostream>
 
 
#include <complex>
 
#include <complex>
 +
#include <iostream>
  
 
int main()
 
int main()
Line 36: Line 36:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/complex/dsc arg}}
+
{{dsc inc|cpp/numeric/complex/dsc arg}}
{{dsc inc | cpp/numeric/complex/dsc polar}}
+
{{dsc inc|cpp/numeric/complex/dsc polar}}
{{dsc inc | cpp/numeric/math/dsc abs}}
+
{{dsc inc|cpp/numeric/math/dsc abs}}
{{dsc inc | cpp/numeric/math/dsc fabs}}
+
{{dsc inc|cpp/numeric/math/dsc fabs}}
{{dsc inc | cpp/numeric/math/dsc hypot}}
+
{{dsc inc|cpp/numeric/math/dsc hypot}}
{{dsc inc | cpp/numeric/valarray/dsc abs}}
+
{{dsc inc|cpp/numeric/valarray/dsc abs}}
{{dsc see c | c/numeric/complex/cabs}}
+
{{dsc see c|c/numeric/complex/cabs}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/numeric/complex/abs]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/numeric/complex/abs]]
+
[[fr:cpp/numeric/complex/abs]]
+
[[it:cpp/numeric/complex/abs]]
+
[[ja:cpp/numeric/complex/abs]]
+
[[pt:cpp/numeric/complex/abs]]
+
[[ru:cpp/numeric/complex/abs]]
+
[[zh:cpp/numeric/complex/abs]]
+

Latest revision as of 12:31, 20 April 2023

 
 
 
 
Defined in header <complex>
template< class T >
T abs( const complex<T>& z );

Returns the magnitude of the complex number z.

Contents

[edit] Parameters

z - complex value

[edit] Return value

If no errors occur, returns the absolute value (also known as norm, modulus, or magnitude) of z.

Errors and special cases are handled as if the function is implemented as std::hypot(std::real(z), std::imag(z)).

[edit] Example

#include <complex>
#include <iostream>
 
int main()
{
    std::complex<double> z(1, 1);
    std::cout << z << " cartesian is rho = " << std::abs(z)
              << " theta = " << std::arg(z) << " polar\n";
}

Output:

(1,1) cartesian is rho = 1.41421 theta = 0.785398 polar

[edit] See also

returns the phase angle
(function template) [edit]
constructs a complex number from magnitude and phase angle
(function template) [edit]
computes absolute value of an integral value (|x|)
(function) [edit]
absolute value of a floating point value (|x|)
(function) [edit]
(C++11)(C++11)(C++11)
computes square root of the sum of the squares of two or three(since C++17) given numbers (x2+y2), (x2+y2+z2)(since C++17)
(function) [edit]
applies the function abs to each element of valarray
(function template) [edit]