From: Adrian Popescu <adixpope@rdsnet.ro>
To: linux-c-programming@vger.kernel.org
Subject: printf format for Float number
Date: Wed, 29 Sep 2004 17:01:16 +0300 [thread overview]
Message-ID: <200409291701.16821.adixpope@rdsnet.ro> (raw)
Can anyone explain this please ???
(what am I getting wrong and how can I avoid this ?)
I get a roughly output aproximation of a float number
using printf: (f=123.1 to 123.099998)
If I use the format:
%f i get 123.099998
%g i get 123.1
The printf "FORMAT" says:
%f double-precision floating-point
%g double-precision floating-point, using %f or %e
I read that "float" is promoted to "double" by printf for Format "%f"
If I use "double" instead of "float" it works fine.
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
/*----------source---------------*/
#include <stdio.h>
int main() {
float f=123.1;
double d=123.1;
printf ("%f\n", f);
printf ("%g\n", f);
printf ("%f\n", d);
printf ("%f\n", 123.1);
}
/*--------------------------------*/
<OUTPUT>
123.099998 <---- ?!?!?!
123.1
123.100000
123.100000
Thanx
___
Adrian Popescu
next reply other threads:[~2004-09-29 14:01 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-09-29 14:01 Adrian Popescu [this message]
2004-10-01 1:53 ` printf format for Float number Suciu Flavius
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200409291701.16821.adixpope@rdsnet.ro \
--to=adixpope@rdsnet.ro \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).