swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Q] on the precision of double variables


From: Ken Cline
Subject: Re: [Q] on the precision of double variables
Date: Wed, 7 Jul 1999 08:24:26 -0400 (EDT)

On Wed, 7 Jul 1999, William S. Shu wrote:

> Norberto Eiji Nawa wrote:
> 
> > Hello:
> >
> > Actually, my question this time is not directly related
> > to swarm, but the response from the swarm community is
> > always so friendly that I couldn't resist the temptation
> > to post it here.
> >
> > I have a very trivial loop that looks more or less like
> > this:
> >         while (currentPrice != [tempBidOffer getPrice]) {
> >
> 
> The internal representation of real numbers (floats,
> doubles, etc.)  in machines is not exact, and so you do
> not test for their equality.  Instead, you test if their
> difference is within a given tolerance.  e.g.,
>    while (abs(currentPrice - [tempBidOffer getPrice]) > EPS) {.
>                 ....
> 
> Where EPS is the tolerance (e.g., #define  EPS 0.01).
> abs(x) obtains the absolute value, just in case the
> differnce is negative.

Just to add a tad...

The standard C library, limits.h, defines values such
as DBL_EPSILON which is the "smallest number x such that
1.0 + x != 1.0".*

I use this value with macros relating to distance
measurement, e.g.
   typedef double  dist_t;
   #define DIST_EPSILON    DBL_EPSILON
   #define equalDist( a, b )  \
      ( (b) -  DIST_EPSILON < (a) && (a) < (b) + DIST_EPSILON )

Perhaps, the abs() function (actually fabs()) would a better
choice, though.

* Footnote: The quote is from K&R.  My 4th edition Harbison
& Steele has a similar, but somewhat puzzling, statement
(see page 118):
 "DBL_EPSILON ... the minimum x > 0.0 such that 1.0 + x != x"


_________________________________________________________
Ken Cline                             address@hidden
SAIC                                 VOICE (410) 571-0413
Annapolis, MD                          FAX (301) 261-8427




                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]