1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // <complex> 10 11 // template<class T> 12 // complex<T> 13 // log(const complex<T>& x); 14 15 #include <complex> 16 #include <cassert> 17 18 #include "../cases.h" 19 20 template <class T> 21 void 22 test(const std::complex<T>& c, std::complex<T> x) 23 { 24 assert(log(c) == x); 25 } 26 27 template <class T> 28 void 29 test() 30 { 31 test(std::complex<T>(0, 0), std::complex<T>(-INFINITY, 0)); 32 } 33 34 void test_edges() 35 { 36 const double pi = std::atan2(+0., -0.); 37 const unsigned N = sizeof(testcases) / sizeof(testcases[0]); 38 for (unsigned i = 0; i < N; ++i) 39 { 40 std::complex<double> r = log(testcases[i]); 41 if (testcases[i].real() == 0 && testcases[i].imag() == 0) 42 { 43 if (std::signbit(testcases[i].real())) 44 { 45 assert(std::isinf(r.real())); 46 assert(r.real() < 0); 47 if (std::signbit(testcases[i].imag())) 48 is_about(r.imag(), -pi); 49 else 50 is_about(r.imag(), pi); 51 } 52 else 53 { 54 assert(std::isinf(r.real())); 55 assert(r.real() < 0); 56 assert(r.imag() == 0); 57 assert(std::signbit(testcases[i].imag()) == std::signbit(r.imag())); 58 } 59 } 60 else if (std::isfinite(testcases[i].real()) && std::isinf(testcases[i].imag())) 61 { 62 assert(std::isinf(r.real())); 63 assert(r.real() > 0); 64 if (testcases[i].imag() > 0) 65 is_about(r.imag(), pi/2); 66 else 67 is_about(r.imag(), -pi/2); 68 } 69 else if (std::isfinite(testcases[i].real()) && std::isnan(testcases[i].imag())) 70 { 71 assert(std::isnan(r.real())); 72 assert(std::isnan(r.imag())); 73 } 74 else if (std::isinf(testcases[i].real()) && testcases[i].real() < 0 && std::isfinite(testcases[i].imag())) 75 { 76 assert(std::isinf(r.real()) && r.real() > 0); 77 if (r.imag() > 0) 78 is_about(r.imag(), pi); 79 else 80 is_about(r.imag(), -pi); 81 } 82 else if (std::isinf(testcases[i].real()) && testcases[i].real() > 0 && std::isfinite(testcases[i].imag())) 83 { 84 assert(std::isinf(r.real()) && r.real() > 0); 85 assert(r.imag() == 0); 86 assert(std::signbit(testcases[i].imag()) == std::signbit(r.imag())); 87 } 88 else if (testcases[i].real() == 1 && testcases[i].imag() == 0) 89 { 90 assert(r.real() == 0); 91 assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); 92 } 93 else if (testcases[i].real() == 0 && testcases[i].imag() == 1) 94 { 95 assert(r.real() == 0); 96 is_about(r.imag(), pi/2); 97 } 98 else if (testcases[i].real() == -1 && testcases[i].imag() == 0) 99 { 100 assert(r.real() == 0); 101 if (std::signbit(testcases[i].imag())) 102 is_about(r.imag(), -pi); 103 else 104 is_about(r.imag(), pi); 105 } 106 else if (testcases[i].real() == 0 && testcases[i].imag() == -1) 107 { 108 assert(r.real() == 0); 109 is_about(r.imag(), -pi/2); 110 } 111 else if (std::isfinite(testcases[i].real()) && std::isfinite(testcases[i].imag()) && abs(testcases[i]) < 1) 112 { 113 assert( std::signbit(r.real())); 114 assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); 115 } 116 else if (std::isfinite(testcases[i].real()) && std::isfinite(testcases[i].imag()) && abs(testcases[i]) > 1) 117 { 118 assert(!std::signbit(r.real())); 119 assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag())); 120 } 121 } 122 } 123 124 int main(int, char**) 125 { 126 test<float>(); 127 test<double>(); 128 test<long double>(); 129 test_edges(); 130 131 return 0; 132 } 133