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 // tanh(const complex<T>& x); 15 16 #include <complex> 17 #include <cassert> 18 19 #include "../cases.h" 20 21 template <class T> 22 void test(const std::complex<T> & c,std::complex<T> x)23test(const std::complex<T>& c, std::complex<T> x) 24 { 25 assert(tanh(c) == x); 26 } 27 28 template <class T> 29 void test()30test() 31 { 32 test(std::complex<T>(0, 0), std::complex<T>(0, 0)); 33 } 34 test_edges()35void test_edges() 36 { 37 typedef std::complex<double> C; 38 const double pi = std::atan2(+0., -0.); 39 const unsigned N = sizeof(x) / sizeof(x[0]); 40 for (unsigned i = 0; i < N; ++i) 41 { 42 std::complex<double> r = tanh(x[i]); 43 if (x[i].real() == 0 && x[i].imag() == 0) 44 { 45 assert(r.real() == 0); 46 assert(std::signbit(r.real()) == std::signbit(x[i].real())); 47 assert(r.imag() == 0); 48 assert(std::signbit(r.imag()) == std::signbit(x[i].imag())); 49 } 50 else if (std::isfinite(x[i].real()) && std::isinf(x[i].imag())) 51 { 52 assert(std::isnan(r.real())); 53 assert(std::isnan(r.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()) && std::isfinite(x[i].imag())) 61 { 62 assert(r.real() == 1); 63 assert(r.imag() == 0); 64 assert(std::signbit(r.imag()) == std::signbit(sin(2*x[i].imag()))); 65 } 66 else if (std::isinf(x[i].real()) && std::isinf(x[i].imag())) 67 { 68 assert(r.real() == 1); 69 assert(r.imag() == 0); 70 } 71 else if (std::isinf(x[i].real()) && std::isnan(x[i].imag())) 72 { 73 assert(r.real() == 1); 74 assert(r.imag() == 0); 75 } 76 else if (std::isnan(x[i].real()) && x[i].imag() == 0) 77 { 78 assert(std::isnan(r.real())); 79 assert(r.imag() == 0); 80 assert(std::signbit(r.imag()) == std::signbit(x[i].imag())); 81 } 82 else if (std::isnan(x[i].real()) && std::isfinite(x[i].imag())) 83 { 84 assert(std::isnan(r.real())); 85 assert(std::isnan(r.imag())); 86 } 87 else if (std::isnan(x[i].real()) && std::isnan(x[i].imag())) 88 { 89 assert(std::isnan(r.real())); 90 assert(std::isnan(r.imag())); 91 } 92 } 93 } 94 main()95int main() 96 { 97 test<float>(); 98 test<double>(); 99 test<long double>(); 100 test_edges(); 101 } 102