Promotion rules ... why no float?

Daniel Kozak via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 6 00:52:47 PDT 2016


Dne 6.9.2016 v 09:04 Sai via Digitalmars-d napsal(a):
> Consider this:
>
> import std.stdio;
> void main()
> {
>     byte a = 6, b = 7;
>     auto c = a + b;
>     auto d = a / b;
>     writefln("%s, %s", typeof(c).stringof, c);
>     writefln("%s, %s", typeof(d).stringof, d);
> }
>
> Output :
> int, 13
> int, 0
>
> I really wish d gets promoted to a float. Besides C compatibility, any 
> reason why d got promoted only to int even at the risk of serious bugs 
> and loss of precision?
>
> I know I could have typed "auto a = 6.0" instead, but still it feels 
> like an half-baked promotion rules.
No, it is really important rule. If there will be automatic promotion to 
float for auto it will hurt performance
in cases when you want int and it will break things.

But maybe in below case it could make more sense:

float d = a / b; // or it could print a warning because there is a high 
probability this is an error

ok maybe something like linter could be use to find those places



More information about the Digitalmars-d mailing list