transc.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Boost example/transc.cpp
  2. * how to use an external library (GMP/MPFR in this case) in order to
  3. * get correct transcendental functions on intervals
  4. *
  5. * Copyright 2003 Guillaume Melquiond
  6. *
  7. * Distributed under the Boost Software License, Version 1.0.
  8. * (See accompanying file LICENSE_1_0.txt or
  9. * copy at http://www.boost.org/LICENSE_1_0.txt)
  10. */
  11. #include <boost/numeric/interval.hpp>
  12. //extern "C" {
  13. #include <gmp.h>
  14. #include <mpfr.h>
  15. //}
  16. #include <iostream>
  17. struct full_rounding:
  18. boost::numeric::interval_lib::rounded_arith_opp<double>
  19. {
  20. private:
  21. typedef int mpfr_func(mpfr_t, const __mpfr_struct*, mp_rnd_t);
  22. double invoke_mpfr(double x, mpfr_func f, mp_rnd_t r) {
  23. mpfr_t xx;
  24. mpfr_init_set_d(xx, x, GMP_RNDN);
  25. f(xx, xx, r);
  26. double res = mpfr_get_d(xx, r);
  27. mpfr_clear(xx);
  28. return res;
  29. }
  30. public:
  31. # define GENR_FUNC(name) \
  32. double name##_down(double x) { return invoke_mpfr(x, mpfr_##name, GMP_RNDD); } \
  33. double name##_up (double x) { return invoke_mpfr(x, mpfr_##name, GMP_RNDU); }
  34. GENR_FUNC(exp)
  35. GENR_FUNC(log)
  36. GENR_FUNC(sin)
  37. GENR_FUNC(cos)
  38. GENR_FUNC(tan)
  39. GENR_FUNC(asin)
  40. GENR_FUNC(acos)
  41. GENR_FUNC(atan)
  42. GENR_FUNC(sinh)
  43. GENR_FUNC(cosh)
  44. GENR_FUNC(tanh)
  45. GENR_FUNC(asinh)
  46. GENR_FUNC(acosh)
  47. GENR_FUNC(atanh)
  48. };
  49. namespace dummy {
  50. using namespace boost;
  51. using namespace numeric;
  52. using namespace interval_lib;
  53. typedef save_state<full_rounding> R;
  54. typedef checking_strict<double> P;
  55. typedef interval<double, policies<R, P> > I;
  56. };
  57. typedef dummy::I I;
  58. template<class os_t>
  59. os_t& operator<<(os_t &os, const I &a) {
  60. os << '[' << a.lower() << ',' << a.upper() << ']';
  61. return os;
  62. }
  63. int main() {
  64. I x(0.5, 2.5);
  65. std::cout << "x = " << x << std::endl;
  66. std::cout.precision(16);
  67. # define GENR_TEST(name) \
  68. std::cout << #name "(x) = " << name(x) << std::endl
  69. GENR_TEST(exp);
  70. GENR_TEST(log);
  71. GENR_TEST(sin);
  72. GENR_TEST(cos);
  73. GENR_TEST(tan);
  74. GENR_TEST(asin);
  75. GENR_TEST(acos);
  76. GENR_TEST(atan);
  77. GENR_TEST(sinh);
  78. GENR_TEST(cosh);
  79. GENR_TEST(tanh);
  80. GENR_TEST(asinh);
  81. GENR_TEST(acosh);
  82. GENR_TEST(atanh);
  83. return 0;
  84. }