[Tutor] Scientific Notation + 18 Digit Pecision

Hubert Fitch photonquark at comcast.net
Wed Nov 30 17:22:34 CET 2005


Hi Danny!  (Python 2.3 IDLE GUI  in WindowsXP Home edition)
Thank you and the others who have sent replies!

Attached to this file is a Rich Text File using "Save Copy As" from the Python Shell display. This will give an example of the desired formatted output. Some displayed lines are long, so I use legal size paper and landscape mode for printing this RTF file.

Methods for turning on true division? 

A. Install the new Python, when it is available.

When will this be available?

B.  from__future__import division

This will work properly in an IDLE Python Shell, if I type it in, but not when it is present in .py modules.  I see that it was read from the .py file by the python shell, but the behaviour in the shell still is not changed from integer division to normal division. (I put the statement at the beginning of my .py modules.)

C. -Qnew

I did not understand how to get and install Qnew, so I searched on the information below, and got a lot of results (1100), that confused me further. 

D. customized parser and evaluator

This approach might be the best, but before I consider it, I must get the two affected fuunctions working. I saved the function modules that you refactored for me, into separate files, but should probably put them all into one file later.

1. Extract Equation    
2. EvaluateEquation  
3. PrintEquationAndValue
4. DisplayPyFile
and my
5. Calling Program Module

When I ran the caling module, (and got the indentation right in the function modues) all modules appeared to work except for the PrintEquationAnd Value Function which gives a syntax error for "if value:" with an arrow pointing to value. Reproduced below are the four function modules. and the calling module, and included is the original display module.
# printEquation.py

############################################
def PrintEquationAndValue(equation, value):
 """Prints out the equation and its value."""
if value:
      name, assignment, comment = equation
      print "%10s = %18.15e (%s)\t[%s]" % (name, value, assignment, comment)
else:
     print "%s not defined. %s" % (name, assignment)
############################################


# extractEquaiton.py

def ExtractEquation(line):
 """Either returns a 3-tuple (name, assignment, comment) from the line,
 or None if we can't extract a definition from the line."""
 line = line.strip()
 if not line:
 return None
 leftAndRight = line.split('#',2)
 comment = ''
 if len(leftAndRight) > 1:
 comment = leftAndRight[1]
 assignment = leftAndRight[0]
 leftAndRight = assignment.split('=')
 if len(leftAndRight) == 2:
 name = leftAndRight[0].strip()
 return (name, assignment, comment)
 return None
    

# evaluateEquation.py
#
##############################################
def EvaluateEquation(equation, context):
  """Evaluates the equation in a given context. If we can't get a value, returns None."""
  assert equation != None
   name, assignment, comment = equation
   exec(assignment, context)
   return context.get(name, None)
##############################################



# new2DisplayPyFile.py
#
#############################################
def NewDisplayPyFile(fname, context=None):
 if context is None:
  context = globals()
  f = open(fname)
 lines = f.readlines()
  f.close()
for line in lines:
 equation = ExtractEquation(line)
 if equation:
  value = EvaluateEquation(equation, context)
  printEquationAndValue(equation, value)
 else:
    print line
##############################################


Original display function module. (Notice that I added the from future statement)

from __future__ import division
def DisplayPyFile(fname, context=None):

    if context is None:
        context = globals()
    f = open(fname)
    lines = f.readlines()
    f.close()

    for line in lines:
        line = line.strip()
        if not line:
            continue
        leftAndRight = line.split('#',2)
        comment = ''
        if len(leftAndRight)>1:
            comment = leftAndRight[1]

        assignment = leftAndRight[0]
        leftAndRight = assignment.split('=')
        if len(leftAndRight) == 2:
            name = leftAndRight[0].strip()
            exec(assignment, context)
            if context.get(name,None) is not None:
                value = context.get(name,'???')
                print "%10s  =  %18.15e (%s)\t[%s]" % (name, value, assignment, comment)
            else:
                print "%s not defined. %s" % (name, assignment)
        else:
            print line
              


--------------------------------------------------------------------------------------------
>There are a few ways of fixing this. The easiest is to start Python up
with the '-Qnew' command line option, which turns on true division
globally:

mumak:~ dyoo$ python -Qnew
Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin
Type "help", "copyright", "credits" or "license" for more information

> I'd recommend this   approach: it means you won't have to change much. Unfortunately, this means that you'll have to remember to use '-Qnew' all the time, and that's
bug-prone.

An alternative way is to fix the bug is to modify extractEquation and
evaluateEquation so that it doesn't depend on Python's evaluation
functions. To an experienced programmer, I'd recommend that approach, to
replace the call to 'exec' with a customized parser and evaluator: we have
more control over the arithmetic.

If you really need something like this, one of us can probably cook up
such an evaluator for you.
---------------------------------------------------------------------------------------------------

Thank you for your help!

Hubert


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051130/bcc8f456/attachment-0001.htm
-------------- next part --------------
A non-text attachment was scrubbed...
Name: R1SaveCopyAs.rtf
Type: application/msword
Size: 7721 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20051130/bcc8f456/R1SaveCopyAs-0001.dot


More information about the Tutor mailing list