[Tutor] Scientific Notation + 18 digit precision

Hubert Fitch photonquark at comcast.net
Sun Nov 27 01:53:20 CET 2005


I am using Python 2.3 under Windows XP Home edition

I am too old to learn all the tricks of Python, and my main purpose is for Physics calculations

I do have some questions, but perhaps my useage of Python as an Algebraic Calculator will be interesting to someone?

Python is the best precision Algebraic Calculator solution that I have found so far. I like the large global space for variables.

Using the Idle GUI to run .py modules, I can then use direct mode calculations in the Python shell, after I have  defined a large list of variables by global assignment statements.

For my purposes, this requires about 7 .py definition modules, which all accumulate these assignments as keys and data, within the scope of the calling Python script. The most important module is the first one which enters a lot of physical constants.

I am not ready to begin any new module or calculation unless all the needed physical constants and derived variables are present within the scope of my calling program. 

Example of a Calling Script .py file ("NoPrintParameters.py"):

The purpose of this file is to enter Physical data variables and values, and assignment formulas that define new variables, (without printing anything to the Shell Screen, unless I decide to do so in the following Display Section.

To accomplish this, I have two similar function modules that I call: 
A Display Module Function, and a RunModule Function.

(The Display Function was written for me by the head of our Physics Deparment, and I adapted it to create the Run Module Function)

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.

(After All this data is on screen I can "Save Copy As" to RTF file format, which prreserves the Shell window formatted data. (Saving in Word document files or Text files will not preserve the formatted data).

The Run Module Function exectues all assignments, but no display is produced in the Shell screen.
This is necessary to get all assignments within global memory.

(As each new Python module was created, and ran for the first time, I always hoped to see only the Idle GUI blue screen characters, but usually I get some undefined variable errors. It is fun to finally get all of these errors resolved! There is so much going on, as the modules run, and I am always amazed that they really work!))

#
#       IMPORT  DISPLAY FUNCTION
from displayPyFile import DisplayPyFile
#
#       IMPORT  RUN MODULE FUNCTION (No Display)
from runPyModule import RunModule 
#
# ---------------------------------------------------
# Load and Run Files (No Display)
# ---------------------------------------------------# 
#   RUN     Main DEFINITION FILE   (NO DISPLAY) 
RunModule ('pythondef.py',globals() ) 
#   RUN     Other DEFINITION FILEs (NO DISPLAY) 
RunModule ('R1Parameters.py',globals() )
RunModule ('R0Parameters.py',globals() )
RunModule ('R10Parameters.py',globals() )
RunModule ('R2Parameters.py',globals() )
RunModule ('R3Parameters.py',globals() )
RunModule ('deBroglieRelativistic.py',globals() )
#
# ------------------------------------------------------
#       DISPLAY LINES in:
# -----------------------------------------------------
#DisplayPyFile ('pythondef.py',globals() ) 
#DisplayPyFile ('R1Parameters.py',globals() )
#DisplayPyFile ('R0Parameters.py',globals() )
#DisplayPyFile ('R10Parameters.py',globals() )
#DisplayPyFile ('R2Parameters.py',globals() )
#DisplayPyFile ('R3Parameters.py',globals() )
#
#DisplayPyFile ('Section1.py',globals() )
#DisplayPyFile ('ClassicalSelfForces.py',globals() )
#DisplayPyFile ('Definitions.py',globals() )
#
DisplayPyFile ('Topics.py',globals() )
#DisplayPyFile ('deBroglieRelativistic.py',globals() )
#
#

As you can see, I can display any (or all) modules by deleting the # comment character,  however in this example, the only file that will produce screen output is "Topics.py" 
I could remove all the # characters, to display all .py file definitions, but that would be too much data to see at once.

The main point of all the defintiion files is to be able to make direct mode calculations in the Python Shell, and to be able to write shorter scripts for a series of calculations which use these definitions.

Many assignments are execution order sensitive. The next assignment formula depends on the previous assignments.


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).

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? 

Or called from a Python script file, when I already know in advance what Python will do?

Another perhaps related thing is the way exact numbers like light speed = C (or C/2) are presented (  C = 299792458 ).  

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.

I wonder if this is caused by the GUI IDLE interpreter? Does the interpreter check all calculation results to see if they have been converted to scientific format?

Thank you for any help.

photonquark


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051126/d8cb789e/attachment.html


More information about the Tutor mailing list