[Tutor] Scientific Notation + 18 digit precision
kent37 at tds.net
Sun Nov 27 03:16:16 CET 2005
Hubert Fitch wrote:
> Now for the questions:
> Most data and results are displayed properly formatted in scientific
> notation, but sometimes large numbers resulting from calculations are
> not converted to scientific notation.
> For example: mu0*I0/r0 = 1209755258303.6067 (should have
> been 1.2097552583036067e+012).
> Is there an easy way to convert these large numbers to the scientific
> notation format? Can we write a function call that I could use
> to convert the numbers to scientific notation whenever I need to do so
> from the Python shell in direct mode?
You can use string formatting to get closer control of the way numbers print. For example:
>>> x= 1209755258303.6067
>>> '%e' % x
See the docs for details:
> And sometimes I get an incorrect result for integer calculations. This
> does not happen very often, but it worries me that it may happen when I
> don't know about it. A calculation involving integers will sometimes
> give an unexpected zero result.
Do you know that integer division is truncated? For example
You can fix this by making sure one of the operands is a float:
>>> 3.0 / 4
Future versions of Python will do this for you. You can enable it by this import:
>>> from __future__ import division
More information about the Tutor