ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
[Tutor] Meaning of %g ?
by Sean Lee other posts by this author
Apr 17 2006 1:29AM messages near this date
Re: [Tutor] functions in Python | [Tutor] Stack Diagrams
>  > >>> 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.

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved