this repo has no description
at develop 120 lines 5.2 kB view raw
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 GECODE_BOOST_NUMERIC_INTERVAL_ROUNDED_ARITH_HPP 11#define GECODE_BOOST_NUMERIC_INTERVAL_ROUNDED_ARITH_HPP 12 13#include <gecode/third-party/boost/numeric/interval/rounding.hpp> 14#include <gecode/third-party/boost/numeric/interval/detail/bugs.hpp> 15#include <gecode/third-party/boost/config/no_tr1/cmath.hpp> 16 17namespace gecode_boost { 18namespace numeric { 19namespace interval_lib { 20 21/* 22 * Three classes of rounding: exact, std, opp 23 * See documentation for details. 24 */ 25 26template<class T, class Rounding> 27struct 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 { GECODE_BOOST_NUMERIC_INTERVAL_using_math(sqrt); return sqrt(x); } 42 T sqrt_up (const T& x) 43 { GECODE_BOOST_NUMERIC_INTERVAL_using_math(sqrt); return sqrt(x); } 44 T int_down (const T& x) 45 { GECODE_BOOST_NUMERIC_INTERVAL_using_math(floor); return floor(x); } 46 T int_up (const T& x) 47 { GECODE_BOOST_NUMERIC_INTERVAL_using_math(ceil); return ceil(x); } 48}; 49 50template<class T, class Rounding> 51struct rounded_arith_std: Rounding { 52# define GECODE_BOOST_DN(EXPR) this->downward(); return this->force_rounding(EXPR) 53# define GECODE_BOOST_NR(EXPR) this->to_nearest(); return this->force_rounding(EXPR) 54# define GECODE_BOOST_UP(EXPR) this->upward(); return this->force_rounding(EXPR) 55 void init() { } 56 template<class U> T conv_down(U const &v) { GECODE_BOOST_DN(v); } 57 template<class U> T conv_up (U const &v) { GECODE_BOOST_UP(v); } 58 T add_down(const T& x, const T& y) { GECODE_BOOST_DN(x + y); } 59 T sub_down(const T& x, const T& y) { GECODE_BOOST_DN(x - y); } 60 T mul_down(const T& x, const T& y) { GECODE_BOOST_DN(x * y); } 61 T div_down(const T& x, const T& y) { GECODE_BOOST_DN(x / y); } 62 T add_up (const T& x, const T& y) { GECODE_BOOST_UP(x + y); } 63 T sub_up (const T& x, const T& y) { GECODE_BOOST_UP(x - y); } 64 T mul_up (const T& x, const T& y) { GECODE_BOOST_UP(x * y); } 65 T div_up (const T& x, const T& y) { GECODE_BOOST_UP(x / y); } 66 T median(const T& x, const T& y) { GECODE_BOOST_NR((x + y) / 2); } 67 T sqrt_down(const T& x) 68 { GECODE_BOOST_NUMERIC_INTERVAL_using_math(sqrt); GECODE_BOOST_DN(sqrt(x)); } 69 T sqrt_up (const T& x) 70 { GECODE_BOOST_NUMERIC_INTERVAL_using_math(sqrt); GECODE_BOOST_UP(sqrt(x)); } 71 T int_down(const T& x) { this->downward(); return this->to_int(x); } 72 T int_up (const T& x) { this->upward(); return this->to_int(x); } 73# undef GECODE_BOOST_DN 74# undef GECODE_BOOST_NR 75# undef GECODE_BOOST_UP 76}; 77 78template<class T, class Rounding> 79struct rounded_arith_opp: Rounding { 80 void init() { this->upward(); } 81# define GECODE_BOOST_DN(EXPR) \ 82 this->downward(); \ 83 T r = this->force_rounding(EXPR); \ 84 this->upward(); \ 85 return r 86# define GECODE_BOOST_NR(EXPR) \ 87 this->to_nearest(); \ 88 T r = this->force_rounding(EXPR); \ 89 this->upward(); \ 90 return r 91# define GECODE_BOOST_UP(EXPR) return this->force_rounding(EXPR) 92# define GECODE_BOOST_UP_NEG(EXPR) return -this->force_rounding(EXPR) 93 template<class U> T conv_down(U const &v) { GECODE_BOOST_UP_NEG(-v); } 94 template<class U> T conv_up (U const &v) { GECODE_BOOST_UP(v); } 95 T add_down(const T& x, const T& y) { GECODE_BOOST_UP_NEG((-x) - y); } 96 T sub_down(const T& x, const T& y) { GECODE_BOOST_UP_NEG(y - x); } 97 T mul_down(const T& x, const T& y) { GECODE_BOOST_UP_NEG(x * (-y)); } 98 T div_down(const T& x, const T& y) { GECODE_BOOST_UP_NEG(x / (-y)); } 99 T add_up (const T& x, const T& y) { GECODE_BOOST_UP(x + y); } 100 T sub_up (const T& x, const T& y) { GECODE_BOOST_UP(x - y); } 101 T mul_up (const T& x, const T& y) { GECODE_BOOST_UP(x * y); } 102 T div_up (const T& x, const T& y) { GECODE_BOOST_UP(x / y); } 103 T median (const T& x, const T& y) { GECODE_BOOST_NR((x + y) / 2); } 104 T sqrt_down(const T& x) 105 { GECODE_BOOST_NUMERIC_INTERVAL_using_math(sqrt); GECODE_BOOST_DN(sqrt(x)); } 106 T sqrt_up (const T& x) 107 { GECODE_BOOST_NUMERIC_INTERVAL_using_math(sqrt); GECODE_BOOST_UP(sqrt(x)); } 108 T int_down(const T& x) { return -this->to_int(-x); } 109 T int_up (const T& x) { return this->to_int(x); } 110# undef GECODE_BOOST_DN 111# undef GECODE_BOOST_NR 112# undef GECODE_BOOST_UP 113# undef GECODE_BOOST_UP_NEG 114}; 115 116} // namespace interval_lib 117} // namespace numeric 118} // namespace gecode_boost 119 120#endif // GECODE_BOOST_NUMERIC_INTERVAL_ROUNDED_ARITH_HPP