printf on a very small floating point number

S

ssylee

How would I printf a really small number (e.g. 1.24e-23) without
having 0.000000...000e+000 shown on the output? i.e. what flag would
that be?
 
U

user923005

How would I printf a really small number (e.g. 1.24e-23) without
having 0.000000...000e+000 shown on the output? i.e. what flag would
that be?

%g

From K&R2, p.126:
g,G
double; use %e or %E if the exponent is less than -4 or greater than
or equal to the precision; otherwise use %f. Trailing zeros and a
trailing decimal point are not printed.
 
C

CBFalconer

ssylee said:
How would I printf a really small number (e.g. 1.24e-23) without
having 0.000000...000e+000 shown on the output? i.e. what flag
would that be?

Why don't you look it up in the C standard. The things marked C99
below are the standard.

Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/> (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2> (pre-C99)
<http://www.dinkumware.com/c99.aspx> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
M

Martin Ambuhl

ssylee said:
How would I printf a really small number (e.g. 1.24e-23) without
having 0.000000...000e+000 shown on the output? i.e. what flag would
that be?

You could simply use any of %e, %E, %g, %G, or (if appropriate and
available) %a or %A. And %f will work with appropriate field width and
precision. Here is an example using %g:

#include <stdio.h>
#include <float.h>

int main(void)
{
float xf = 1.24e-23f;
double xd = 1.24e-23;
long double xl = 1.24e-23L;
printf("1.24e-23 stored in a float: %.*g\n"
"%21sdouble: %.*g\n"
"%21slong double: %.*Lg\n",
FLT_DIG, xf, "", DBL_DIG, xd, "", LDBL_DIG, xl);
return 0;
}


[Output]
1.24e-23 stored in a float: 1.24e-23
double: 1.24e-23
long double: 1.24e-23
 
S

Sjouke Burry

ssylee said:
How would I printf a really small number (e.g. 1.24e-23) without
having 0.000000...000e+000 shown on the output? i.e. what flag would
that be?
You would print it as 1.24E-23 , in other words
use scientific notation(see options of printf).
 
S

ssylee

You would print it as 1.24E-23 , in other words
use scientific notation(see options of printf).

I have attempted to look up the % flags. Apparently, when I ran the
code in the debugger, the values are indeed zero, not just a very
small number. It looks like I don't have a good way to time function
execution time.
 
S

Sjouke Burry

ssylee said:
I have attempted to look up the % flags. Apparently, when I ran the
code in the debugger, the values are indeed zero, not just a very
small number. It looks like I don't have a good way to time function
execution time.
The only way to get accurate timings, is machine
specific.
I have done it with microseconds, by reading a cpu
counter register(2.5MHZ).
But that cannot be done in a portable way.
The way to do it generally, might be to call an
essential part of your functions several million times,
and count seconds, you can do that even by reading
your(stop)watch .
 
S

ssylee

The only way to get accurate timings, is machine
specific.
I have done it with microseconds, by reading a cpu
counter register(2.5MHZ).
But that cannot be done in a portable way.
The way to do it generally, might be to call an
essential part of your functions several million times,
and count seconds, you can do that even by reading
your(stop)watch .

Well, I just want to figure out how much time it takes for each
particular function in my code to run comparatively, so that I can
figure out which function to optimize in terms of priorities.
 
K

Keith Thompson

ssylee said:
Well, I just want to figure out how much time it takes for each
particular function in my code to run comparatively, so that I can
figure out which function to optimize in terms of priorities.

See if your system has a profiler.
 
R

Richard

CBFalconer said:

I assume this will be your answer for ALL questions covered by the C
standard then Chuck?

Good work. Less prone to error......
 
W

William Hughes

Well, I just want to figure out how much time it takes for each
particular function in my code to run comparatively, so that I can
figure out which function to optimize in terms of priorities.



What you need is a profiler which does exactly that. They take a bit
of
work to set up, but nothing as hard as doing your own timing,
and output is much better. Many have graphical tools which can really
help. If you have a debugger you probably have a profiler.
(If you use gcc look for gprof)

- William Hughes
 
B

Bartc

CBFalconer said:

If I had a deadline to meet I think I'd rather be told to try %g printf
format than have to wade through this lot.

Or at least a more specific reference, such as this (ms-oriented) table:

http://msdn.microsoft.com/en-us/library/hf4y5e3w(VS.80).aspx
 
R

Richard

Walter Banks said:
I thought that N869 had an ISO WG14 copyright,

Falconer wouldn't have advertised it if it was illegal. After all he
frequently chastises people for posting links to things he deems
"illegal".
 
J

jameskuyper

Walter said:
I thought that N869 had an ISO WG14 copyright,

It's a draft version of the standard that was made freely available as
a PDF to the general public. It's was already out of date when the
official standard was approved, and became even more so with each of
the three Technical Corrigenda that was approved. This text version is
missing a lot of useful information that was presented in the PDF
original by formatting. It's also a lot harder to read, for the same
reason. It's mainly of use to people who find the search capabilities
of their PDF readers hard to use. For the rest of us, n1256.pdf is a
much superior document. I'm not sure precisely what the copyright laws
say about such things, but even if this is technically illegal, I
doubt that anyone cares enough to object. Lawrence Jones is
responsible for editing the official standard, and I'm reasonably sure
he's seen CBFalconer's many previous references to this file, and has
yet to voice an objection.
 
A

Antoninus Twink

If I had a deadline to meet I think I'd rather be told to try %g printf
format than have to wade through this lot.

Gee, d'ya reckon?

You'll be saying next that CBF was being deliberately and wilfully
unhelpful!
 
C

CBFalconer

Bartc said:
If I had a deadline to meet I think I'd rather be told to try %g
printf format than have to wade through this lot.

But, if you download and hang onto a standard copy, you can satisfy
yourself as to all sorts of things. For example, it is handy to
check the parameters of any standard function, also types, etc.
Most of these things can be examined within about 5 seconds,
especially with the text version.
 
C

CBFalconer

CBFalconer said:

It is interesting to note that this generated 7 replies, of which
at least 4, possibly 5, are pure trollwork. Richard the nameless
troll entered 3 trolls, and Twinkletroll one more. Two of the
further responses were replies to the trollers.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top