integer division with float result

Stas Sergeev stsp at aknet.ru
Mon Nov 19 04:00:37 PST 2007


Lars Noschinski Wrote:
> >Of course, but there is no problem to make the / op to
> >just always return float, or is there?
> Efficiency and accuracy. An integer division is faster than a float
> division.
But isn't it just an optimization issue? The compiler is supposed
to know the result type beforehand, is it not? It then
can decide whether it needs the float calculations or not.
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.



More information about the Digitalmars-d mailing list