%g not the same as %e or %f

Kent Johnson kent at kentsjohnson.com
Fri Apr 14 08:02:36 EDT 2006


According to the docs, %g formatting is "Same as "e" if exponent is
greater than -4 or less than precision, "f" otherwise." So I would
expect that for any num, '%g'%num == '%e'%num or '%g'%num == '%f'%num.
But this is not the case in fact:

Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.

In [1]: num = 1234567898.2345678945

In [2]: print "%g\n%e\n%f" % (num,num,num)
1.23457e+009
1.234568e+009
1234567898.234568

In [3]: num = 1234.456789

In [4]: print "%g\n%e\n%f" % (num,num,num)
1234.46
1.234457e+003
1234.456789

So I'm wondering if the docs are wrong or the implementation is wrong or
there's something I don't understand?

Thanks,
Kent



More information about the Python-list mailing list