Home | History | Annotate | Download | only in complex.transcendentals
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <complex>
     11 
     12 // template<class T>
     13 //   complex<T>
     14 //   pow(const complex<T>& x, const T& y);
     15 
     16 #include <complex>
     17 #include <cassert>
     18 
     19 #include "../cases.h"
     20 
     21 template <class T>
     22 void
     23 test(const std::complex<T>& a, const T& b, std::complex<T> x)
     24 {
     25     std::complex<T> c = pow(a, b);
     26     is_about(real(c), real(x));
     27     is_about(imag(c), imag(x));
     28 }
     29 
     30 template <class T>
     31 void
     32 test()
     33 {
     34     test(std::complex<T>(2, 3), T(2), std::complex<T>(-5, 12));
     35 }
     36 
     37 void test_edges()
     38 {
     39     const double pi = std::atan2(+0., -0.);
     40     const unsigned N = sizeof(x) / sizeof(x[0]);
     41     for (unsigned i = 0; i < N; ++i)
     42     {
     43         for (unsigned j = 0; j < N; ++j)
     44         {
     45             std::complex<double> r = pow(x[i], real(x[j]));
     46             std::complex<double> z = exp(std::complex<double>(real(x[j])) * log(x[i]));
     47             if (std::isnan(real(r)))
     48                 assert(std::isnan(real(z)));
     49             else
     50             {
     51                 assert(real(r) == real(z));
     52             }
     53             if (std::isnan(imag(r)))
     54                 assert(std::isnan(imag(z)));
     55             else
     56             {
     57                 assert(imag(r) == imag(z));
     58             }
     59         }
     60     }
     61 }
     62 
     63 int main()
     64 {
     65     test<float>();
     66     test<double>();
     67     test<long double>();
     68     test_edges();
     69 }
     70