delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/17/10:39:33

Sender: hecht AT DH_NRZ24 DOT dillinger DOT de
Message-ID: <3587C720.794B@dillinger.de>
Date: Wed, 17 Jun 1998 16:39:44 +0300
From: michael hecht <michael DOT hecht AT dillinger DOT de>
MIME-Version: 1.0
To: sassi AT biomed DOT polimi DOT it
CC: djgpp AT delorie DOT com
Subject: Re: A question about atof() and the double 0.1

after modifying Your program as follows, You get the 'right' output, so
be aware of using direct comparisions between any floting types !!!


#include <stdio.h>

int main(void) {

 char rr[10];
 long double r=0.0;
 long double inc=(long double)0.05;

 rr[0]='0';
 rr[1]='.';
 rr[2]='1';
 rr[3]='\0';

 r= (long double) strtold(rr,NULL);
 if(r==((long double)0.1)) printf("OKAY\n");
 else printf("ERROR\n");
 printf("%40.38lf\n",(double)r);

 r+=inc;
 if(r<=(long double)0.15) printf("OKAY\n");
 else printf("ERROR\n");
 if(r==(long double)0.15) printf("OKAY\n");
 else printf("ERROR\n");

 rr[0]='0';
 rr[1]='.';
 rr[2]='1'; /* only a test */
 rr[3]='5';
 rr[4]='\0';

 r= (long double) strtold(rr,NULL);
 if(r==((long double)0.15)) printf("OKAY\n");
 else printf("ERROR\n");
 printf("%40.38lf\n",(double)r);
}

ERROR
0.10000000000000001000000000000000000000
ERROR
ERROR
ERROR
0.14999999999999999000000000000000000000
~

- Raw text -


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