[Tutor] Meaning of %g ?
Sean Lee
sean.x.lee at gmail.com
Mon Apr 17 10:28:40 CEST 2006
> > >>> print "%14.3g\n%14.3e\n%14.3f" % (bignum,bignum,bignum)
> > 1.23e+009
> > 1.235e+009
> > 1234567898.235
> >
> > But in practice, we do not know how big is the data in advance when
> > doing scientific computation in a compiled program.
>
> That is irrelevant, the format you display the data in has
> no bearing on its precision, the display format is purely to
> make it more readable.
Another interesting one:
>>> 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 ?
Can we override the %g in python, or adding in some other format identifier
so that print statement with custom format string still works ?
Here if I want to dynamically output floating data based on the number of
significant digits (SD) from an input's:
- If SD > m, truncate (or round it) in the output
- If SD < m, just directly output the number (without padding 0 to the
end).
- In either of above cases, similar to %g, if %e is necessary, just output
%e format as result (also truncate the data based on SD in the %e format if
necessary).
e.g, if SD = 3:
- for input = 100.0, the output is just 100.0 (not 100.00000..), since
100.0 = 1.0 * 10e2 (2<3)
- for input = 0.01, the output is just 0.01, since 0.01 = 1.0 * 10e-2
(2<3)
- for input with SD > 3, truncate it to output.
SD can be set to some number (e.g. 6) in the program.
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060417/cd67759a/attachment-0001.html
More information about the Tutor
mailing list