Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
July 11, 2009 formatting floating point | ||||
---|---|---|---|---|
| ||||
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 |
July 11, 2009 Re: formatting floating point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Sat, Jul 11, 2009 at 5:50 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?
|
July 11, 2009 Re: formatting floating point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | > 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. |
July 11, 2009 Re: formatting floating point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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. |
July 11, 2009 Re: formatting floating point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | "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 |
July 12, 2009 Re: formatting floating point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Sat, Jul 11, 2009 at 7:39 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.
>
> 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?
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.
|
July 12, 2009 Re: formatting floating point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | >Ohh, I see. Your initial question was really vague, now that I see what you were asking. 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] |
July 13, 2009 Re: formatting floating point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Saaa wrote:
>> Ohh, I see. Your initial question was really vague, now that I see
>> what you were asking.
> 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.
|
July 14, 2009 Re: formatting floating point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don | >>> 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
to work perfectly already.
Although there probably is a better formatting option, I hope.
|
Copyright © 1999-2021 by the D Language Foundation