Date: Thu, 1 May 1997 13:30:40 +0300 (IDT) From: Eli Zaretskii To: djgpp AT delorie DOT com cc: David Jenkins , George Foot Subject: Re: Floating point bug??? In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk 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 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.