Mail Archives: djgpp/1996/12/02/21:35:35
George Foot wrote:
>
> On Sat, 30 Nov 1996 14:05:17 GMT, Francois Charton
> <deef AT pobox DOT oleane DOT com> wrote:
>
> >float f;
> >int i,j;
> >i=(int) (1.13*100.0);
> >f=1.13*100.0;
> >j=(int) f;
> >printf("%d %d\n",i,j);
> >
> >
> >prints 112 113...
> >
> >And this is not because you don't have a FPU : I got a 486DX.
> >I'd say this is a parsing problem, with the way DJGPP represents
> >internally floating point numbers :
>
> I don't think this is quite correct... It's not a parsing problem, and
> it's not DJGPP's (or gcc's) fault. I think it's more a problem with
> converting binary floating point into decimal, combined with a lack of
> understanding of the cast from float to int.
>
Not so fast. Before the answer, we have the real question: Not why is
the result 'wrong' but why are they different? Why does i=(int)(f*100);
yield a different result than f=f*100; i=(int)f; ? Why? Clearly the
compiler is doing conversions differently in the two cases.
Joe
- Raw text -