This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: strange double compare problem
- From: Jakob Oestergaard <jakob at unthought dot net>
- To: Robin Ericsson <robin dot ericsson at noname4us dot com>
- Cc: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 21 Feb 2003 00:28:28 +0100
- Subject: Re: strange double compare problem
- References: <1045651654.472.40.camel@pulver.localhost.nu>
On Wed, Feb 19, 2003 at 11:47:34AM +0100, Robin Ericsson wrote:
> Can someone explain this problem.
>
> When this is compiled with g++ (tested with 2.96, 3.1 and 3.3) it
> produces "not equal" even though the values is the same.
>
> Changing test2 and test3 to float resolves the problem, but why doesn't
> it work with double and according to my man and headerfiles, atof should
> return a double.
Equality tests on floating point values does not make sense.
Any book on numerics will explain this in depth.
An equality test done right will look like:
if (fabs(a - b) < 1E-6) { ... }
--
................................................................
: jakob at unthought dot net : And I see the elder races, :
:.........................: putrid forms of man :
: Jakob Østergaard : See him rise and claim the earth, :
: OZ9ABN : his downfall is at hand. :
:.........................:............{Konkhra}...............: