Mail Archives: djgpp/1996/12/01/05:46:37
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
- Raw text -