473,657 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

most accurate printf("%f")

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%c gl%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
Jul 22 '05 #1
14 7978
"Gernot Frisch" <Me@Privacy.net > wrote in message
news:2s******** *****@uni-berlin.de...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2

"Ivan Vecerina" <NO************ *************** *******@vecerin a.com>
schrieb im Newsbeitrag news:ck******** **@newshispeed. ch...
"Gernot Frisch" <Me@Privacy.net > wrote in message
news:2s******** *****@uni-berlin.de...
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.


double f = 10000.000000012 3567;
printf("%20.15f - %.15g\n", f, f);
// 10000.000000012 356000 - 10000.000000012 4


Jul 22 '05 #3
Gernot Frisch wrote:

"Ivan Vecerina" <NO************ *************** *******@vecerin a.com>
schrieb im Newsbeitrag news:ck******** **@newshispeed. ch...
"Gernot Frisch" <Me@Privacy.net > wrote in message
news:2s******** *****@uni-berlin.de...
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.


double f = 10000.000000012 3567;


cout the digits

10000.000000012 3567

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!

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #4
Gernot Frisch wrote:

"Ivan Vecerina" <NO************ *************** *******@vecerin a.com>
schrieb im Newsbeitrag news:ck******** **@newshispeed. ch...
"Gernot Frisch" <Me@Privacy.net > wrote in message
news:2s******** *****@uni-berlin.de...
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.


double f = 10000.000000012 3567;


cout the digits

10000.000000012 3567

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!

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #5
> cout the digits

10000.000000012 3567

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?
Jul 22 '05 #6
Gernot Frisch wrote:
cout the digits

10000.000000012 3567

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?


15 is average. It depends on the number itself how many digits you get
exactly. BTW by assining 10000.000000012 3567 and getting
10000.000000012 356000 one cannot talk from correctly. The last
digit you assigned was 7, printf brought back a 0 (and didn't round
the previous 6 to 7).

Might be interesting for you:
http://www.petebecker.com/js200006.html
http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #7
> 15 is average. It depends on the number itself how many digits you
get
exactly. BTW by assining 10000.000000012 3567 and getting
10000.000000012 356000 one cannot talk from correctly. The last
digit you assigned was 7, printf brought back a 0 (and didn't round
the previous 6 to 7).

Might be interesting for you:
http://www.petebecker.com/js200006.html
http://docs.sun.com/source/806-3568/ncg_goldberg.html


Thank's a lot!
Jul 22 '05 #8
Gernot Frisch wrote:
cout the digits

10000.0000000 123567

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?


Absolutely.

Change the number (to be, e.g., 10000.000000012 3333) 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
Jul 22 '05 #9
On Mon, 11 Oct 2004 11:40:15 +0200, "Gernot Frisch" <Me@Privacy.net >
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
5084
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is relative NOT to the immediate script that is including it, but is relative to the top-level calling script. In practice, this means that you have to constantly worry and adjust paths in includes, based on the startup scripts that call these...
40
5696
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but welcome any improvements: i = 0; if (g && 1) i++; if (g && 2) i++;
14
2727
by: Peter Mount | last post by:
Hello I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I run the program in cygwin on Windows 98SE it skips that line completely and ends the program. Does scanf have problems with "%c" or is it the operating system I'm using? 1 #include <stdio.h> 2 3 int main()
33
3155
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
43
2713
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \ !(v)->vcpu_info->evtchn_upcall_mask) whereas evtchn_upcall_pending is of type unsigned char (and also evtchn_upcall_mask is of type unsigned char).
5
2199
by: chandanlinster | last post by:
consider the program, /*********************************************************/ int main() { int a; printf("a = %u\n", a); printf("*a = %u\n", *a); return 0; }
51
4104
by: Spidey | last post by:
for(;0;) printf("hello"); even the condition s wrong i get a hello printed on the screen y s this happening
29
11081
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
16
7410
by: hiteshthappa | last post by:
hi Can anyone please help me in finding the total number of words in a file I get the newlines, characters and blankspaces correctly but counting words ia problem.I have tried many ways but it didnt help. Here is my code...... #include <string.h> #include <stdio.h> main(int argc , char *argv)
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.