Mail Archives: djgpp/1998/07/13/17:45:27
In article <Pine DOT SUN DOT 3 DOT 91 DOT 980617170255 DOT 20468H-100000 AT is>,
Eli Zaretskii <eliz AT is DOT elta DOT co DOT il> wrote:
>
> the following Golden Rule Of Comparing
> Floating-Point Numbers:
>
> To check whether two floating-point numbers a and b are equal, use the
> following paradigm:
>
> if (fabs (a - b) > min (fabs(a), fabs(b))*DBL_EPSILON)
> printf ("Not equal\n");
> else
> printf ("Equal\n");
>
> (Use FLT_EPSILON for floats, LDBL_EPSILON for long doubles; all of them
> are defined on <float.h>.)
>
> In other words, don't EVER compare FP numbers for exact equality, since
> floating-point computations have inherent inaccuracy, unlike integer
> numbers.
This looks pretty ugly (the multiply and even more the min() ) - isn't
there a better way? And what about comparing an expression to zero?
I tried several; since FLT_EPSILON is defined in terms of 1 -
if( fabs(a-b) < FLT_EPSILON....
if( (1 + fabs(a-b)) < (1+FLT_EPSILON).....
and a few others.
Does anyone have an explanation of what & why to do?
Peter
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
- Raw text -