FedTimeD.hh

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 // CERTI - HLA RunTime Infrastructure
00003 // Copyright (C) 2002-2005  ONERA
00004 //
00005 // This file is part of CERTI-libCERTI
00006 //
00007 // CERTI-libCERTI is free software ; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License
00009 // as published by the Free Software Foundation ; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // CERTI-libCERTI is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY ; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015 // Lesser General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public
00018 // License along with this program ; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00020 // USA
00021 //
00022 // $Id: FedTimeD.hh,v 4.1 2009/04/02 19:58:09 erk Exp $
00023 // ----------------------------------------------------------------------------
00024 
00025 #ifndef CERTI_FEDTIMED_HH
00026 #define CERTI_FEDTIMED_HH
00027 
00028 #include "certi.hh"
00029 
00030 #include <math.h>
00031 #include <ostream>
00032 
00033 namespace certi {
00034 
00035 /*
00036  fcmp
00037  Copyright (c) 1998-2000 Theodore C. Belding
00038  University of Michigan Center for the Study of Complex Systems
00039  <mailto:Ted.Belding@umich.edu>
00040  <http://fcmp.sourceforge.net>
00041 
00042  This file is part of the fcmp distribution. fcmp is free software;
00043  you can redistribute and modify it under the terms of the GNU Library
00044  General Public License (LGPL), version 2 or later.  This software
00045  comes with absolutely no warranty. See the file COPYING for details
00046  and terms of copying.
00047 
00048  Description:
00049 
00050  Knuth's floating point comparison operators, from:
00051  Knuth, D. E. (1998). The Art of Computer Programming.
00052  Volume 2: Seminumerical Algorithms. 3rd ed. Addison-Wesley.
00053  Section 4.2.2, p. 233. ISBN 0-201-89684-2.
00054 
00055  Input parameters:
00056  x1, x2: numbers to be compared
00057  epsilon: determines tolerance
00058 
00059  epsilon should be carefully chosen based on the machine's precision,
00060  the observed magnitude of error, the desired precision, and the
00061  magnitude of the numbers to be compared. See the fcmp README file for
00062  more information.
00063 
00064  This routine may be used for both single-precision (float) and
00065  double-precision (double) floating-point numbers.
00066 
00067  Returns:
00068  -1 if x1 < x2
00069   0 if x1 == x2
00070   1 if x1 > x2
00071 */
00072 
00073 inline int
00074 fcmp(double x1,double x2, double epsilon)
00075 {
00076     int exponent;
00077     double delta;
00078     double difference;
00079 
00080     /* Get exponent(max(fabs(x1), fabs(x2))) and store it in exponent. */
00081 
00082     /* If neither x1 nor x2 is 0, */
00083     /* this is equivalent to max(exponent(x1), exponent(x2)). */
00084 
00085     /* If either x1 or x2 is 0, its exponent returned by frexp would be 0, */
00086     /* which is much larger than the exponents of numbers close to 0 in */
00087     /* magnitude. But the exponent of 0 should be less than any number */
00088     /* whose magnitude is greater than 0. */
00089 
00090     /* So we only want to set exponent to 0 if both x1 and */
00091     /* x2 are 0. Hence, the following works for all x1 and x2. */
00092 
00093     frexp(fabs(x1) > fabs(x2) ? x1 : x2, &exponent);
00094 
00095     /* Do the comparison. */
00096 
00097     /* delta = epsilon * pow(2, exponent) */
00098 
00099     /* Form a neighborhood around x2 of size delta in either direction. */
00100     /* If x1 is within this delta neighborhood of x2, x1 == x2. */
00101     /* Otherwise x1 > x2 or x1 < x2, depending on which side of */
00102     /* the neighborhood x1 is on. */
00103 
00104     delta = ldexp(epsilon, exponent);
00105 
00106     difference = x1 - x2;
00107 
00108     if (difference > delta)
00109         return 1; /* x1 > x2 */
00110     else if (difference < -delta)
00111         return -1;  /* x1 < x2 */
00112     else /* -delta <= difference <= delta */
00113         return 0;  /* x1 == x2 */
00114 }
00115 
00116 class CERTI_EXPORT FedTime
00117 {
00118 public:
00119     static const double epsilon;
00120     static int fcmp(const double x1, const double x2);
00121 
00122     FedTime();
00123     FedTime(const double &);
00124     FedTime(const FedTime &);
00125     ~FedTime();
00126 
00127 public:
00128     void setZero();
00129     bool isZero();
00130     void setEpsilon();
00131     void setPositiveInfinity();
00132     bool isPositiveInfinity() const;
00133     int encodedLength() const;
00134     void encode(char *) const;
00135     int getPrintableLength() const;
00136     void getPrintableString(char *);
00137     double getTime() const;
00138 
00139     FedTime& operator+=(const FedTime &);
00140     FedTime& operator-=(const FedTime &);
00141     bool operator<=(const FedTime &) const;
00142     bool operator<(const FedTime &) const;
00143     bool operator>=(const FedTime &) const;
00144     bool operator>(const FedTime &) const;
00145     bool operator==(const FedTime &) const;
00146     bool operator==(const double &) const;
00147     bool operator!=(const FedTime &) const;
00148     bool operator!=(const double &) const;
00149     FedTime &operator=(const FedTime &);
00150     FedTime &operator=(const double &);
00151     FedTime &operator*=(const FedTime &);
00152     FedTime &operator/=(const FedTime &);
00153     FedTime &operator+=(const double &);
00154     FedTime &operator-=(const double &);
00155     FedTime &operator*=(const double &);
00156     FedTime &operator/=(const double &);
00157     FedTime operator+(const FedTime &);
00158     FedTime operator+(const double &);
00159     FedTime operator-(const FedTime &);
00160     FedTime operator-(const double &);
00161     FedTime operator*(const FedTime &);
00162     FedTime operator*(const double &);
00163     FedTime operator/(const FedTime &);
00164     FedTime operator/(const double &);
00165 
00166     friend std::ostream CERTI_EXPORT &operator<<(std::ostream&, const FedTime &time);
00167 
00168 private:
00169     double _fedTime ;
00170     double _zero ;
00171     double _epsilon ;
00172     double _positiveInfinity ;
00173 };
00174 
00175 FedTime operator+(const double &, const FedTime &);
00176 FedTime operator-(const double &, const FedTime &);
00177 FedTime operator*(const double &, const FedTime &);
00178 FedTime operator/(const double &, const FedTime &);
00179 
00180 typedef FedTime FederationTime;
00181 typedef FedTime FederationTimeDelta;
00182 
00183 } // namespace certi
00184 
00185 #endif // CERTI_FEDTIMED_HH
00186 
00187 // $Id: FedTimeD.hh,v 4.1 2009/04/02 19:58:09 erk Exp $

Generated on Thu Apr 30 15:53:49 2009 for CERTIDeveloperDocumentation by doxygen 1.5.5