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 //   sqrt(const complex<T>& x);
     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>& c, std::complex<T> x)
     24 {
     25     std::complex<T> a = sqrt(c);
     26     is_about(real(a), real(x));
     27     assert(std::abs(imag(c)) < 1.e-6);
     28 }
     29 
     30 template <class T>
     31 void
     32 test()
     33 {
     34     test(std::complex<T>(64, 0), std::complex<T>(8, 0));
     35 }
     36 
     37 void test_edges()
     38 {
     39     const unsigned N = sizeof(x) / sizeof(x[0]);
     40     for (unsigned i = 0; i < N; ++i)
     41     {
     42         std::complex<double> r = sqrt(x[i]);
     43         if (x[i].real() == 0 && x[i].imag() == 0)
     44         {
     45             assert(!std::signbit(r.real()));
     46             assert(std::signbit(r.imag()) == std::signbit(x[i].imag()));
     47         }
     48         else if (std::isinf(x[i].imag()))
     49         {
     50             assert(std::isinf(r.real()));
     51             assert(r.real() > 0);
     52             assert(std::isinf(r.imag()));
     53             assert(std::signbit(r.imag()) == std::signbit(x[i].imag()));
     54         }
     55         else if (std::isfinite(x[i].real()) && std::isnan(x[i].imag()))
     56         {
     57             assert(std::isnan(r.real()));
     58             assert(std::isnan(r.imag()));
     59         }
     60         else if (std::isinf(x[i].real()) && x[i].real() < 0 && std::isfinite(x[i].imag()))
     61         {
     62             assert(r.real() == 0);
     63             assert(!std::signbit(r.real()));
     64             assert(std::isinf(r.imag()));
     65             assert(std::signbit(x[i].imag()) == std::signbit(r.imag()));
     66         }
     67         else if (std::isinf(x[i].real()) && x[i].real() > 0 && std::isfinite(x[i].imag()))
     68         {
     69             assert(std::isinf(r.real()));
     70             assert(r.real() > 0);
     71             assert(r.imag() == 0);
     72             assert(std::signbit(x[i].imag()) == std::signbit(r.imag()));
     73         }
     74         else if (std::isinf(x[i].real()) && x[i].real() < 0 && std::isnan(x[i].imag()))
     75         {
     76             assert(std::isnan(r.real()));
     77             assert(std::isinf(r.imag()));
     78         }
     79         else if (std::isinf(x[i].real()) && x[i].real() > 0 && std::isnan(x[i].imag()))
     80         {
     81             assert(std::isinf(r.real()));
     82             assert(r.real() > 0);
     83             assert(std::isnan(r.imag()));
     84         }
     85         else if (std::isnan(x[i].real()) && (std::isfinite(x[i].imag()) || std::isnan(x[i].imag())))
     86         {
     87             assert(std::isnan(r.real()));
     88             assert(std::isnan(r.imag()));
     89         }
     90         else if (std::signbit(x[i].imag()))
     91         {
     92             assert(!std::signbit(r.real()));
     93             assert(std::signbit(r.imag()));
     94         }
     95         else
     96         {
     97             assert(!std::signbit(r.real()));
     98             assert(!std::signbit(r.imag()));
     99         }
    100     }
    101 }
    102 
    103 int main()
    104 {
    105     test<float>();
    106     test<double>();
    107     test<long double>();
    108     test_edges();
    109 }
    110