linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* printf format for Float number
@ 2004-09-29 14:01 Adrian Popescu
  2004-10-01  1:53 ` Suciu Flavius
  0 siblings, 1 reply; 2+ messages in thread
From: Adrian Popescu @ 2004-09-29 14:01 UTC (permalink / raw)
  To: linux-c-programming


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




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: printf format for Float number
  2004-09-29 14:01 printf format for Float number Adrian Popescu
@ 2004-10-01  1:53 ` Suciu Flavius
  0 siblings, 0 replies; 2+ messages in thread
From: Suciu Flavius @ 2004-10-01  1:53 UTC (permalink / raw)
  To: linux-c-programming

Hi,

your problem it's a very common "Round-off error".

Please checkout this link from Micro$oft for a tutorial in floating 
points mistakes ;)

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q42/9/80.ASP&NoWebContent=1


Regards and "Numa bine" ;)



Adrian Popescu wrote:
> 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
> 
> 
> 
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-10-01  1:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-29 14:01 printf format for Float number Adrian Popescu
2004-10-01  1:53 ` Suciu Flavius

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