integer division with float result

Bill Baxter dnewsgroup at billbaxter.com
Sun Nov 18 16:46:42 PST 2007


Stas Sergeev wrote:
> Hi.
> 
> This is basically an out of curiosity.
> I wonder how does D handle the integer division.
> In C, as we all know, the following:
> int a = 3;
> int b = 5;
> float c = a / b;
> will give the result that will beat the ghost
> out of any mathematician (unless you cast
> one of the operands to float, of course).
> I haven't used D yet, but I know that most
> (or all?) of the C-unrelated languages do
> that in a proper way, and even the Python
> developers are going to fix that in the future.
> 
> So the question is: how does D handle that?
> Was it infected by the Cish way of handling
> an integer division (if so - why?), or managed
> to get rid of that legacy thingy?

Comparing with dynamically typed languages like Python isn't useful 
because in Python a/2 can return an integer for if a==4 a float if a==5. 
  A statically typed language like D doesn't have that luxury.

Anyway, that said, I think it'd be nice to have / do float division, and 
introduce another operator (like python's //) for truncated integer 
division.  But I think Walter is too entrenched in C-ish ways to see any 
value in changing how it works.  Python is changing because it wants to 
be newbie/scientist friendly above all else.  D merely wants to be 
easier to use than C++.  There's a lot of territory between 
"newbie-friendy" and "easier than C++".  In particular "has pointers" 
and "newbie-friendly" tend to keep their distance from one another.

--bb



More information about the Digitalmars-d mailing list