Chris@16: /* Boost interval/rounded_arith.hpp template implementation file Chris@16: * Chris@16: * Copyright 2002-2003 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion Chris@16: * Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or Chris@16: * copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_NUMERIC_INTERVAL_ROUNDED_ARITH_HPP Chris@16: #define BOOST_NUMERIC_INTERVAL_ROUNDED_ARITH_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace numeric { Chris@16: namespace interval_lib { Chris@16: Chris@16: /* Chris@16: * Three classes of rounding: exact, std, opp Chris@16: * See documentation for details. Chris@16: */ Chris@16: Chris@16: template Chris@16: struct rounded_arith_exact: Rounding { Chris@16: void init() { } Chris@16: template T conv_down(U const &v) { return v; } Chris@16: template T conv_up (U const &v) { return v; } Chris@16: T add_down (const T& x, const T& y) { return x + y; } Chris@16: T add_up (const T& x, const T& y) { return x + y; } Chris@16: T sub_down (const T& x, const T& y) { return x - y; } Chris@16: T sub_up (const T& x, const T& y) { return x - y; } Chris@16: T mul_down (const T& x, const T& y) { return x * y; } Chris@16: T mul_up (const T& x, const T& y) { return x * y; } Chris@16: T div_down (const T& x, const T& y) { return x / y; } Chris@16: T div_up (const T& x, const T& y) { return x / y; } Chris@16: T median (const T& x, const T& y) { return (x + y) / 2; } Chris@16: T sqrt_down(const T& x) Chris@16: { BOOST_NUMERIC_INTERVAL_using_math(sqrt); return sqrt(x); } Chris@16: T sqrt_up (const T& x) Chris@16: { BOOST_NUMERIC_INTERVAL_using_math(sqrt); return sqrt(x); } Chris@16: T int_down (const T& x) Chris@16: { BOOST_NUMERIC_INTERVAL_using_math(floor); return floor(x); } Chris@16: T int_up (const T& x) Chris@16: { BOOST_NUMERIC_INTERVAL_using_math(ceil); return ceil(x); } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct rounded_arith_std: Rounding { Chris@16: # define BOOST_DN(EXPR) this->downward(); return this->force_rounding(EXPR) Chris@16: # define BOOST_NR(EXPR) this->to_nearest(); return this->force_rounding(EXPR) Chris@16: # define BOOST_UP(EXPR) this->upward(); return this->force_rounding(EXPR) Chris@16: void init() { } Chris@16: template T conv_down(U const &v) { BOOST_DN(v); } Chris@16: template T conv_up (U const &v) { BOOST_UP(v); } Chris@16: T add_down(const T& x, const T& y) { BOOST_DN(x + y); } Chris@16: T sub_down(const T& x, const T& y) { BOOST_DN(x - y); } Chris@16: T mul_down(const T& x, const T& y) { BOOST_DN(x * y); } Chris@16: T div_down(const T& x, const T& y) { BOOST_DN(x / y); } Chris@16: T add_up (const T& x, const T& y) { BOOST_UP(x + y); } Chris@16: T sub_up (const T& x, const T& y) { BOOST_UP(x - y); } Chris@16: T mul_up (const T& x, const T& y) { BOOST_UP(x * y); } Chris@16: T div_up (const T& x, const T& y) { BOOST_UP(x / y); } Chris@16: T median(const T& x, const T& y) { BOOST_NR((x + y) / 2); } Chris@16: T sqrt_down(const T& x) Chris@16: { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_DN(sqrt(x)); } Chris@16: T sqrt_up (const T& x) Chris@16: { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_UP(sqrt(x)); } Chris@101: T int_down(const T& x) { this->downward(); return this->to_int(x); } Chris@101: T int_up (const T& x) { this->upward(); return this->to_int(x); } Chris@16: # undef BOOST_DN Chris@16: # undef BOOST_NR Chris@16: # undef BOOST_UP Chris@16: }; Chris@16: Chris@16: template Chris@16: struct rounded_arith_opp: Rounding { Chris@16: void init() { this->upward(); } Chris@16: # define BOOST_DN(EXPR) \ Chris@16: this->downward(); \ Chris@16: T r = this->force_rounding(EXPR); \ Chris@16: this->upward(); \ Chris@16: return r Chris@16: # define BOOST_NR(EXPR) \ Chris@16: this->to_nearest(); \ Chris@16: T r = this->force_rounding(EXPR); \ Chris@16: this->upward(); \ Chris@16: return r Chris@16: # define BOOST_UP(EXPR) return this->force_rounding(EXPR) Chris@16: # define BOOST_UP_NEG(EXPR) return -this->force_rounding(EXPR) Chris@16: template T conv_down(U const &v) { BOOST_UP_NEG(-v); } Chris@16: template T conv_up (U const &v) { BOOST_UP(v); } Chris@16: T add_down(const T& x, const T& y) { BOOST_UP_NEG((-x) - y); } Chris@16: T sub_down(const T& x, const T& y) { BOOST_UP_NEG(y - x); } Chris@16: T mul_down(const T& x, const T& y) { BOOST_UP_NEG(x * (-y)); } Chris@16: T div_down(const T& x, const T& y) { BOOST_UP_NEG(x / (-y)); } Chris@16: T add_up (const T& x, const T& y) { BOOST_UP(x + y); } Chris@16: T sub_up (const T& x, const T& y) { BOOST_UP(x - y); } Chris@16: T mul_up (const T& x, const T& y) { BOOST_UP(x * y); } Chris@16: T div_up (const T& x, const T& y) { BOOST_UP(x / y); } Chris@16: T median (const T& x, const T& y) { BOOST_NR((x + y) / 2); } Chris@16: T sqrt_down(const T& x) Chris@16: { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_DN(sqrt(x)); } Chris@16: T sqrt_up (const T& x) Chris@16: { BOOST_NUMERIC_INTERVAL_using_math(sqrt); BOOST_UP(sqrt(x)); } Chris@101: T int_down(const T& x) { return -this->to_int(-x); } Chris@101: T int_up (const T& x) { return this->to_int(x); } Chris@16: # undef BOOST_DN Chris@16: # undef BOOST_NR Chris@16: # undef BOOST_UP Chris@16: # undef BOOST_UP_NEG Chris@16: }; Chris@16: Chris@16: } // namespace interval_lib Chris@16: } // namespace numeric Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_NUMERIC_INTERVAL_ROUNDED_ARITH_HPP