[Tutor] specifying precision with scientific notation

Andre' Walker-Loud walksloud at gmail.com
Tue Oct 5 21:52:01 CEST 2010


Hi All,

I want to print scientific numbers with a specified number of decimal places.  However, I want the number printed to be dynamically determined by the data.  Example:

> a = 0.00762921383941
> ea = 0.000830132912068
> a / ea
9.190352205653852

By default, I will print the uncertainty ("ea") with two significant digits.  In this example, the central value is about 10 times larger than the uncertainty, so I want to print it with 3 significant figures.  So I want to do something like

> p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the scientific notation (is there a better way?)
> if p >= 0:
>	print('%.' + str(int(2+p)) +'e +- %.1e' % (a, ea))
> else:
	print('%.2e +- %.1e' % (a, ea))

(desired output): 7.63e-03 +- 8.3e-04

but this fails.  And I haven't figured out how to get this to work.  Seems like it should be simple.

Any help?


Thanks,

Andre



More information about the Tutor mailing list