pow_valarray_valarray.pass.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // <valarray>
  10. // template<class T> class valarray;
  11. // template<class T>
  12. // valarray<T>
  13. // pow(const valarray<T>& x, const valarray<T>& y);
  14. #include <valarray>
  15. #include <cassert>
  16. #include <sstream>
  17. bool is_about(double x, double y, int p)
  18. {
  19. std::ostringstream o;
  20. o.precision(p);
  21. scientific(o);
  22. o << x;
  23. std::string a = o.str();
  24. o.str("");
  25. o << y;
  26. return a == o.str();
  27. }
  28. int main()
  29. {
  30. {
  31. typedef double T;
  32. T a1[] = {.9, .5, 0., .5, .75};
  33. T a2[] = {-.8, .25, 0.375, -.5, .75};
  34. T a3[] = {1.0879426248455297e+00,
  35. 8.4089641525371450e-01,
  36. 0.0000000000000000e+00,
  37. 1.4142135623730949e+00,
  38. 8.0592744886765644e-01};
  39. const unsigned N = sizeof(a1)/sizeof(a1[0]);
  40. std::valarray<T> v1(a1, N);
  41. std::valarray<T> v2(a2, N);
  42. std::valarray<T> v3 = pow(v1, v2);
  43. assert(v3.size() == v1.size());
  44. for (int i = 0; i < v3.size(); ++i)
  45. assert(is_about(v3[i], a3[i], 10));
  46. }
  47. }