<span class="gmail_quote"></span>
<div style="DIRECTION: ltr"><br>
<div></div>
<div style="DIRECTION: ltr"><span class="q">
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">> >>> print "%14.3g\n%14.3e\n%14.3f" % (bignum,bignum,bignum)<br>> 1.23e+009<br>
> 1.235e+009<br>> 1234567898.235<br>><br>> But in practice, we do not know how big is the data in advance when<br>> doing scientific computation in a compiled program.<br><br>That is irrelevant, the format you display the data in has
<br>no bearing on its precision, the display format is purely to<br>make it more readable.</blockquote>
<div> </div>
<div> </div></span></div>
<div style="DIRECTION: ltr">
<div>Another interesting one:</div>
<div> </div>
<div>>>> a = 123456.78<br>>>> print "%g\n%e\n%f" % (a,a,a)<br>123457<br>1.234568e+005<br>123456.780000<br>>>></div>
<div> </div>
<div>Float number loses digits and becomes integer in the output ?</div>
<div> </div>
<div>Can we override the %g in python, or adding in some other format identifier so that print statement with custom format string still works ?</div>
<div> </div>
<div>Here if I want to dynamically output floating data based on the number of significant digits (SD) from an input's: </div>
<div> </div>
<div>- If SD > m, truncate (or round it) in the output</div>
<div>- If SD < m, just directly output the number (without padding 0 to the end).</div>
<div>
<div>- 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). </div> </div>
<div>e.g, if SD = 3:</div>
<div> - for input = 100.0, the output is just 100.0 (not 100.00000..), since 100.0 = 1.0 * 10e2 (2<3)</div>
<div> - for input = 0.01, the output is just 0.01, since 0.01 = 1.0 * 10e-2 (2<3)</div>
<div> - for input with SD > 3, truncate it to output.</div>
<div> </div>
<div>SD can be set to some number (e.g. 6) in the program.</div>
<div> </div>
<div>Thanks.</div> </div></div>