So why double to float conversion is implicit ?

Atila Neves atila.neves at gmail.com
Mon Oct 23 13:30:29 UTC 2017


On Saturday, 21 October 2017 at 20:17:12 UTC, NX wrote:
> I was working on some sort of math library for use in graphical 
> computing and I wrote something like this:
>
> const float PiOver2 = (atan(1.0) * 4) / 2;
>
> Interestingly enough, I realized that atan() returns double (in 
> this case) but wait, it's assigned to a float variable! 
> Compiler didn't even emit warnings, let alone errors.
>
> I see no reason as to why would this be legal in this century, 
> especially in D.
> So can someone tell me what's the argument against this?
> Why type conversions differ between integral and floating types?
> Why can't I assign a long to an int but it's fine when 
> assigning double to float?
>
> I think this is a serious topic and needs clarification.

I think an issue here is specifying `float` to begin with. I'd 
only do that if I wanted it to be `float`, knowing that the 
expression is a different type, and even then I'd add a comment 
explaining why I felt the need to be explicity. i.e. I'd have 
written

const PiOver2 = (atan(1.0) * 4) / 2;

Atila


More information about the Digitalmars-d mailing list