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 //   atanh(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     assert(atanh(c) == x);
     26 }
     27 
     28 template <class T>
     29 void
     30 test()
     31 {
     32     test(std::complex<T>(0, 0), std::complex<T>(0, 0));
     33 }
     34 
     35 void test_edges()
     36 {
     37     typedef std::complex<double> C;
     38     const double pi = std::atan2(+0., -0.);
     39     const unsigned N = sizeof(testcases) / sizeof(testcases[0]);
     40     for (unsigned i = 0; i < N; ++i)
     41     {
     42         std::complex<double> r = atanh(testcases[i]);
     43         if (testcases[i].real() == 0 && testcases[i].imag() == 0)
     44         {
     45             assert(std::signbit(r.real()) == std::signbit(testcases[i].real()));
     46             assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag()));
     47         }
     48         else if ( testcases[i].real() == 0 && std::isnan(testcases[i].imag()))
     49         {
     50             assert(r.real() == 0);
     51             assert(std::signbit(testcases[i].real()) == std::signbit(r.real()));
     52             assert(std::isnan(r.imag()));
     53         }
     54         else if (std::abs(testcases[i].real()) == 1 && testcases[i].imag() == 0)
     55         {
     56             assert(std::isinf(r.real()));
     57             assert(std::signbit(testcases[i].real()) == std::signbit(r.real()));
     58             assert(r.imag() == 0);
     59             assert(std::signbit(testcases[i].imag()) == std::signbit(r.imag()));
     60         }
     61         else if (std::isfinite(testcases[i].real()) && std::isinf(testcases[i].imag()))
     62         {
     63             assert(r.real() == 0);
     64             assert(std::signbit(testcases[i].real()) == std::signbit(r.real()));
     65             if (testcases[i].imag() > 0)
     66                 is_about(r.imag(),  pi/2);
     67             else
     68                 is_about(r.imag(), -pi/2);
     69         }
     70         else if (std::isfinite(testcases[i].real()) && std::isnan(testcases[i].imag()))
     71         {
     72             assert(std::isnan(r.real()));
     73             assert(std::isnan(r.imag()));
     74         }
     75         else if (std::isinf(testcases[i].real()) && std::isfinite(testcases[i].imag()))
     76         {
     77             assert(r.real() == 0);
     78             assert(std::signbit(testcases[i].real()) == std::signbit(r.real()));
     79             if (std::signbit(testcases[i].imag()))
     80                 is_about(r.imag(), -pi/2);
     81             else
     82                 is_about(r.imag(),  pi/2);
     83         }
     84         else if (std::isinf(testcases[i].real()) && std::isinf(testcases[i].imag()))
     85         {
     86             assert(r.real() == 0);
     87             assert(std::signbit(testcases[i].real()) == std::signbit(r.real()));
     88             if (std::signbit(testcases[i].imag()))
     89                 is_about(r.imag(), -pi/2);
     90             else
     91                 is_about(r.imag(),  pi/2);
     92         }
     93         else if (std::isinf(testcases[i].real()) && std::isnan(testcases[i].imag()))
     94         {
     95             assert(r.real() == 0);
     96             assert(std::signbit(testcases[i].real()) == std::signbit(r.real()));
     97             assert(std::isnan(r.imag()));
     98         }
     99         else if (std::isnan(testcases[i].real()) && std::isfinite(testcases[i].imag()))
    100         {
    101             assert(std::isnan(r.real()));
    102             assert(std::isnan(r.imag()));
    103         }
    104         else if (std::isnan(testcases[i].real()) && std::isinf(testcases[i].imag()))
    105         {
    106             assert(r.real() == 0);
    107             assert(std::signbit(testcases[i].real()) == std::signbit(r.real()));
    108             if (std::signbit(testcases[i].imag()))
    109                 is_about(r.imag(), -pi/2);
    110             else
    111                 is_about(r.imag(),  pi/2);
    112         }
    113         else if (std::isnan(testcases[i].real()) && std::isnan(testcases[i].imag()))
    114         {
    115             assert(std::isnan(r.real()));
    116             assert(std::isnan(r.imag()));
    117         }
    118         else
    119         {
    120             assert(std::signbit(r.real()) == std::signbit(testcases[i].real()));
    121             assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag()));
    122         }
    123     }
    124 }
    125 
    126 int main()
    127 {
    128     test<float>();
    129     test<double>();
    130     test<long double>();
    131     test_edges();
    132 }
    133