Detecting inadvertent use of integer division

Saaa empty at needmail.com
Mon Dec 14 07:59:11 PST 2009


Don wrote
> Consider this notorious piece of code:
>
> assert(x>1);
> double y = 1 / x;
>
> This calculates y as the reciprocal of x, if x is a floating-point number. 
> But if x is an integer, an integer division is performed instead of a 
> floating-point one, and y will be 0.
>
> It's a very common newbie trap

Agreed! :( 





More information about the Digitalmars-d mailing list