Date: Sun, 30 May 1999 12:23:01 +0300 (IDT) From: Eli Zaretskii X-Sender: eliz AT is To: Christian Hofrichter cc: djgpp AT delorie DOT com Subject: Re: how to round fp-numbers correctly ? In-Reply-To: <37504A52.DA4C5283@gmx.de> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Reply-To: djgpp AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk 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");