brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 6022fdd Raw
71 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <complex>10 11// template<class T>12//   complex<T>13//   pow(const T& x, const complex<T>& y);14 15#include <complex>16#include <cassert>17 18#include "test_macros.h"19#include "../cases.h"20 21template <class T>22void23test(const T& a, const std::complex<T>& b, std::complex<T> x)24{25    std::complex<T> c = pow(a, b);26    is_about(real(c), real(x));27    assert(std::abs(imag(c)) < 1.e-6);28}29 30template <class T>31void32test()33{34    test(T(2), std::complex<T>(2), std::complex<T>(4));35}36 37void test_edges()38{39    const unsigned N = sizeof(testcases) / sizeof(testcases[0]);40    for (unsigned i = 0; i < N; ++i)41    {42        for (unsigned j = 0; j < N; ++j)43        {44            std::complex<double> r = pow(real(testcases[i]), testcases[j]);45            std::complex<double> z = exp(testcases[j] * log(std::complex<double>(real(testcases[i]))));46            if (std::isnan(real(r)))47                assert(std::isnan(real(z)));48            else49            {50                assert(real(r) == real(z));51            }52            if (std::isnan(imag(r)))53                assert(std::isnan(imag(z)));54            else55            {56                assert(imag(r) == imag(z));57            }58        }59    }60}61 62int main(int, char**)63{64    test<float>();65    test<double>();66    test<long double>();67    test_edges();68 69  return 0;70}71