Detecting inadvertent use of integer division

Aelxx aelxx at yandex.ru
Mon Dec 14 07:02:09 PST 2009


"bearophile" <bearophileHUGS at lycos.com> wrote:
> Don:
>> > In Pascal too (and OCaML, but the situation is different) they are 
>> > separated. I think here having two operators is better,
>>
>> Why?
>
> You are intelligent and expert so you must know my answer, so I fear yours 
> is a trick question :-)
>
> Two operators allow to reduce the need for casts (and 
> rounding/truncation), and are more explicit, allowing the code to express 
> its meaning better to people that come after the original programmer.
>
> You can put them in the middle of a long expression, so you know what it's 
> happening in the middle of it. This is useful for programming newbies too. 
> I know there's a C translation of every usage of those two operators, but 
> this is beside the point: even if the Laws of C language are sometimes 
> explicit, sometimes they are not so natural and easy to remember, because 
> normal people are not computers.
>
> Not every part of the C language is designed perfectly, there's space for 
> improvements.
>
> Bye,
> bearophile
>
I like Pascal having / for floating-point and div for integers. It's rather 
intuiteve that 1/2 = 0.5 and 1 div 2 = 0.
Or worse - what newbies use to input things like :
const int b=1, a=0, n = 10 ;
double h = (b-a)/n ; // haha who expect it be 0.0

As fact integer division is very different operation then floating point 
one. IMHO giving them one is  a little like giving < and > operators second 
usage for templates.

But besides that all it's only a convenience issue. Having enough 
expierience and making explicit casting is rather quick thing. 





More information about the Digitalmars-d mailing list