Handling big FP numbers

Murilo murilomiranda92 at hotmail.com
Sun Feb 10 20:25:02 UTC 2019


On Saturday, 9 February 2019 at 05:46:22 UTC, H. S. Teoh wrote:
> On Sat, Feb 09, 2019 at 03:52:38AM +0000, Murilo via 
> Digitalmars-d-learn wrote:
>> On Saturday, 9 February 2019 at 03:28:24 UTC, Adam D. Ruppe 
>> wrote:
> [...]
>> > But you can change this with the format specifiers (use 
>> > `writefln` instead of `writeln` and give a precision 
>> > argument) or, of course, you can use the same C printf 
>> > function from D.
>> 
>> Thanks, but which precision specifier should I use? I already 
>> tried "%.20f" but it still produces a result different from C? 
>> Is there a way to make it identical to C?
>
> I say again, you're asking for far more digits than are 
> actually there. The extra digits are only an illusion of 
> accuracy; they are essentially garbage values that have no real 
> meaning. It really does not make any sense to print more digits 
> than are actually there.
>
> On the other hand, if your purpose is to export the double in a 
> textual representation that guarantees reproduction of exactly 
> the same bits when imported later, you could use the %a format 
> which writes out the mantissa in exact hexadecimal form. It can 
> then be parsed again later to reproduce the same bits as was in 
> the original double.
>
> Or if your goal is simply to replicate C behaviour, there's 
> also the option of calling the C fprintf directly. Just import 
> core.stdc.stdio or declare the prototype yourself in an 
> extern(C) declaration.
>
>
> T

Thanks, but even using core.stdc.stdio.fprintf() it still shows a 
different result on the screen. It seems this is a feature of D I 
will have to get used to and accept the fact I can't always get 
the same number as in C even though in the end it doesn't make a 
difference cause as you explained the final digits are just 
garbage anyway.


More information about the Digitalmars-d-learn mailing list