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