[Tutor] Meaning of %g ?

Terry Carroll carroll at tjc.com
Mon Apr 17 20:35:13 CEST 2006

On Mon, 17 Apr 2006, Alan Gauld wrote:

> >>>> a = 123456.78
> >>>> print "%g\n%e\n%f" % (a,a,a)
> > 123457
> > 1.234568e+005
> > 123456.780000
> >Float number loses digits and becomes integer in the output ?
> Yep, I am rapidly coming to the comnclusion that %g is broken 
> in Python. I must do some tests with gcc to see what it does 
> with %g, it may be the wierd behaviour is coming from there.
> I've never used %g with gcc...

FWIW, (Cygwin) Perl gives a similar result:

 > perl  -e 'my $a = 123456.78;printf ("%g\n%e\n%f", $a, $a, $a);'

The only difference being the two-digit vs. three-digit exponent on %e 
(Python's 1.234568e+005 vs. Perl's 1.234568e+05).

Hmm.. Upon further review, Cygwin Perl gives 1.234568e+05; while 
Activestate Perl gives 1.234568e+005.  (For reasons that elude me, 
Activestate Perl won't do the command-line version, but I don't care 
enough to track it down.)

More information about the Tutor mailing list