delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/03/23/00:52:42

Date: Fri, 23 Mar 2001 11:03:24 +0530 (IST)
From: Mridul Muralidharan <ec97027 AT matrix DOT reccal DOT ernet DOT in>
To: wajnberg AT antibes DOT inra DOT fr
cc: Djgpp mailing list <djgpp AT delorie DOT com>
Subject: Re: Strange behavior in loops!!
In-Reply-To: <99ctq8$t29$1@saphir.jouy.inra.fr>
Message-ID: <Pine.LNX.4.04.10103231056150.29100-100000@matrix.reccal.ernet.in>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

hi,
	This has something got to do with the way floating point numbers
are represented . some fractional number's cannot be expressed in binary
format in finite number of bits ( 1/3 in decimal for example ) and so the
value just gets truncated/rounded. this leads to inconsistencies that u
observed . i think printf/scanf rounds off a number to nearest complete
fraction.
	This is mentioned somewhere in the documentation though cannot
offhand recollect where.

hope this helped u,

Mridul Muralidharan
S8 Electronics and Communication
Regional Engineering College
Calicut
India

------------------------------------------------------------------------------

If the code and the comments disagree, then both are probably wrong.
                -- Norm Schryer

All generalizations are false, including this one.
                -- Mark Twain

/earth is 98% full ... please delete anyone you can.

	- fortune

On 22 Mar 2001, Eric Wajnberg wrote:

> 
> I just got the strange following behavior with DJGPP that looks
> like an error to me.
> 
> The following simple code:
> 
> #include <stdio.h>
> int main(void)
> {
>  float from=3,to=5,by=.5;
>  float x;
>  for (x=from;x<=to;x+=by)
>    (void)printf("%g\n",x);
>  return 0;
> }
> 
> produces of course the values 3, 3.5, 4, 4.5 and 5.
> 
> Up to now things are ok.
> 
> Now look at the following other code:
> 
> #include <stdio.h>
> int main(void)
> {
>  float from=.3,to=.5,by=.05;
>  float x;
>  for (x=from;x<=to;x+=by)
>   (void)printf("%g\n",x);
>  return 0;
> }
> 
> Which is exactly the same than the previous one except that all values
> have been divided by 10.
> 
> In that case, the values outputted are .3, .35, .4, and .45.
> 
> In other words the value ".5" (i.e., the last one) is not outputted.
> 
> Strange, isn'it?
> 
> Of course the real problem I am facing now is much more complicated than
> this one (the length of my code is currently of several thousand lines), but
> these small examples explain it clearly.
> 
> I've made several other tests, but I'm keeping on obtaining the same problem.
> 
> Did I missed something?
> 
> Thanks for any help on that.
> 
> 
>     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   
>     Eric Wajnberg
>     Directeur adjoint du GDR CNRS 2155 Ecologie Comportementale
>     I.N.R.A.
>     37, Bld. du Cap. 06600 Antibes. France.
>     Tel : (33-0) 4.93.67.88.92
>     Fax : (33-0) 4.93.67.88.97
>     e-mail : wajnberg AT antibes DOT inra DOT fr
> 
>     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
>   
> 
> 
> 
> 

- Raw text -


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