integer division with float result

Stas Sergeev stsp at aknet.ru
Mon Nov 19 21:58:59 PST 2007


0ffh Wrote:
> Hrmm, probably int division and two casts would suffice, which
> might be reduced by the optimiser to just the division.
Yes, that's exactly what I thought was a possibility.

> Still,
> this works only for for some cases
I thought in cases where it doesn't, you would need an explicit
cast anyway. eg:
int a = (int)(b / c) * 500;
You either _need_ that cast, or you need the float math. Because
the result will be different.
Well, this example is probably naive, but I yet have to dig
this thread more to find the counter-examples.
Well, the thread went to discussing the overloading of
the division operator, which is not my preference anyway.

> unless you have the compiler do type
> inference.
You know, all flavours of the optimization are taken for
granted these days. :) When I try to compile my programs
with the optimizer disabled, it usually became 3 times
bigger and basically stops working because it became
a CPU hog. :) (well, this is not about a PC progs, but about
the progs for MCUs, and this basically is not my fault but
gcc/avr-libc's one, but the fact is that the prog became
completely unusable without an optimization).



More information about the Digitalmars-d mailing list