[Tutor] Scientific Notation + 18 digit precision

Chris or Leslie Smith smiles at worksmail.net
Sun Nov 27 05:20:25 CET 2005


| The display function operates on each line in the .py file and
| provides 4 formatted columns for: 
| Variable Name, Data (18 digits ), Assignment Formula, and Comments.
| 

There are a couple of recipes at ASPN that might be useful with table generation:
 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/267662
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302380

[x]
| 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). 
| 

Apparently python doesn't agree ;-) Part of the answer has to do with how you show the result and part has to do with the size of the number. There are two representations of the number that are possible, str() and repr(). Here's a sample of both using your number:

######
>>> n=1209755258303.6067
>>> print n
1.2097552583e+012
>>> print str(n)
1.2097552583e+012
>>> print repr(n)
1209755258303.6067
>>> n
1209755258303.6067
>>> n=n*10**5
>>> n
1.2097552583036066e+017
######

As you can see, even the repr() form of the number eventually is too large to display in non-exponential form..  How were you displaying the value to the screen?


| I must then count about 7 or about 15 digits to detrmine how many
| places to the left to manuually move the  decimal point, and manually
| add the e+012. Why does Python sometimes do this?  
| 
| 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?   

If you check the python language reference section on "String Formatting Operations" that will show you that you can use a format to create the desired number representation. If you want exponential, the %e is the format you want:

######
>>> for i in range(1,17,3):
...  fmt='%%.%ie' % i
...  print fmt,'\t', fmt % n
... 
%.1e  1.2e+012
%.4e  1.2098e+012
%.7e  1.2097553e+012
%.10e  1.2097552583e+012
%.13e  1.2097552583036e+012
%.16e  1.2097552583036067e+012
######

So if you want 17 places after the decimal try

print "%.17e"  %  n

| Another perhaps related thing is the way exact numbers like light
| speed = C (or C/2) are presented (  C = 299792458 ). 
|
It's not clear what the problem is here. Can you clarify?
 
| 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.   

Watch out for the truncated division (presently the default for python). There's a nice write up at

http://www.ferg.org/projects/python_gotchas.html

(see section 3).  Either convert one of the numbers to float or import the future behavior as described in the link above.

######
>>> 2/3
0
>>> float(2)/3
0.66666666666666663
>>> from __future__ import division
>>> 2/3
0.66666666666666663
>>> 2//3  #with the future behavior in effect, the truncated division is obtained with //
0
######

/c


More information about the Tutor mailing list