Handling big FP numbers

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Feb 9 05:46:22 UTC 2019

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.


If it breaks, you get to keep both pieces. -- Software disclaimer notice

More information about the Digitalmars-d-learn mailing list