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 | |
---|
12 | #include <boost/numeric/interval.hpp> |
---|
13 | //extern "C" { |
---|
14 | #include <gmp.h> |
---|
15 | #include <mpfr.h> |
---|
16 | //} |
---|
17 | #include <iostream> |
---|
18 | |
---|
19 | struct full_rounding: |
---|
20 | boost::numeric::interval_lib::rounded_arith_opp<double> |
---|
21 | { |
---|
22 | private: |
---|
23 | typedef int mpfr_func(mpfr_t, const __mpfr_struct*, mp_rnd_t); |
---|
24 | double invoke_mpfr(double x, mpfr_func f, mp_rnd_t r) { |
---|
25 | mpfr_t xx; |
---|
26 | mpfr_init_set_d(xx, x, GMP_RNDN); |
---|
27 | f(xx, xx, r); |
---|
28 | double res = mpfr_get_d(xx, r); |
---|
29 | mpfr_clear(xx); |
---|
30 | return res; |
---|
31 | } |
---|
32 | public: |
---|
33 | # define GENR_FUNC(name) \ |
---|
34 | double name##_down(double x) { return invoke_mpfr(x, mpfr_##name, GMP_RNDD); } \ |
---|
35 | double name##_up (double x) { return invoke_mpfr(x, mpfr_##name, GMP_RNDU); } |
---|
36 | GENR_FUNC(exp) |
---|
37 | GENR_FUNC(log) |
---|
38 | GENR_FUNC(sin) |
---|
39 | GENR_FUNC(cos) |
---|
40 | GENR_FUNC(tan) |
---|
41 | GENR_FUNC(asin) |
---|
42 | GENR_FUNC(acos) |
---|
43 | GENR_FUNC(atan) |
---|
44 | GENR_FUNC(sinh) |
---|
45 | GENR_FUNC(cosh) |
---|
46 | GENR_FUNC(tanh) |
---|
47 | GENR_FUNC(asinh) |
---|
48 | GENR_FUNC(acosh) |
---|
49 | GENR_FUNC(atanh) |
---|
50 | }; |
---|
51 | |
---|
52 | namespace dummy { |
---|
53 | using namespace boost; |
---|
54 | using namespace numeric; |
---|
55 | using namespace interval_lib; |
---|
56 | typedef save_state<full_rounding> R; |
---|
57 | typedef checking_strict<double> P; |
---|
58 | typedef interval<double, policies<R, P> > I; |
---|
59 | }; |
---|
60 | |
---|
61 | typedef dummy::I I; |
---|
62 | |
---|
63 | template<class os_t> |
---|
64 | os_t& operator<<(os_t &os, const I &a) { |
---|
65 | os << '[' << a.lower() << ',' << a.upper() << ']'; |
---|
66 | return os; |
---|
67 | } |
---|
68 | |
---|
69 | int main() { |
---|
70 | I x(0.5, 2.5); |
---|
71 | std::cout << "x = " << x << std::endl; |
---|
72 | std::cout.precision(16); |
---|
73 | # define GENR_TEST(name) \ |
---|
74 | std::cout << #name "(x) = " << name(x) << std::endl |
---|
75 | GENR_TEST(exp); |
---|
76 | GENR_TEST(log); |
---|
77 | GENR_TEST(sin); |
---|
78 | GENR_TEST(cos); |
---|
79 | GENR_TEST(tan); |
---|
80 | GENR_TEST(asin); |
---|
81 | GENR_TEST(acos); |
---|
82 | GENR_TEST(atan); |
---|
83 | GENR_TEST(sinh); |
---|
84 | GENR_TEST(cosh); |
---|
85 | GENR_TEST(tanh); |
---|
86 | GENR_TEST(asinh); |
---|
87 | GENR_TEST(acosh); |
---|
88 | GENR_TEST(atanh); |
---|
89 | return 0; |
---|
90 | } |
---|