Killing integer division problems?

burt invalid_email_address at cab.abc
Tue Aug 4 15:43:19 UTC 2020


On Tuesday, 4 August 2020 at 15:25:50 UTC, bachmeier wrote:
> On Tuesday, 4 August 2020 at 12:34:37 UTC, burt wrote:
>> [...]
>
> This would be *really* confusing. y is a double in your 
> example, and the user would be forced to cast to an int in 
> order to enable an implicit cast (or promotion, or whatever 
> language you want to use) to a double?
>
> I hate the current default behavior. It's not safe but it is 
> consistent with what a C programmer will expect. You can't have 
> these yielding different values:
>
> ```
> x = 7/3;
> y = 7/3;
> ```

That is not what I was proposing (please reread). I am just 
saying that the bottom one (assuming x is int and y is double) 
should issue a warning, unless it is explicitly told that integer 
division is intended.

> If x is an int and y is a double, you'd get different results, 
> and that would be awful. What's needed is something explicit 
> like this:
>
> y = double(7/3);
> x = 7/3;
> y = 7/3; // Error because of implicit cast

This is the same as what I was proposing, except for the syntax 
to explicitly annotate integer division: in your example, you use 
a cast to double (to show you're casting from integer to 
division), in my example, you use a cast to int (to show you're 
truncating). Open for discussion, but the same idea.

> [...]



More information about the Digitalmars-d mailing list