problem with std.variant rounding

Suliman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 28 09:50:21 PDT 2017


On Friday, 28 April 2017 at 16:49:18 UTC, kinke wrote:
> On Friday, 28 April 2017 at 16:24:55 UTC, Suliman wrote:
>> import std.stdio;
>> import std.variant;
>>
>> void main()
>> {
>> 	Variant b = 56.051151;
>> 	float x = b.coerce!float;
>> 	writeln(x);
>> }
>>
>>>56.0512
>
> void main()
> {
>     import core.stdc.stdio;
>     import std.stdio;
>
>     double d = 56.051151;
>     writeln(d);
>     printf("%g %f %a\n\n", d, d, d);
>
>     real r = 56.051151L;
>     writeln(r);
>     printf("%Lg %Lf %La\n", r, r, r);
> }
>
> =>
>
> 56.0512
> 56.0512 56.051151 0x1.c068c1db0142fp+5
>
> 56.0512
> 56.0512 56.051151 0x1.c068c1db0142f61ep+5
>
> So using write[ln]() to check floating-point values isn't a 
> good idea as you may lose precision; hex formatting (or a 
> proper debugger) is a much better choice. Additionally, your 
> value isn't *exactly* representable; you may want to read up on 
> floating-point representations if that's unclear.

Yeah! It was issue with rounding during writeln


More information about the Digitalmars-d-learn mailing list