integer division with float result

0ffh frank at frankhirsch.youknow.what.todo.net
Mon Nov 19 04:18:15 PST 2007


Stas Sergeev wrote:
> But isn't it just an optimization issue? The compiler is supposed
> to know the result type beforehand, is it not?

No, exactly that is the problem: The compiler knows the result type
only in some cases, as has been said twice now already.

> And in a more complex cases, the one can hint the compiler
> to not use float calculation by explicit casts to int, like this:
> int a = (int)(b / c) * 500;
> This expression can still be compiled without the fp math.
> And the cast is not redundant here as it truncates the value,
> and the programmer always knows when he needs to truncate
> the value in his expression. He is unlikely to forget such a cast.
> While currently the casts like this:
> float a = (b / (float)c) * 500;
> are used only to tell the compiler about the desired type, and
> the programmer can easily forget to put one.

That tradeoff can be made. Some people might be more happy then.
Some will be less happy. A lot of work will have been caused for
little gain.
Also: We have all understood your point. Some think the same as you,
some different. As it's a matter of tastes, you won't achieve much
more if you keep pressing it. This is not like wooing a woman, where
you get extra points for perseverance.

Regards, frank



More information about the Digitalmars-d mailing list