This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Floating point woes
- To: Rafael Herrera <raffo at neuronet dot pitt dot edu>
- Subject: Re: Floating point woes
- From: Ingo Krabbe <ikrabbe at earthling dot net>
- Date: Tue, 5 Jun 2001 22:45:47 +0200 (CEST)
- cc: <gcc-help at gcc dot gnu dot org>
- Reply-To: Ingo Krabbe <i dot krabbe at dokom dot net>
On Sat, 2 Jun 2001, Rafael Herrera wrote:
> Hello,
>
> I've inherited a program wich has started to crash after attempting to
> compare floating point numbers (doubles to be precise). For example, a
> construct like this:
>
> if (a/2 > b)
> {
> <..>
> }
>
> would be true, even though the a/2 and b are equal.
>
> What is the recommended procedure to compare two doubles? If not
> recommended, is there a tool that can scan the sources and report the
> instances in which these potentially problematic constructs occur?
>
> Thank you
> --
> Rafael
>
Its common to compare equality of floats against an epsilon distance,
because of internal rounding troubles.
if ( abs(a/2-b) > epsilon )
with epsilon in a range which fits into precision and to our experiment:
epsilon = 1e-5 for example.
CU INGO