| [ CnUnix ] in KIDS 글 쓴 이(By): diploid ( 2n) 날 짜 (Date): 2003년 5월 28일 수요일 오전 06시 54분 18초 제 목(Title): Re: [Q] C/C++의 if문에서.. 한때 이 보드에서 회자되었던 C-faq에도 언급이 되어있군요. http://www.eskimo.com/~scs/C-faq/q14.5.html 링크못하시는분들을 위해서. ------ Question 14.5 What's a good way to check for ``close enough'' floating-point equality? --------------------------------------------------------------------------- Since the absolute accuracy of floating point values varies, by definition, with their magnitude, the best way of comparing two floating point values is to use an accuracy threshold which is relative to the magnitude of the numbers being compared. Rather than double a, b; ... if(a == b) /* WRONG */ use something like #include <math.h> if(fabs(a - b) <= epsilon * fabs(a)) for some suitably-chosen epsilon. References: Knuth Sec. 4.2.2 pp. 217-8 |