1 | /* Boost interval/rounded_arith.hpp template implementation file
|
---|
2 | *
|
---|
3 | * Copyright 2002-2003 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
|
---|
4 | *
|
---|
5 | * Distributed under the Boost Software License, Version 1.0.
|
---|
6 | * (See accompanying file LICENSE_1_0.txt or
|
---|
7 | * copy at http://www.boost.org/LICENSE_1_0.txt)
|
---|
8 | */
|
---|
9 |
|
---|
10 | #ifndef BOOST_NUMERIC_INTERVAL_ROUNDED_ARITH_HPP
|
---|
11 | #define BOOST_NUMERIC_INTERVAL_ROUNDED_ARITH_HPP
|
---|
12 |
|
---|
13 | #include <boost/numeric/interval/rounding.hpp>
|
---|
14 | #include <boost/numeric/interval/detail/bugs.hpp>
|
---|
15 | #include <cmath>
|
---|
16 |
|
---|
17 | namespace boost {
|
---|
18 | namespace numeric {
|
---|
19 | namespace interval_lib {
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Three classes of rounding: exact, std, opp
|
---|
23 | * See documentation for details.
|
---|
24 | */
|
---|
25 |
|
---|
26 | template<class T, class Rounding>
|
---|
27 | struct rounded_arith_exact: Rounding {
|
---|
28 | void init() { }
|
---|
29 | template<class U> T conv_down(U const &v) { return v; }
|
---|
30 | template<class U> T conv_up (U const &v) { return v; }
|
---|
31 | T add_down (const T& x, const T& y) { return x + y; }
|
---|
32 | T add_up (const T& x, const T& y) { return x + y; }
|
---|
33 | T sub_down (const T& x, const T& y) { return x - y; }
|
---|
34 | T sub_up (const T& x, const T& y) { return x - y; }
|
---|
35 | T mul_down (const T& x, const T& y) { return x * y; }
|
---|
36 | T mul_up (const T& x, const T& y) { return x * y; }
|
---|
37 | T div_down (const T& x, const T& y) { return x / y; }
|
---|
38 | T div_up (const T& x, const T& y) { return x / y; }
|
---|
39 | T median (const T& x, const T& y) { return (x + y) / 2; }
|
---|
40 | T sqrt_down(const T& x)
|
---|
41 | { BOOST_NUMERIC_INTERVAL_using_math(sqrt); return sqrt(x); }
|
---|
42 | T sqrt_up (const T& x)
|
---|
43 | { BOOST_NUMERIC_INTERVAL_using_math(sqrt); return sqrt(x); }
|
---|
44 | T int_down (const T& x)
|
---|
45 | { BOOST_NUMERIC_INTERVAL_using_math(floor); return floor(x); }
|
---|
46 | T int_up (const T& x)
|
---|
47 | { BOOST_NUMERIC_INTERVAL_using_math(ceil); return ceil(x); }
|
---|
48 | };
|
---|
49 |
|
---|
50 | template<class T, class Rounding>
|
---|
51 | struct rounded_arith_std: Rounding {
|
---|
52 | # define BOOST_DN(EXPR) this->downward(); return this->force_rounding(EXPR)
|
---|
53 | # define BOOST_NR(EXPR) this->to_nearest(); return this->force_rounding(EXPR)
|
---|
54 | # define BOOST_UP(EXPR) this->upward(); return this->force_rounding(EXPR)
|
---|
55 | void init() { }
|
---|
56 | template<class U> T conv_down(U const &v) { BOOST_DN(v); }
|
---|
57 | template<class U> T conv_up (U const &v) { BOOST_UP(v); }
|
---|
58 | T add_down(const T& x, const T& y) { BOOST_DN(x + y); }
|
---|
59 | T sub_down(const T& x, const T& y) { BOOST_DN(x - y); }
|
---|
60 | T mul_down(const T& x, const T& y) { BOOST_DN(x * y); }
|
---|
61 | T div_down(const T& x, const T& y) { BOOST_DN(x / y); }
|
---|
62 | T add_up (const T& x, const T& y) { BOOST_UP(x + y); }
|
---|
63 | T sub_up (const T& x, const T& y) { BOOST_UP(x - y); }
|
---|
64 | T mul_up (const T& x, const T& y) { BOOST_UP(x * y); }
|
---|
65 | T div_up (const T& x, const T& y) { BOOST_UP(x / y); }
|
---|
66 | T median(const T& x, const T& y) { BOOST_NR((x + y) / 2); }
|
---|
67 | T sqrt_down(const T& x)
|
---|
68 | { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_DN(sqrt(x)); }
|
---|
69 | T sqrt_up (const T& x)
|
---|
70 | { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_UP(sqrt(x)); }
|
---|
71 | T int_down(const T& x) { this->downward(); return to_int(x); }
|
---|
72 | T int_up (const T& x) { this->upward(); return to_int(x); }
|
---|
73 | # undef BOOST_DN
|
---|
74 | # undef BOOST_NR
|
---|
75 | # undef BOOST_UP
|
---|
76 | };
|
---|
77 |
|
---|
78 | template<class T, class Rounding>
|
---|
79 | struct rounded_arith_opp: Rounding {
|
---|
80 | void init() { this->upward(); }
|
---|
81 | # define BOOST_DN(EXPR) \
|
---|
82 | this->downward(); \
|
---|
83 | T r = this->force_rounding(EXPR); \
|
---|
84 | this->upward(); \
|
---|
85 | return r
|
---|
86 | # define BOOST_NR(EXPR) \
|
---|
87 | this->to_nearest(); \
|
---|
88 | T r = this->force_rounding(EXPR); \
|
---|
89 | this->upward(); \
|
---|
90 | return r
|
---|
91 | # define BOOST_UP(EXPR) return this->force_rounding(EXPR)
|
---|
92 | # define BOOST_UP_NEG(EXPR) return -this->force_rounding(EXPR)
|
---|
93 | template<class U> T conv_down(U const &v) { BOOST_UP_NEG(-v); }
|
---|
94 | template<class U> T conv_up (U const &v) { BOOST_UP(v); }
|
---|
95 | T add_down(const T& x, const T& y) { BOOST_UP_NEG((-x) - y); }
|
---|
96 | T sub_down(const T& x, const T& y) { BOOST_UP_NEG(y - x); }
|
---|
97 | T mul_down(const T& x, const T& y) { BOOST_UP_NEG(x * (-y)); }
|
---|
98 | T div_down(const T& x, const T& y) { BOOST_UP_NEG(x / (-y)); }
|
---|
99 | T add_up (const T& x, const T& y) { BOOST_UP(x + y); }
|
---|
100 | T sub_up (const T& x, const T& y) { BOOST_UP(x - y); }
|
---|
101 | T mul_up (const T& x, const T& y) { BOOST_UP(x * y); }
|
---|
102 | T div_up (const T& x, const T& y) { BOOST_UP(x / y); }
|
---|
103 | T median (const T& x, const T& y) { BOOST_NR((x + y) / 2); }
|
---|
104 | T sqrt_down(const T& x)
|
---|
105 | { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_DN(sqrt(x)); }
|
---|
106 | T sqrt_up (const T& x)
|
---|
107 | { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_UP(sqrt(x)); }
|
---|
108 | T int_down(const T& x) { return -to_int(-x); }
|
---|
109 | T int_up (const T& x) { return to_int(x); }
|
---|
110 | # undef BOOST_DN
|
---|
111 | # undef BOOST_NR
|
---|
112 | # undef BOOST_UP
|
---|
113 | # undef BOOST_UP_NEG
|
---|
114 | };
|
---|
115 |
|
---|
116 | } // namespace interval_lib
|
---|
117 | } // namespace numeric
|
---|
118 | } // namespace boost
|
---|
119 |
|
---|
120 | #endif // BOOST_NUMERIC_INTERVAL_ROUNDED_ARITH_HPP
|
---|