T
towers
Hello,
I've got a bit of experience in C++, but I'm writing my first app that
is dependent on relatively precise math functions. The app requires
that I get a time stamp based on s sample number, from a time series.
This seems liek an easy thing to do:
long lSample = 500; (for example)
double dSampleRate = 1000.0;
double dTimestamp = (double)lSample / dSampleRate;
The problem I am having is that the division by 1000 is not precise,
presumably because 1/(10^n) cannot be represented as a standard
floating point number. So, with each subsequent sample, the calculated
time stamp is further from the actual time of the sample.
The same problem occurs if I simply add the sample period to the
previous timestamp, and start at dTimestamp = 0.0. It also occurs when
trying to get the sample period, e.g.
double dSamplePeriod = 1.0 / dSampleRate
then dSamplePeriod = 0.00100000004749745
Currently, I am using a class off the web, called CLargeDouble, to get
around this, but this class uses string conversion, and as such, slows
the program immensely.
This app is being compiled using MS Visual C++ 7, and run on XP
machines. However, when I ran it on our terminal server (Win 2000
server), the 1/1000 operation did _not_ include the garbage starting at
the 6th or 7th siginificant digit.
My question is, is there a standard, fast workaround to this problem?
Or does it seem that this involves some problem in my code, which only
occurs when running on an XP platform? My hope is to avoid truncating
at some arbitrary number of significant digits, since I will be using
different sample rates (e.g. 1024hz, the inverse of which can be
respesented in floating point).
Thanks in advance for any help.
-D.T.
I've got a bit of experience in C++, but I'm writing my first app that
is dependent on relatively precise math functions. The app requires
that I get a time stamp based on s sample number, from a time series.
This seems liek an easy thing to do:
long lSample = 500; (for example)
double dSampleRate = 1000.0;
double dTimestamp = (double)lSample / dSampleRate;
The problem I am having is that the division by 1000 is not precise,
presumably because 1/(10^n) cannot be represented as a standard
floating point number. So, with each subsequent sample, the calculated
time stamp is further from the actual time of the sample.
The same problem occurs if I simply add the sample period to the
previous timestamp, and start at dTimestamp = 0.0. It also occurs when
trying to get the sample period, e.g.
double dSamplePeriod = 1.0 / dSampleRate
then dSamplePeriod = 0.00100000004749745
Currently, I am using a class off the web, called CLargeDouble, to get
around this, but this class uses string conversion, and as such, slows
the program immensely.
This app is being compiled using MS Visual C++ 7, and run on XP
machines. However, when I ran it on our terminal server (Win 2000
server), the 1/1000 operation did _not_ include the garbage starting at
the 6th or 7th siginificant digit.
My question is, is there a standard, fast workaround to this problem?
Or does it seem that this involves some problem in my code, which only
occurs when running on an XP platform? My hope is to avoid truncating
at some arbitrary number of significant digits, since I will be using
different sample rates (e.g. 1024hz, the inverse of which can be
respesented in floating point).
Thanks in advance for any help.
-D.T.