• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <boost/config/no_tr1/cmath.hpp>
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 {
initboost::numeric::interval_lib::rounded_arith_exact28   void init() { }
conv_downboost::numeric::interval_lib::rounded_arith_exact29   template<class U> T conv_down(U const &v) { return v; }
conv_upboost::numeric::interval_lib::rounded_arith_exact30   template<class U> T conv_up  (U const &v) { return v; }
add_downboost::numeric::interval_lib::rounded_arith_exact31   T add_down (const T& x, const T& y) { return x + y; }
add_upboost::numeric::interval_lib::rounded_arith_exact32   T add_up   (const T& x, const T& y) { return x + y; }
sub_downboost::numeric::interval_lib::rounded_arith_exact33   T sub_down (const T& x, const T& y) { return x - y; }
sub_upboost::numeric::interval_lib::rounded_arith_exact34   T sub_up   (const T& x, const T& y) { return x - y; }
mul_downboost::numeric::interval_lib::rounded_arith_exact35   T mul_down (const T& x, const T& y) { return x * y; }
mul_upboost::numeric::interval_lib::rounded_arith_exact36   T mul_up   (const T& x, const T& y) { return x * y; }
div_downboost::numeric::interval_lib::rounded_arith_exact37   T div_down (const T& x, const T& y) { return x / y; }
div_upboost::numeric::interval_lib::rounded_arith_exact38   T div_up   (const T& x, const T& y) { return x / y; }
medianboost::numeric::interval_lib::rounded_arith_exact39   T median   (const T& x, const T& y) { return (x + y) / 2; }
sqrt_downboost::numeric::interval_lib::rounded_arith_exact40   T sqrt_down(const T& x)
41   { BOOST_NUMERIC_INTERVAL_using_math(sqrt); return sqrt(x); }
sqrt_upboost::numeric::interval_lib::rounded_arith_exact42   T sqrt_up  (const T& x)
43   { BOOST_NUMERIC_INTERVAL_using_math(sqrt); return sqrt(x); }
int_downboost::numeric::interval_lib::rounded_arith_exact44   T int_down (const T& x)
45   { BOOST_NUMERIC_INTERVAL_using_math(floor); return floor(x); }
int_upboost::numeric::interval_lib::rounded_arith_exact46   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)
initboost::numeric::interval_lib::rounded_arith_std55   void init() { }
conv_downboost::numeric::interval_lib::rounded_arith_std56   template<class U> T conv_down(U const &v) { BOOST_DN(v); }
conv_upboost::numeric::interval_lib::rounded_arith_std57   template<class U> T conv_up  (U const &v) { BOOST_UP(v); }
add_downboost::numeric::interval_lib::rounded_arith_std58   T add_down(const T& x, const T& y) { BOOST_DN(x + y); }
sub_downboost::numeric::interval_lib::rounded_arith_std59   T sub_down(const T& x, const T& y) { BOOST_DN(x - y); }
mul_downboost::numeric::interval_lib::rounded_arith_std60   T mul_down(const T& x, const T& y) { BOOST_DN(x * y); }
div_downboost::numeric::interval_lib::rounded_arith_std61   T div_down(const T& x, const T& y) { BOOST_DN(x / y); }
add_upboost::numeric::interval_lib::rounded_arith_std62   T add_up  (const T& x, const T& y) { BOOST_UP(x + y); }
sub_upboost::numeric::interval_lib::rounded_arith_std63   T sub_up  (const T& x, const T& y) { BOOST_UP(x - y); }
mul_upboost::numeric::interval_lib::rounded_arith_std64   T mul_up  (const T& x, const T& y) { BOOST_UP(x * y); }
div_upboost::numeric::interval_lib::rounded_arith_std65   T div_up  (const T& x, const T& y) { BOOST_UP(x / y); }
medianboost::numeric::interval_lib::rounded_arith_std66   T median(const T& x, const T& y) { BOOST_NR((x + y) / 2); }
sqrt_downboost::numeric::interval_lib::rounded_arith_std67   T sqrt_down(const T& x)
68   { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_DN(sqrt(x)); }
sqrt_upboost::numeric::interval_lib::rounded_arith_std69   T sqrt_up  (const T& x)
70   { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_UP(sqrt(x)); }
int_downboost::numeric::interval_lib::rounded_arith_std71   T int_down(const T& x) { this->downward(); return this->to_int(x); }
int_upboost::numeric::interval_lib::rounded_arith_std72   T int_up  (const T& x) { this->upward();   return this->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 {
initboost::numeric::interval_lib::rounded_arith_opp80   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)
conv_downboost::numeric::interval_lib::rounded_arith_opp93   template<class U> T conv_down(U const &v) { BOOST_UP_NEG(-v); }
conv_upboost::numeric::interval_lib::rounded_arith_opp94   template<class U> T conv_up  (U const &v) { BOOST_UP(v); }
add_downboost::numeric::interval_lib::rounded_arith_opp95   T add_down(const T& x, const T& y) { BOOST_UP_NEG((-x) - y); }
sub_downboost::numeric::interval_lib::rounded_arith_opp96   T sub_down(const T& x, const T& y) { BOOST_UP_NEG(y - x); }
mul_downboost::numeric::interval_lib::rounded_arith_opp97   T mul_down(const T& x, const T& y) { BOOST_UP_NEG(x * (-y)); }
div_downboost::numeric::interval_lib::rounded_arith_opp98   T div_down(const T& x, const T& y) { BOOST_UP_NEG(x / (-y)); }
add_upboost::numeric::interval_lib::rounded_arith_opp99   T add_up  (const T& x, const T& y) { BOOST_UP(x + y); }
sub_upboost::numeric::interval_lib::rounded_arith_opp100   T sub_up  (const T& x, const T& y) { BOOST_UP(x - y); }
mul_upboost::numeric::interval_lib::rounded_arith_opp101   T mul_up  (const T& x, const T& y) { BOOST_UP(x * y); }
div_upboost::numeric::interval_lib::rounded_arith_opp102   T div_up  (const T& x, const T& y) { BOOST_UP(x / y); }
medianboost::numeric::interval_lib::rounded_arith_opp103   T median  (const T& x, const T& y) { BOOST_NR((x + y) / 2); }
sqrt_downboost::numeric::interval_lib::rounded_arith_opp104   T sqrt_down(const T& x)
105   { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_DN(sqrt(x)); }
sqrt_upboost::numeric::interval_lib::rounded_arith_opp106   T sqrt_up  (const T& x)
107   { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_UP(sqrt(x)); }
int_downboost::numeric::interval_lib::rounded_arith_opp108   T int_down(const T& x) { return -this->to_int(-x); }
int_upboost::numeric::interval_lib::rounded_arith_opp109   T int_up  (const T& x) { return  this->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
121