Mail Archives: djgpp/1999/05/30/05:28:15
On Sat, 29 May 1999, Christian Hofrichter wrote:
> I want to round an fp-number to the nearest representable integer value.
I suggest using `rint' from libm.a. IMHO, any hand-made solution will
get you in trouble some day.
> Another problem is the limited precision. Let's say some fp-operations
> should return 100.0 but return 99.9999998 due to precision problems. If
> I must compare this value (e.g in a loop) to define an end-condition I
> need a comparing methode which says that 99.9999998 is equal to 100.0
> and aborts the loop. Is this possible ?
Use DBL_EPSILON multiplied by the absolute value of the number, in
this case 100, as the tolerance. For example:
if (fabs (a - 100.0) < DBL_EPSILON * 100)
printf ("The numbers are equal within the accuracy of a double.\n");
- Raw text -