So why double to float conversion is implicit ?

Basile B. b2.temp at gmx.com
Tue Oct 24 14:28:20 UTC 2017


On Tuesday, 24 October 2017 at 01:22:57 UTC, codephantom wrote:
> On Monday, 23 October 2017 at 21:51:24 UTC, Basile B. wrote:
>> ---
>> /**
>>  * Wraps a floating point type that doesn't follow D 
>> permissive float conversion
>>  * rules.
>>  *
>>  * In D, as in C++, implicit conversion from $(D double) to 
>> $(D float) is allowed,
>>  * leading to a possible precision loss. This can't happen 
>> when using this wrapper.
>>  */
>
> Want to hammer in a nail.. just go ahead and use a bulldozer ;-)
>
> Simple things should be simple.
>
> I cannot see how the simplest solution would be any other than 
> to have a compiler option for warning you of implicit 
> conversions.

I'm gonna propose the change in a PR. But I already see 
problems...
One may call a trigo function with the highest precision possible 
so that the FP coprocessor get more accurate internally. The 
result, even if converted from real to float is better. This is 
exactly what happens here:

https://github.com/dlang/phobos/pull/5804/files#diff-f127f38af25baf8333b65fa51824b92fR3037

To be clear, w/ the patch a warning is emitted for the first cos:

void main()
{
     import std.math;
     float f0 = cos(2 * PI /  1.123548789545545646452154L);
     float f1 = cos(cast(float)(2 * PI /  
1.123548789545545646452154L));
     writefln("%.8g - %.8g", f0, f1);
}

but paradoxically the first is more accurate.







More information about the Digitalmars-d mailing list