The Finite Resolution Dynamics Software
rounding.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// @file rounding.h
4 ///
5 /// Rigorous rounding of arithmetic operations.
6 /// This file contains the definition of a class for doing arithmetic
7 /// operations with controlled rounding directions (upwards/downwards).
8 /// This class uses the BOOST library.
9 ///
10 /// @author Pawel Pilarczyk
11 ///
12 /////////////////////////////////////////////////////////////////////////////
13 
14 // Copyright (C) 2007-2010 by Pawel Pilarczyk.
15 //
16 // This file is part of my research program package. This is free software;
17 // you can redistribute it and/or modify it under the terms of the GNU
18 // General Public License as published by the Free Software Foundation;
19 // either version 2 of the License, or (at your option) any later version.
20 //
21 // This software is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License along
27 // with this software; see the file "license.txt". If not, write to the
28 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 // MA 02111-1307, USA.
30 
31 // Started on July 10, 2007. Last revision: April 12, 2010.
32 
33 
34 #ifndef _FINRESDYN_ROUNDING_H_
35 #define _FINRESDYN_ROUNDING_H_
36 
37 
38 // BOOST (this header must be included first)
39 #include "boost/numeric/interval.hpp"
40 
41 
42 // --------------------------------------------------
43 // -------------- rounding operations ---------------
44 // --------------------------------------------------
45 
46 /// A class for rounding operations which uses the BOOST library.
47 template <class NumType>
48 class tRounding
49 {
50 public:
51 /// This is an internal macro of the class template "tRounding"
52 /// which defines arithmetic operations using the operations
53 /// available in the BOOST library.
54 #define DEFOP(WHAT) \
55  static inline NumType WHAT (const NumType &x, const NumType &y) \
56  { \
57  NumType result = Rounding. WHAT (x, y); \
58  Rounding. to_nearest (); \
59  return result; \
60  }
61 
62  DEFOP(add_down)
63  DEFOP(add_up)
64  DEFOP(sub_down)
65  DEFOP(sub_up)
66  DEFOP(mul_down)
67  DEFOP(mul_up)
68  DEFOP(div_down)
69  DEFOP(div_up)
70 
71 #undef DEFOP
72 
73 /* NumType add_down (const NumType &x, const NumType &y) const
74  {
75  NumType result = Rounding. add_down (x, y);
76  Rounding. to_nearest ();
77  return result;
78  }
79 */
80 
81  /// The smallest positive representable number.
82  /// This function returns the smallest possible positive number,
83  /// which typically equals the machine precision and may be
84  /// implementation-dependent for different number types.
85  static NumType min_number ();
86 
87 private:
88  /// This is an object from the BOOST library which contains
89  /// the definitions of arithmetic operations with correct rounding.
90  static boost::numeric::interval_lib::rounded_arith_std<NumType>
92 }; /* class tRounding */
93 
94 // --------------------------------------------------
95 
96 template <>
98 {
99  return __FLT_DENORM_MIN__;
100 } /* min_number */
101 
102 template <>
104 {
105  return __DBL_DENORM_MIN__;
106 } /* min_number */
107 
108 template <>
110 {
111  return __LDBL_DENORM_MIN__;
112 } /* min_number */
113 
114 // --------------------------------------------------
115 
116 template <class NumType>
117 boost::numeric::interval_lib::rounded_arith_std<NumType>
119 
120 
121 #endif // _FINRESDYN_ROUNDING_H_
122 
static boost::numeric::interval_lib::rounded_arith_std< NumType > Rounding
This is an object from the BOOST library which contains the definitions of arithmetic operations with...
Definition: rounding.h:91
A class for rounding operations which uses the BOOST library.
Definition: rounding.h:48
static NumType min_number()
The smallest positive representable number.
#define DEFOP(WHAT)
This is an internal macro of the class template "tRounding" which defines arithmetic operations using...
Definition: rounding.h:54