integer division with float result

Bruce Adams tortoise_74 at yeah.who.co.uk
Sun Nov 18 17:16:10 PST 2007


Bill Baxter Wrote:

> 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

Here's another (non-D?) thought. How about overloading on the return type? In a strongly typed language its only convention that makes the return type somehow special. 
  I believe the original reason for return values being special may have been that they are the only things left on the stack when returning from a function call. This was only true early on when all arguments were passed by value.
  We don't seem to have moved beyond that, we only tease around the edges with covariant returns and returning multiple values but semantically there's no difference between:

void divide(in T numerator, in T denominator, out T result) 

and

T divide(in T numerator, in T denominator)

where T = any numeric type

So why do we still insist on it in modern languages?

Regards,

Bruce.



More information about the Digitalmars-d mailing list