| delorie.com/archives/browse.cgi | search | 
| Message-ID: | <3ABA005C.4B1E1B74@earthlink.net> | 
| From: | Martin Ambuhl <mambuhl AT earthlink DOT net> | 
| X-Mailer: | Mozilla 4.76 [en] (Win95; U) | 
| X-Accept-Language: | en,zh-CN,fr,de-CH,ru | 
| MIME-Version: | 1.0 | 
| Newsgroups: | comp.os.msdos.djgpp | 
| Subject: | Re: Strange behavior in loops!! | 
| References: | <99ctq8$t29$1 AT saphir DOT jouy DOT inra DOT fr> | 
| Lines: | 25 | 
| Date: | Thu, 22 Mar 2001 13:35:28 GMT | 
| NNTP-Posting-Host: | 64.152.162.179 | 
| X-Complaints-To: | abuse AT earthlink DOT net | 
| X-Trace: | newsread2.prod.itd.earthlink.net 985268128 64.152.162.179 (Thu, 22 Mar 2001 05:35:28 PST) | 
| NNTP-Posting-Date: | Thu, 22 Mar 2001 05:35:28 PST | 
| Organization: | EarthLink Inc. -- http://www.EarthLink.net | 
| To: | djgpp AT delorie DOT com | 
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp | 
| Reply-To: | djgpp AT delorie DOT com | 
Eric Wajnberg wrote:
>
> 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?
No.  5/100 = 1/20 is not a binary fraction.  It cannot be expressed exactly in
a binary mantissa.
| webmaster | delorie software privacy | 
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |