Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

printf on a very small floating point number

2 views
Skip to first unread message

ssylee

unread,
Jan 22, 2009, 5:19:44 PM1/22/09
to
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?

user923005

unread,
Jan 22, 2009, 5:26:09 PM1/22/09
to

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

Richard Heathfield

unread,
Jan 22, 2009, 5:31:39 PM1/22/09
to
ssylee said:

#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

CBFalconer

unread,
Jan 22, 2009, 8:19:29 PM1/22/09
to

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.

Martin Ambuhl

unread,
Jan 22, 2009, 9:10:05 PM1/22/09
to

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

Sjouke Burry

unread,
Jan 22, 2009, 9:33:19 PM1/22/09
to
You would print it as 1.24E-23 , in other words
use scientific notation(see options of printf).

ssylee

unread,
Jan 23, 2009, 12:52:58 AM1/23/09
to
On Jan 22, 6:33 pm, Sjouke Burry <burrynulnulf...@ppllaanneett.nnll>
wrote:

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.

Sjouke Burry

unread,
Jan 23, 2009, 2:16:31 AM1/23/09
to
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 .

ssylee

unread,
Jan 23, 2009, 2:34:28 AM1/23/09
to
On Jan 22, 11:16 pm, Sjouke Burry <burrynulnulf...@ppllaanneett.nnll>

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.

Keith Thompson

unread,
Jan 23, 2009, 3:37:27 AM1/23/09
to
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.

--
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"

Richard

unread,
Jan 23, 2009, 6:37:10 AM1/23/09
to
CBFalconer <cbfal...@yahoo.com> writes:

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

William Hughes

unread,
Jan 23, 2009, 6:54:06 AM1/23/09
to

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

Richard

unread,
Jan 23, 2009, 6:56:29 AM1/23/09
to
Keith Thompson <ks...@mib.org> writes:

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


Bartc

unread,
Jan 23, 2009, 7:58:09 AM1/23/09
to

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

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

Richard

unread,
Jan 23, 2009, 8:04:37 AM1/23/09
to
"Bartc" <ba...@freeuk.com> writes:

> "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!

Walter Banks

unread,
Jan 23, 2009, 9:06:35 AM1/23/09
to
> CBFalconer <cbfal...@yahoo.com> writes:
>
> > <http://cbfalconer.home.att.net/download/n869_txt.bz2> (pre-C99)

I thought that N869 had an ISO WG14 copyright,


Richard

unread,
Jan 23, 2009, 9:34:29 AM1/23/09
to
Walter Banks <wal...@bytecraft.com> writes:

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

jameskuyper

unread,
Jan 23, 2009, 11:10:13 AM1/23/09
to

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.

Antoninus Twink

unread,
Jan 23, 2009, 1:07:58 PM1/23/09
to
On 23 Jan 2009 at 12:58, Bartc wrote:

> "CBFalconer" <cbfal...@yahoo.com> wrote:
>> Why don't you look it up in the C standard.
>
> 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!

CBFalconer

unread,
Jan 23, 2009, 6:03:47 PM1/23/09
to
Bartc wrote:
> "CBFalconer" <cbfal...@yahoo.com> wrote in message
>> 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.

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.

CBFalconer

unread,
Jan 23, 2009, 6:11:10 PM1/23/09
to
CBFalconer wrote:
> 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>

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.

CBFalconer

unread,
Jan 23, 2009, 6:14:22 PM1/23/09
to

That is doubtful, but would depend on the code you used to install
the number. Show the code for a reasonable answer.

CBFalconer

unread,
Jan 23, 2009, 6:17:59 PM1/23/09
to
Sjouke Burry wrote:
> ssylee wrote:
>
... snip ...

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

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.

Richard Heathfield

unread,
Jan 23, 2009, 11:01:18 PM1/23/09
to
CBFalconer said:

<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?

nick_keigh...@hotmail.com

unread,
Jan 25, 2009, 12:00:55 PM1/25/09
to
On 23 Jan, 05:52, ssylee <staniga...@gmail.com> wrote:
> On Jan 22, 6:33 pm, Sjouke Burry <burrynulnulf...@ppllaanneett.nnll>

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

0 new messages