On 10/23/2017 07:22 AM, Arun Chandrasekaran wrote:
> void main() {
>      double a = 22/7.0;
>      import std.stdio: writeln, writefln;
>      writefln("%.51f", a);
> }

> But why does the compiler bring the C baggage for the integer
> division? Why do I need to `cast (double)` ?

I think you mean having to write 7.0 to bring a double into the expression.

> Can't the compiler figure
> it out?

Yes, it can but we don't want that. Otherwise, the code would be too slippery to keep under control. For example, introducing the following temporary variable would change the semantics:

  // Before:
  foo(22/7);

  // After
  double temp = 22/7;
  foo(temp);    // Is this the same call with the same value?

Writing the above made me aware that function overloading would be too cumbersome as the compiler would find too many potential matches for the expressions. For example, should it convert 22/7 to float or double or long, etc.

The rule is that every expression has a type and 22/7 is int. The rules are carried from C and can be tricky. Both "Integer Promotions" and "Usual Arithmetic Conversion" are interesting here:

  https://dlang.org/spec/type.html#integer-promotions

Ali

Reply via email to