%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.
#include <stdio.h>
int main(void)
{
printf("%g\n", 0.0000000000000000000000124);
return 0;
}
Output:
1.24e-23
That may be what you meant. If not, what did you mean?
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
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>
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
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
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.
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.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
> ssylee wrote:
>>
>> 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>
I assume this will be your answer for ALL questions covered by the C
standard then Chuck?
Good work. Less prone to error......
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
> ssylee <stani...@gmail.com> writes:
> [...]
>> 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.
Great advice.
If you use the cross platform gcc then use gprof.
http://www.cs.duke.edu/~ola/courses/programming/gprof.html
All C programmers should use something like this in conjunction with a
good debugger like gdb on a regular basis.
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
--
Bartc
> "CBFalconer" <cbfal...@yahoo.com> wrote in message
> news:49791B21...@yahoo.com...
>> ssylee wrote:
>>>
>>> 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>
>
> 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.
Bingo!
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".
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.
Gee, d'ya reckon?
You'll be saying next that CBF was being deliberately and wilfully
unhelpful!
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.
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.
That is doubtful, but would depend on the code you used to install
the number. Show the code for a reasonable answer.
Is he worrying about execution time, which is highly machine
specific, and thus off-topic. I assumed he was worrying about
displaying a zero for a small value.
<snip>
> It is interesting to note that this generated 7 replies, of which
> at least 4, possibly 5, are pure trollwork.
Please - do us all a favour, and use a killfile. Then these
statistics will trouble you (and us) no longer.
> Richard the nameless
> troll entered 3 trolls, and Twinkletroll one more.
Who gives a tinker's cuss?
<snip>
> > 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.
any good reference should explain these. Dinkum's
website is pretty good or K&R for a dead tree
version
<snip>