Kent Johnson wrote:
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?
format != result. the phrase "same format" refers to decimal format vs.
exponential format (see the descriptions of %e and %f in the same table),
not the contents of the output string.
(both formats use the same precision value, but %g interprets it as number
of significant digits, while %f and %e interprets it as number of decimals).
the C standard uses the word "style" instead:
e) The double argument shall be converted in the style "[-]d.ddde±dd",
where there is one digit before the radix character (which is non-zero
if the argument is non-zero) and the number of digits after it is equal
to the precision /.../
f) The double argument shall be converted to decimal notation in the
style "[-]ddd.ddd", where the number of digits after the radix character
is equal to the precision specification /.../
g) The double argument shall be converted in the style f or e (or in
the style F or E in the case of a G conversion specifier), with the precision
specifying the number of significant digits /.../
</F>