Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > most accurate printf("%f")

Reply
Thread Tools

most accurate printf("%f")

 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-12-2004
"Jack Klein" <(E-Mail Removed)> wrote...
> On Mon, 11 Oct 2004 11:40:15 +0200, "Gernot Frisch" <(E-Mail Removed)>
> wrote in comp.lang.c++:
>
>> Hi,
>>
>>
>> I want to represent a double in char* as accurate as possible, but as
>> short as possible:
>>
>> double char*
>> 10000 1E5
>> 1000.00123 1000.00123
>> ...
>>
>> How can I archive this?
>> %.10f prints too much, and %.10g is too inaccurate.

>
> sprintf() with "%.20f" into a large enough character array buffer.
> Trim trailing zeros. Do what you need to do with the result.


So, how well is it going to represent 1.000000000009e-22? Just curious,
I guess...

Victor


 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      10-12-2004

> So, how well is it going to represent 1.000000000009e-22? Just
> curious,
> I guess...


1.000000000009e-22


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-12-2004
Gernot Frisch wrote:

>>So, how well is it going to represent 1.000000000009e-22? Just
>>curious,
>>I guess...

>
>
> 1.000000000009e-22



Is that so? You get that output using the suggested %.20f format?
REALLY??? What compiler/library are you using?
 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      10-12-2004

"Victor Bazarov" <(E-Mail Removed)> schrieb im Newsbeitrag
news:IhRad.4655$(E-Mail Removed)09.us.to.v erio.net...
> Gernot Frisch wrote:
>
>>>So, how well is it going to represent 1.000000000009e-22? Just
>>>curious,
>>>I guess...

>>
>>
>> 1.000000000009e-22

>
>
> Is that so? You get that output using the suggested %.20f format?
> REALLY??? What compiler/library are you using?




int main(int, char**)
{
double f = 1.000000000009e-22;
printf("%20.15f %.15g\n", f, f);
}

// output:
// 0.000000000000000 1.000000000009e-022

gcc 2.95.3-6



 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-12-2004
Gernot Frisch wrote:
> "Victor Bazarov" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:IhRad.4655$(E-Mail Removed)09.us.to.v erio.net...
>
>>Gernot Frisch wrote:
>>
>>
>>>>So, how well is it going to represent 1.000000000009e-22? Just
>>>>curious,
>>>>I guess...
>>>
>>>
>>>1.000000000009e-22

>>
>>
>>Is that so? You get that output using the suggested %.20f format?
>>REALLY??? What compiler/library are you using?

>
>
>
>
> int main(int, char**)
> {
> double f = 1.000000000009e-22;
> printf("%20.15f %.15g\n", f, f);
> }
>
> // output:
> // 0.000000000000000 1.000000000009e-022
>
> gcc 2.95.3-6


Well, duh. The 'g' format definitely works. That's not what Jack
suggested. He suggested the 'f' format and I pointed out that it's
not going to work for small numbers, as you confirmed.

V
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic digital image editing -- which programs are most accurate? socraticquest Digital Photography 1 12-30-2010 06:58 AM
Flickr: difference between "most relevant" and "most interesting" Max Digital Photography 7 09-26-2007 10:38 PM
Re: Most accurate browser for CSS? kchayka HTML 0 04-02-2004 02:12 PM
Re: Most accurate browser for CSS? Whitecrest HTML 0 04-01-2004 01:19 AM
Invoking the Constructor of the Top Most Class in the Hierarchy from the Bottom most class H.MuthuKumaraRajan Java 3 02-04-2004 01:33 PM



Advertisments