## digitalmars.D.learn - formatting floating point

• Saaa (5/5) Jul 11 2009 double d[2] = [ 0, 1, double.max];
• Jarrett Billingsley (2/7) Jul 11 2009 You want a 309-digit number consisting mostly of 0s?
• Saaa (4/10) Jul 11 2009 Yes, but only if they are necessary.
• Jarrett Billingsley (12/23) Jul 11 2009 Um, doubles don't have infinite precision. See those digits that it
• Saaa (9/34) Jul 11 2009 I now see what you mean by 309 digits.. No I don't want that !! :D
• Jarrett Billingsley (4/17) Jul 11 2009 Ohh, I see. Your initial question was really vague, now that I see
• Saaa (10/14) Jul 11 2009 I found the formatting options, they are in std.format..
• Don (2/21) Jul 13 2009 double.dig, real.dig gives you the number of meaningful decimal digits.
• Saaa (4/19) Jul 13 2009 Ah, I see, thanks.
"Saaa" <empty needmail.com> writes:
```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
```
Jul 11 2009
Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
```On Sat, Jul 11, 2009 at 5:50 PM, Saaa<empty needmail.com> wrote:
double d[2] =3D [ 0, 1, double.max];
char[] c =3D format(d);

=A0How 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?
```
Jul 11 2009
"Saaa" <empty needmail.com> writes:
``` 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.

What else is the point in saving doubles.

```
Jul 11 2009
Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
```On Sat, Jul 11, 2009 at 6:44 PM, Saaa<empty 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.
```
Jul 11 2009
"Saaa" <empty needmail.com> writes:
```"Jarrett Billingsley" <jarrett.billingsley gmail.com> wrote in message
news:mailman.51.1247352795.14071.digitalmars-d-learn puremagic.com...
On Sat, Jul 11, 2009 at 6:44 PM, Saaa<empty 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.

I now see what you mean by 309 digits.. No I don't want that !! :D
string.format does only 6 digits by default, I added the extra double.max
digits in myself from wikipedia :)
I overestimated the precision of a double, my bad.
(53bits != 309 digits !)
How can I format to the full number of digits, like I did for c?

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.

I could add the option, but for now I'm going for human readability
```
Jul 11 2009
Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
```On Sat, Jul 11, 2009 at 7:39 PM, Saaa<empty needmail.com> wrote:
Um, doubles don't have infinite precision. =A0See those digits that it
output? =A0That's all you get. =A0Those are the only digits that are
necessary because those are the only digits that are *stored*. =A0Just
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.

I now see what you mean by 309 digits.. No I don't want that !! :D
string.format does only 6 digits by default, I added the extra double.max
digits in myself from wikipedia :)
I overestimated the precision of a double, my bad.
(53bits !=3D 309 digits !)
How can I format to the full number of digits, like I did for c?

Ohh, I see.  Your initial question was really vague, now that I see
what you were asking.  You'd just have to convert each element of the
array separately.
```
Jul 11 2009
"Saaa" <empty needmail.com> writes:
```Ohh, I see.  Your initial question was really vague, now that I see

sorry
You'd just have to convert each element of the
array separately.

I found the formatting options, they are in std.format..
(I was apparently searching for the 'g' option.. )

this seems to work (using std2.string.format)

double[] d1;
d1=[double.max,double.min];
char[] c;
c=format( "%.100g",d1); //100 is enough for reals I think?
writefln(c);
//[1.7976931348623157079e+308,2.2250738585072013832e-308]
```
Jul 11 2009
Don <nospam nospam.com> writes:
```Saaa wrote:
Ohh, I see.  Your initial question was really vague, now that I see

sorry
You'd just have to convert each element of the
array separately.

I found the formatting options, they are in std.format..
(I was apparently searching for the 'g' option.. )

this seems to work (using std2.string.format)

double[] d1;
d1=[double.max,double.min];
char[] c;
c=format( "%.100g",d1); //100 is enough for reals I think?
writefln(c);
//[1.7976931348623157079e+308,2.2250738585072013832e-308]

double.dig, real.dig gives you the number of meaningful decimal digits.
```
Jul 13 2009
"Saaa" <empty needmail.com> writes:
``` You'd just have to convert each element of the
array separately.

I found the formatting options, they are in std.format..
(I was apparently searching for the 'g' option.. )

this seems to work (using std2.string.format)

double[] d1;
d1=[double.max,double.min];
char[] c;
c=format( "%.100g",d1); //100 is enough for reals I think?
writefln(c);
//[1.7976931348623157079e+308,2.2250738585072013832e-308]

double.dig, real.dig gives you the number of meaningful decimal digits.

Ah, I see, thanks.
But choosing an insane big number of precision in combination with g seems