library of assembled shared sources

http://lass.cocamware.com

floating_point_consistency.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_FLOATING_POINT_CONSISTENCY_H
00045 #define LASS_GUARDIAN_OF_INCLUSION_NUM_FLOATING_POINT_CONSISTENCY_H
00046 
00047 #include "num_common.h"
00048 #include "../util/call_traits.h"
00049 #include <complex>
00050 
00051 namespace lass
00052 {
00053 namespace num
00054 {
00055 
00056 template <typename T>
00057 class Consistent
00058 {
00059 public:
00060     typedef Consistent<T> TSelf;
00061     typedef typename util::CallTraits<T>::TValue TValue;
00062     typedef typename util::CallTraits<T>::TParam TParam;
00063     typedef typename util::CallTraits<T>::TReference TReference;
00064     typedef typename util::CallTraits<T>::TConstReference TConstReference;
00065     typedef num::NumTraits<T> TNumTraits;
00066 
00067     Consistent() : t_() 
00068     {
00069     }
00070 
00071     Consistent(TParam t): 
00072         t_(t) 
00073     {
00074     }
00075 
00076     Consistent(const TSelf& other):
00077         t_(other.t_)
00078     {
00079     }
00080 
00081     TParam value() const 
00082     { 
00083         return t_; 
00084     }
00085 
00086     TSelf operator-() const
00087     {
00088         return TSelf(-t_);
00089     }
00090 
00091     TSelf operator+() const
00092     {
00093         return *this;
00094     }
00095 
00096     TSelf& operator+=(const TSelf& other) 
00097     { 
00098         t_ += other.t_; 
00099         return *this; 
00100     }
00101     TSelf& operator-=(const TSelf& other) 
00102     { 
00103         t_ -= other.t_; 
00104         return *this; 
00105     }
00106     TSelf& operator*=(const TSelf& other) 
00107     { 
00108         t_ *= other.t_; 
00109         return *this; 
00110     }
00111     TSelf& operator/=(const TSelf& other)
00112     { 
00113         t_ /= other.t_; 
00114         return *this; 
00115     }
00116     
00117     void swap(TSelf& other)
00118     {
00119         std::swap(t_, other.t_);
00120     }
00121 
00122 private:
00123     volatile TValue t_;
00124 };
00125 
00126 
00127 
00128 template <typename T> inline
00129 Consistent<T> operator+(const Consistent<T>& a, const Consistent<T>& b)  
00130 { 
00131     Consistent<T> result(a); 
00132     result += b; 
00133     return result; 
00134 }
00135 
00136 template <typename T> inline
00137 Consistent<T> operator-(const Consistent<T>& a, const Consistent<T>& b) 
00138 {
00139     Consistent<T> result(a);
00140     result -= b;
00141     return result;
00142 }
00143 
00144 template <typename T> inline
00145 Consistent<T> operator*(const Consistent<T>& a, const Consistent<T>& b) 
00146 {
00147     Consistent<T> result(a);
00148     result *= b;
00149     return result;
00150 }
00151 
00152 template <typename T> inline
00153 Consistent<T> operator/(const Consistent<T>& a, const Consistent<T>& b) 
00154 {
00155     Consistent<T> result(a);
00156     result /= b;
00157     return result;
00158 }
00159 
00160 template <typename T> inline
00161 bool operator==(const Consistent<T>& a, const Consistent<T>& b) 
00162 {
00163     return a.value() == b.value();
00164 }
00165 
00166 template <typename T> inline
00167 bool operator!=(const Consistent<T>& a, const Consistent<T>& b) 
00168 {
00169     return !(a == b);
00170 }
00171 
00172 template <typename T> inline
00173 bool operator<(const Consistent<T>& a, const Consistent<T>& b) 
00174 {
00175     return a.value() < b.value();
00176 }
00177 
00178 template <typename T> inline
00179 bool operator>(const Consistent<T>& a, const Consistent<T>& b) 
00180 {
00181     return b < a;
00182 }
00183 
00184 template <typename T> inline
00185 bool operator<=(const Consistent<T>& a, const Consistent<T>& b) 
00186 {
00187     return !(b < a);
00188 }
00189 
00190 template <typename T> inline
00191 bool operator>=(const Consistent<T>& a, const Consistent<T>& b) 
00192 {
00193     return !(a < b);
00194 }
00195 
00196 
00197 
00198 
00199 template <typename T> inline
00200 Consistent<T> operator+(const Consistent<T>& a, const T& b)  
00201 { 
00202     Consistent<T> result(a); 
00203     result += b; 
00204     return result; 
00205 }
00206 
00207 template <typename T> inline
00208 Consistent<T> operator-(const Consistent<T>& a, const T& b) 
00209 {
00210     Consistent<T> result(a);
00211     result -= b;
00212     return result;
00213 }
00214 
00215 template <typename T> inline
00216 Consistent<T> operator*(const Consistent<T>& a, const T& b) 
00217 {
00218     Consistent<T> result(a);
00219     result *= b;
00220     return result;
00221 }
00222 
00223 template <typename T> inline
00224 Consistent<T> operator/(const Consistent<T>& a, const T& b) 
00225 {
00226     Consistent<T> result(a);
00227     result /= b;
00228     return result;
00229 }
00230 
00231 template <typename T> inline
00232 bool operator==(const Consistent<T>& a, const T& b) 
00233 {
00234     return a == Consistent<T>(b);
00235 }
00236 
00237 template <typename T> inline
00238 bool operator!=(const Consistent<T>& a, const T& b) 
00239 {
00240     return !(a == b);
00241 }
00242 
00243 template <typename T> inline
00244 bool operator<(const Consistent<T>& a, const T& b) 
00245 {
00246     return a < Consistent<T>(b);
00247 }
00248 
00249 template <typename T> inline
00250 bool operator>(const Consistent<T>& a, const T& b) 
00251 {
00252     return b < a;
00253 }
00254 
00255 template <typename T> inline
00256 bool operator<=(const Consistent<T>& a, const T& b) 
00257 {
00258     return !(b < a);
00259 }
00260 
00261 template <typename T> inline
00262 bool operator>=(const Consistent<T>& a, const T& b) 
00263 {
00264     return !(a < b);
00265 }
00266 
00267 
00268 
00269 
00270 template <typename T> inline
00271 Consistent<T> operator+(const T& a, const Consistent<T>& b)  
00272 { 
00273     Consistent<T> result(a); 
00274     result += b; 
00275     return result; 
00276 }
00277 
00278 template <typename T> inline
00279 Consistent<T> operator-(const T& a, const Consistent<T>& b) 
00280 {
00281     Consistent<T> result(a);
00282     result -= b;
00283     return result;
00284 }
00285 
00286 template <typename T> inline
00287 Consistent<T> operator*(const T& a, const Consistent<T>& b) 
00288 {
00289     Consistent<T> result(a);
00290     result *= b;
00291     return result;
00292 }
00293 
00294 template <typename T> inline
00295 Consistent<T> operator/(const T& a, const Consistent<T>& b) 
00296 {
00297     Consistent<T> result(a);
00298     result /= b;
00299     return result;
00300 }
00301 
00302 template <typename T> inline
00303 bool operator==(const T& a, const Consistent<T>& b) 
00304 {
00305     return Consistent<T>(a) == b;
00306 }
00307 
00308 template <typename T> inline
00309 bool operator!=(const T& a, const Consistent<T>& b) 
00310 {
00311     return !(a == b);
00312 }
00313 
00314 template <typename T> inline
00315 bool operator<(const T& a, const Consistent<T>& b) 
00316 {
00317     return Consistent<T>(a) < b;
00318 }
00319 
00320 template <typename T> inline
00321 bool operator>(const T& a, const Consistent<T>& b) 
00322 {
00323     return b < a;
00324 }
00325 
00326 template <typename T> inline
00327 bool operator<=(const T& a, const Consistent<T>& b) 
00328 {
00329     return !(b < a);
00330 }
00331 
00332 template <typename T> inline
00333 bool operator>=(const T& a, const Consistent<T>& b) 
00334 {
00335     return !(a < b);
00336 }
00337 
00338 
00339 
00340 
00341 
00342 template <typename T> inline
00343 Consistent<T> abs(const Consistent<T>& v)
00344 {
00345     return num::abs(v.value());
00346 }
00347 
00348 template <typename T> inline
00349 Consistent<T> inv(const Consistent<T>& v)
00350 {
00351     return num::inv(v.value());
00352 }
00353 /*
00354 template <typename T> inline
00355 Consistent<T> sqrt(const Consistent<T>& v)
00356 {
00357     return num::sqrt(v.value());
00358 }
00359 */
00360 template <typename T> inline
00361 Consistent<T> sqrt(Consistent<T> v)
00362 {
00363     return num::sqrt(v.value());
00364 }
00365 
00366 template <typename T> inline
00367 Consistent<T> pow(const Consistent<T>& v, const Consistent<T>& p)
00368 {
00369     return num::pow(v.value(), p.value());
00370 }
00371 
00372 template <typename T> inline
00373 Consistent<T> exp(const Consistent<T>& v)
00374 {
00375     return num::exp(v.value());
00376 }
00377 
00378 template <typename T> inline
00379 Consistent<T> log(const Consistent<T>& v)
00380 {
00381     return num::log(v.value());
00382 }
00383 
00384 template <typename T> inline
00385 Consistent<T> cos(const Consistent<T>& v)
00386 {
00387     return num::cos(v.value());
00388 }
00389 
00390 template <typename T> inline
00391 Consistent<T> sin(const Consistent<T>& v)
00392 {
00393     return num::sin(v.value());
00394 }
00395 
00396 template <typename T> inline
00397 Consistent<T> tan(const Consistent<T>& v)
00398 {
00399     return num::tan(v.value());
00400 }
00401 
00402 template <typename T> inline
00403 Consistent<T> acos(const Consistent<T>& v)
00404 {
00405     return num::acos(v.value());
00406 }
00407 
00408 template <typename T> inline
00409 Consistent<T> asin(const Consistent<T>& v)
00410 {
00411     return num::asin(v.value());
00412 }
00413 
00414 template <typename T> inline
00415 Consistent<T> atan(const Consistent<T>& v)
00416 {
00417     return num::atan(v.value());
00418 }
00419 
00420 template <typename T> inline
00421 Consistent<T> atan2(const Consistent<T>& y, const Consistent<T>& x)
00422 {
00423     return num::atan2(y.value(), x.value());
00424 }
00425 
00426 template <typename T> inline
00427 Consistent<T> floor(const Consistent<T>& v)
00428 {
00429     return num::floor(v.value());
00430 }
00431 
00432 template <typename T> inline
00433 Consistent<T> ceil(const Consistent<T>& v)
00434 {
00435     return num::ceil(v.value());
00436 }
00437 
00438 template <typename T> inline
00439 Consistent<T> round(const Consistent<T>& v)
00440 {
00441     return num::round(v.value());
00442 }
00443 
00444 template <typename T> inline
00445 Consistent<T> fractional(const Consistent<T>& v)
00446 {
00447     return num::fractional(v.value());
00448 }
00449 
00450 template <typename T> inline
00451 Consistent<T> div(const Consistent<T>& v, const Consistent<T>& m)
00452 {
00453     return num::div(v.value(), m.value());
00454 }
00455 
00456 template <typename T> inline
00457 Consistent<T> mod(const Consistent<T>& v, const Consistent<T>& m)
00458 {
00459     return num::mod(v.value(), m.value());
00460 }
00461 
00462 
00463 #define LASS_NUM_FLOATING_POINT_CONSISTENCY_DECLARE_NUMTRAITS(type)\
00464     template <>\
00465     struct LASS_DLL NumTraits< Consistent< type > >\
00466     {\
00467     private:\
00468         typedef NumTraits< type > TBaseTraits;\
00469     public:\
00470         typedef Consistent< type > selfType;\
00471         typedef type baseType;\
00472         typedef type intervalType;\
00473         enum\
00474         {\
00475             isDistribution = TBaseTraits::isDistribution,\
00476             isIntegral = TBaseTraits::isIntegral,\
00477             isNative = TBaseTraits::isNative,\
00478             isSigned = TBaseTraits::isSigned,\
00479             hasInfinity = TBaseTraits::hasInfinity,\
00480             hasNaN = TBaseTraits::hasNaN\
00481         };\
00482         static const int memorySize;\
00483         static const std::string name() { return "Consistent<" + TBaseTraits::name() + ">" ; }\
00484         static const selfType one;\
00485         static const selfType zero;\
00486         static const selfType sNaN;\
00487         static const selfType qNaN;\
00488         static const selfType epsilon;\
00489         static const selfType infinity;\
00490         static const selfType min;\
00491         static const selfType max;\
00492         static const selfType minStrictPositive;\
00493         static const selfType pi;\
00494         static const selfType e;\
00495         static const selfType sqrt2;\
00496         static const selfType sqrtPi;\
00497     };
00498 
00499 LASS_NUM_FLOATING_POINT_CONSISTENCY_DECLARE_NUMTRAITS(float)
00500 LASS_NUM_FLOATING_POINT_CONSISTENCY_DECLARE_NUMTRAITS(double)
00501 LASS_NUM_FLOATING_POINT_CONSISTENCY_DECLARE_NUMTRAITS(long double)
00502 
00503 template<class T> inline
00504 bool isNaN( const Consistent<T>& v )
00505 {
00506     return num::isNaN(v.value());
00507 }
00508 
00509 template <typename T, typename Char, typename Traits>
00510 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& s, const Consistent<T>& v)
00511 {
00512     s << v.value();
00513     return s;
00514 }
00515 
00516 template <typename T, typename Char, typename Traits>
00517 std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& s, Consistent<T>& v)
00518 {
00519     T temp;
00520     s >> temp;
00521     v = Consistent<T>(temp);
00522     return s;
00523 }
00524 
00525 }
00526 
00527 }
00528 
00529 namespace std
00530 {
00531 
00532 template <typename T>
00533 void swap(::lass::num::Consistent<T>& a, ::lass::num::Consistent<T>& b)
00534 {
00535     a.swap(b);
00536 }
00537 
00538 }
00539 
00540 #endif

Generated on Mon Nov 10 14:20:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo