delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/30/05:28:15

Date: Sun, 30 May 1999 12:23:01 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Christian Hofrichter <ChristianHofrichter AT gmx DOT de>
cc: djgpp AT delorie DOT com
Subject: Re: how to round fp-numbers correctly ?
In-Reply-To: <37504A52.DA4C5283@gmx.de>
Message-ID: <Pine.SUN.3.91.990530122237.12571Z-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019