From: plipson AT my-dejanews DOT com Newsgroups: comp.os.msdos.djgpp Subject: Use of FLT_EPSILON Date: Mon, 13 Jul 1998 20:27:46 GMT Organization: Deja News - The Leader in Internet Discussion Lines: 34 Message-ID: <6odqk1$p23$1@nnrp1.dejanews.com> References: <3587a411 DOT 12016463 AT news DOT polimi DOT it> NNTP-Posting-Host: 207.77.64.13 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk In article , Eli Zaretskii 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 .) > > 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