rounding.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 ///
00003 /// @file rounding.h
00004 ///
00005 /// Rigorous rounding of arithmetic operations.
00006 /// This file contains the definition of a class for doing arithmetic
00007 /// operations with controlled rounding directions (upwards/downwards).
00008 /// This class uses the BOOST library.
00009 ///
00010 /// @author Pawel Pilarczyk
00011 ///
00012 /////////////////////////////////////////////////////////////////////////////
00013 
00014 // Copyright (C) 2007-2010 by Pawel Pilarczyk.
00015 //
00016 // This file is part of my research program package.  This is free software;
00017 // you can redistribute it and/or modify it under the terms of the GNU
00018 // General Public License as published by the Free Software Foundation;
00019 // either version 2 of the License, or (at your option) any later version.
00020 //
00021 // This software is distributed in the hope that it will be useful,
00022 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 // GNU General Public License for more details.
00025 //
00026 // You should have received a copy of the GNU General Public License along
00027 // with this software; see the file "license.txt".  If not, write to the
00028 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00029 // MA 02111-1307, USA.
00030 
00031 // Started on July 10, 2007. Last revision: April 12, 2010.
00032 
00033 
00034 #ifndef _FINRESDYN_ROUNDING_H_
00035 #define _FINRESDYN_ROUNDING_H_
00036 
00037 
00038 // BOOST (this header must be included first)
00039 #include "boost/numeric/interval.hpp"
00040 
00041 
00042 // --------------------------------------------------
00043 // -------------- rounding operations ---------------
00044 // --------------------------------------------------
00045 
00046 /// A class for rounding operations which uses the BOOST library.
00047 template <class NumType>
00048 class tRounding
00049 {
00050 public:
00051 /// This is an internal macro of the class template "tRounding"
00052 /// which defines arithmetic operations using the operations
00053 /// available in the BOOST library.
00054 #define DEFOP(WHAT) \
00055         static inline NumType WHAT (const NumType &x, const NumType &y) \
00056         { \
00057                 NumType result = Rounding. WHAT (x, y); \
00058                 Rounding. to_nearest (); \
00059                 return result; \
00060         }
00061 
00062         DEFOP(add_down)
00063         DEFOP(add_up)
00064         DEFOP(sub_down)
00065         DEFOP(sub_up)
00066         DEFOP(mul_down)
00067         DEFOP(mul_up)
00068         DEFOP(div_down)
00069         DEFOP(div_up)
00070 
00071 #undef DEFOP
00072 
00073 /*      NumType add_down (const NumType &x, const NumType &y) const
00074         {
00075                 NumType result = Rounding. add_down (x, y);
00076                 Rounding. to_nearest ();
00077                 return result;
00078         }
00079 */
00080 
00081         /// The smallest positive representable number.
00082         /// This function returns the smallest possible positive number,
00083         /// which typically equals the machine precision and may be
00084         /// implementation-dependent for different number types.
00085         static NumType min_number ();
00086 
00087 private:
00088         /// This is an object from the BOOST library which contains
00089         /// the definitions of arithmetic operations with correct rounding.
00090         static boost::numeric::interval_lib::rounded_arith_std<NumType>
00091                 Rounding;
00092 }; /* class tRounding */
00093 
00094 // --------------------------------------------------
00095 
00096 template <>
00097 inline float tRounding<float>::min_number ()
00098 {
00099         return __FLT_DENORM_MIN__;
00100 } /* min_number */
00101 
00102 template <>
00103 inline double tRounding<double>::min_number ()
00104 {
00105         return __DBL_DENORM_MIN__;
00106 } /* min_number */
00107 
00108 template <>
00109 inline long double tRounding<long double>::min_number ()
00110 {
00111         return __LDBL_DENORM_MIN__;
00112 } /* min_number */
00113 
00114 // --------------------------------------------------
00115 
00116 template <class NumType>
00117 boost::numeric::interval_lib::rounded_arith_std<NumType>
00118         tRounding<NumType>::Rounding;
00119 
00120 
00121 #endif // _FINRESDYN_ROUNDING_H_
00122 

Generated on Mon Apr 12 15:09:57 2010 for The Finite Resolution Dynamics Software by  doxygen 1.5.3