## 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

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

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 fullyYou want a 309-digit number consisting mostly of 0s?

Jul 11 2009

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 fullyYou 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

On Sat, Jul 11, 2009 at 6:44 PM, Saaa<empty needmail.com> wrote: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.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 fullyYou 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.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

"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: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?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.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.I could add the option, but for now I'm going for human readabilityWhat 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

On Sat, Jul 11, 2009 at 7:39 PM, Saaa<empty needmail.com> wrote: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.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?

Jul 11 2009

Ohh, I see. Your initial question was really vague, now that I see what you were asking.sorryYou'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

Saaa wrote:double.dig, real.dig gives you the number of meaningful decimal digits.Ohh, I see. Your initial question was really vague, now that I see what you were asking.sorryYou'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 13 2009

Ah, I see, thanks. But choosing an insane big number of precision in combination with g seems to work perfectly already. Although there probably is a better formatting option, I hope.double.dig, real.dig gives you the number of meaningful decimal digits.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 13 2009