most accurate printf("%f")

G

Gernot Frisch

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.


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
I

Ivan Vecerina

Gernot Frisch said:
I want to represent a double in char* as accurate as possible, but as
short as possible:
This is the intent of the %g formatter in printf.
double char*
10000 1E5
1000.00123 1000.00123
...

How can I archive this?
%.10f prints too much, and %.10g is too inaccurate.
How is %g "too inaccurate", could you provide an example ?
The accuracy of a double, in IEEE format, is limited to about 15 decimal
digits. So a %.15g should do what you asked for.


Regards,
Ivan
 
G

Gernot Frisch

Ivan Vecerina said:
This is the intent of the %g formatter in printf.

How is %g "too inaccurate", could you provide an example ?
The accuracy of a double, in IEEE format, is limited to about 15
decimal
digits. So a %.15g should do what you asked for.

double f = 10000.0000000123567;
printf("%20.15f - %.15g\n", f, f);
// 10000.000000012356000 - 10000.0000000124
 
K

Karl Heinz Buchegger

Gernot said:
double f = 10000.0000000123567;

cout the digits

10000.0000000123567

00000 0000111111111 (Note: 1 reads: 14
12345 6789012345678 4

So everything to the right of

......123

is most likely noise anyway (assuming a standard
precission of 15 digits)

Note: 15 digits does *not* mean: 15 digits after comma.
It means 15 digits total!
 
K

Karl Heinz Buchegger

Gernot said:
double f = 10000.0000000123567;

cout the digits

10000.0000000123567

00000 0000111111111 (Note: 1 reads: 14
12345 6789012345678 4

So everything to the right of

.......000123

is most likely noise anyway (assuming a standard
precission of 15 digits)

Note: 15 digits does *not* mean: 15 digits after comma.
It means 15 digits total!
 
G

Gernot Frisch

cout the digits
10000.0000000123567

00000 0000111111111 (Note: 1 reads: 14
12345 6789012345678 4

So everything to the right of

.......000123

is most likely noise anyway (assuming a standard
precission of 15 digits)

Note: 15 digits does *not* mean: 15 digits after comma.
It means 15 digits total!

So, why is %.15f printing it correctly? Coincidence?
 
K

Karl Heinz Buchegger

V

Victor Bazarov

Gernot said:
So, why is %.15f printing it correctly? Coincidence?

Absolutely.

Change the number (to be, e.g., 10000.0000000123333) and see if
you get the same "precise" output. I got two different results
on Linux (g++ v 3.2.2) and IRIX (MIPSpro 7.3). Both had garbage
after the first 16 digits. sizeof(double) is 8 on both systems.

Also, change the leading 1 to, say, leading 9, and you will get
even "less" accuracy -- the garbage will begin to appear in the
16th digit.

V
 
J

Jack Klein

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.
 
V

Victor Bazarov

Jack Klein said:
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
 
G

Gernot Frisch

Victor Bazarov said:
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
 
V

Victor Bazarov

Gernot said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

int(a) vs (int)a 19
reference to parent inherritance? 9
queue where I can delete in the middle? 3
std::format 1
a[3} slower than a.x; a.z; a.z 27
date comparison 2
a thread safe stack? 7
operator overloading... 5

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top