So why double to float conversion is implicit ?

user1234 user1234 at 12.nl
Sat Oct 21 20:44:37 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.

D is compliant with C++ in this case (see 
http://en.cppreference.com/w/cpp/language/implicit_conversion) so 
the question is rather why does C++ allow this conversion. When 
you convert a double to a float you're more likely to have a 
precision loss while when you convert an ulong to an int you risk 
a most serious data loss.


More information about the Digitalmars-d mailing list