formatting floating point

Jarrett Billingsley jarrett.billingsley at gmail.com
Sat Jul 11 15:53:08 PDT 2009


On Sat, Jul 11, 2009 at 6:44 PM, Saaa<empty at needmail.com> wrote:
>> double d[2] = [ 0, 1, double.max];
>> char[] c = format(d);
>>
>> How do I get c to represent full precision?
>>
>> "[0,1,1.7976931348623157e+308]" // but then with double.max being
>> represented fully
>
>> You want a 309-digit number consisting mostly of 0s?
>
> Yes, but only if they are necessary.
> 0 doesn't need all hose digits for instance.

Um, doubles don't have infinite precision.  See those digits that it
output?  That's all you get.  Those are the only digits that are
necessary because those are the only digits that are *stored*.  Just
because it's followed by almost 300 0s doesn't mean that 300 0s are
actually stored in the number; it just means the exponent is that
large.

> What else is the point in saving doubles.

If you're saving doubles in a textual format, try using the %a format
specifier.  It outputs a float in hexadecimal notation, which is a bit
more precise than decimal (since no rounding has to be performed).  I
don't know if Phobos' unformatting can actually _read_ hex floats,
though.


More information about the Digitalmars-d-learn mailing list