Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:13

0001 // name=complex_norm_test ; gcc $name.cc -std=c++11 -lstdc++ -o /tmp/$name && /tmp/$name
0002 
0003 /**
0004 
0005 https://en.cppreference.com/w/cpp/numeric/complex/norm
0006 
0007 **/
0008 
0009 
0010 #include <cassert>
0011 #include <complex>
0012 #include <iostream>
0013  
0014 int main()
0015 {
0016     std::complex<double> z {3.0, 4.0};
0017 
0018     assert(std::norm(z) == (z.real() * z.real() + z.imag() * z.imag()));
0019     assert(std::norm(z) == (z * std::conj(z)));
0020     assert(std::norm(z) == (std::abs(z) * std::abs(z)));
0021 
0022     std::cout << "std::norm(" << z << ") = " << std::norm(z) << '\n';
0023 
0024 
0025     for(double v=2.0 ; v > -2.1 ; v-=0.1 )
0026     {
0027         std::complex<double> q {v, 0.0 } ; 
0028         std::cout << "std::norm(" << q << ") = " << std::norm(q) << '\n';
0029     }
0030 
0031     return 0 ; 
0032 }
0033