This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: strange double compare problem


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}...............:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]