[Tutor] Meaning of %g ?

Alan Gauld alan.gauld at freenet.co.uk
Tue Apr 18 09:30:56 CEST 2006

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

Tried it and get the same behaviour.

Tried the Borland C++ compiler and got the same behaviour.

Tried my MIX K&R compiler and - it didn't work! - it's a
pure DOS application and XP barfed... Oh well!

So it looks like Python is consistent with the norm for C compilers
on PCs at least.

I tried looking up the ANSI standard to see what it said but
couldn't find a clear statement there either. And finally, the most precise
statement seemed to come from the Linux printf man page:

 The double argument is converted in style f or e (or F or E for G 
The precision specifies the number of significant digits. If the precision 
is missing,
6 digits are given; if the precision is zero, it is treated as 1. Style e is 
used if the
exponent from its conversion is less than -4 or greater than or equal to the 
Trailing zeros are removed from the fractional part of the result; a decimal
point appears only if it is followed by at least one digit.

At this point I'll setytle for Python doing the "normal" thing with %g but
for me not to use it because I can't see that behaviour being very useful!

Alan G
Author of the learn to program web tutor

More information about the Tutor mailing list