linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nate Jenkins" <nate@uniwest.com>
To: Glynn Clements <glynn@gclements.plus.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Double values - what precision do I use for fprintf?
Date: Fri, 13 Jan 2006 16:35:40 -0800	[thread overview]
Message-ID: <007201c618a2$7233be00$8e01a8c0@Nate> (raw)
In-Reply-To: 17352.15389.697568.176591@cerise.gclements.plus.com


>
> Nate Jenkins wrote:
>
>> If it is a 64-bit-floating-point data type, then isn't one supposed to 
>> use
>> the "long" modifier, i.e. "%.52f" -> "%.52lf"?  Or is that not portable?
>
> You cannot pass a float to printf(); a float will automatically be cast to
> double.
>
> More generally, a float argument will be cast to double if the
> function doesn't have a prototype, or if the prototype doesn't specify
> a type for the argument (i.e. for a variadic function). Similarly,
> char and short values are automatically converted to int in such
> circumstances.
>
> -- 
> Glynn Clements <glynn@gclements.plus.com>
>

So for a short example:
(this should compile but with a few casting warnings)

<.......code.......>

float fNum = 3.1415926535897932384626433832795; // will get truncated
double dNum = fNum; // will get some extra garbage trailing
char cNum = '*'; // 42 in ASCII
short sNum = cNum;
int nNum = cNum;


//////////// floating point number types ////////////

printf("%f \n", fNum);
// IIRC,  7 digits --> "3.141593

printf("%f \n", dNum);
// IIRC, 15 digits --> "3.14159200000261"
// or something like that due to extra garbage
// trailing without formatting?

printf("%lf \n", dNum);
// same --> "3.14159200000261"  Should not work but it does?


//////////// integer number types ////////////

printf("%c \n", cNum);
printf("%c \n", (char)(sNum));
printf("%c \n", (char)(nNum));
// so all of these would show --> "*" ?

printf("%hi \n", cNum);
printf("%hi \n", sNum);
printf("%hi \n", (short)(nNum));
// so all of these would show --> "42" ?

printf("%d \n", cNum);
printf("%d \n", sNum);
printf("%d \n", nNum);
// so all of these would show --> "42" ?

<......./code.......>



Are my comment assumptions wrong?  I have been using "%lf" for years... 
Could the old compiler I use be made to consider "%lf" the same as "%f" ?

Nate



  reply	other threads:[~2006-01-14  0:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-12 12:30 Double values - what precision do I use for fprintf? Shriramana Sharma
2006-01-12 14:17 ` Patrick Leslie Polzer
2006-01-12 18:51 ` Steve Graegert
2006-01-12 21:22   ` Nate Jenkins
2006-01-12 22:00     ` Mihai Dontu
2006-01-13 23:47     ` Glynn Clements
2006-01-14  0:35       ` Nate Jenkins [this message]
2006-01-17  5:37         ` Glynn Clements
2006-01-12 21:57   ` Scott
2006-01-12 22:06     ` Scott
2006-01-13  5:25     ` Steve Graegert

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='007201c618a2$7233be00$8e01a8c0@Nate' \
    --to=nate@uniwest.com \
    --cc=glynn@gclements.plus.com \
    --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).