From: Joe Wright Newsgroups: comp.os.msdos.djgpp Subject: Re: Problems with DJGPP V2.01 - atof() function Date: Sun, 01 Dec 1996 03:25:10 -0500 Organization: Alpha Solutions Lines: 44 Message-ID: <32A140E6.7172@exis.net> References: <329e68a5 DOT 10316617 AT news DOT ua DOT pt> <57mtq1$4mo AT vidar DOT diku DOT dk> <32A02DD1 DOT 1157 AT pobox DOT oleane DOT com> Reply-To: wrightj AT exis DOT net NNTP-Posting-Host: ppp-2-57.exis.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Francois Charton wrote: > > Morten Welinder wrote: > > > > afonso AT inesca DOT inesca DOT pt writes: > > > > > char string[]="1.13"; > > > int result; > > > ... > > > result = (int)(atof(string)*100); > > > ... > > > > > I've got result = 112!!! not 113 as I wished, because > > >the function atof() return is 1.29999... not 1.13 (and I only have > > >an old i386). > > > > Getting 112 is well within the C standard. If your program does > > not work in this situation then you have a bug. > > > > Sorry to disagree but this *is* a bug : to be sure try the following > program : > > int main(void) > { > char ch[8]="1.13"; > int result, otherresult; > float f; > result=(int)(atof(ch)*100.0); > f=atof(ch)*100.0; > otherresult=(int) f; > printf("result: %d otherresult:%d\n", result, otherresult); > return 0; > } > > On my machine I get result: 112 and otherresult: 113... > > Francois Not me. I get 112 in all cases. The right way is probably result = (int)(ceil(atof(ch)*100)); which gives 113. Joe