delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/01/06:45:20

Date: Thu, 1 May 1997 13:30:40 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: djgpp AT delorie DOT com
cc: David Jenkins <me AT jenkinsdavid DOT demon DOT co DOT uk>,
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Subject: Re: Floating point bug???
In-Reply-To: <Pine.SUN.3.91.970429191100.24554A-100000@is>
Message-ID: <Pine.SUN.3.91.970501132517.3617M-100000@is>
MIME-Version: 1.0

George Foot wrote:

> There is still
> perhaps the possibility that current could go from (target-SPEED/2-delta)
> to (target+SPEED/2+delta) where delta is a really small number, and then

This delta is called the ``machine epsilon''.  Specifically, all
comparisons between floats and doubles should be given relative
tolerance of that epsilon.  Therefore, where for ints you would say
just this:

   int i, j;
   int i_equals_j = (i == j);

for doubles you should say instead this:

   double a, b;
   double max_abs = max (fabs (a), fabs (b));
   int a_equals_b = (a - b < max_abs * eps && b - a < max_abs * eps);

Note that eps is the *relative* tolerance; hence the actual tolerance
is computed by multiplying it by the maximum absolute value of the two
comparees.

The header <float.h> defines a macro DBL_EPSILON which should be used
as the value of `eps' in the above snippet.  Two other macros,
FLT_EPSILON and LDBL_EPSILON give values for floats and long doubles,
respectfully.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019