www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/18/02:46:11

Date: Thu, 18 Jun 1998 09:45:44 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Roberto Sassi <sassi AT biomed DOT polimi DOT it>
cc: djgpp AT delorie DOT com
Subject: Re: Another silly question about the double 0.1 ...
In-Reply-To: <3587fb67.19298603@news.polimi.it>
Message-ID: <Pine.SUN.3.91.980618094518.23552E-100000@is>
MIME-Version: 1.0

On Wed, 17 Jun 1998, Roberto Sassi wrote:

>  double r=0.1;
>  double inc=0.05;
> 
>  for(;r<=0.25;r+=inc) 
>     printf("%6.5f\n",r);
> 

You should reiterate Lesson no.1: this is the same case.  The loop
tests r and 0.25 for EQUALITY.  In a previous message I describe the
way to do that: you need to allow for a slack of (in this case)
0.25*DBL_EPSILON, like this:

     for ( ; r <= 0.25 * (1 + DBL_EPSILON); r += inc)

> The floating point optimization error, I think, makes the loop one
> step shorter.

No, optimizations rearrange the floating-point operations and use FP
registers of the CPU, so the results are slightly different.  Your
program uses code whose behavior is undefined, so you get different
results for different compilers and optimization levels.

- Raw text -


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