[Edu-sig] using Python as a calculator

kirby urner kirby.urner at gmail.com
Thu Apr 8 16:43:13 CEST 2010


I think Guido was wise to start his tutorial by showing how we
might use Python as a calculator.

We might assume many students in this day and age are quite
familiar with this device, and even if they're not, the text might
project one, show a picture on the screen, if what these things
used to look like (still do).

However, one thing calculators lack over the old wood pulp
textbooks are trig tables with multiple rows showing a lot of
data at the same time.  Their small "chat window" does not
permit much data to be seen at one time.

Back in the day, a student could run her finger down the
rows, as the number of angular degrees increase from
0 to 60 and onward to 90, perhaps all the way around to
360.

Going across the row, one would have sine and cosine,
perhaps tangent.  Having all the data visible at once, or spread
across a few pages, inspired some insights and understanding,
as one could see the trends in the numbers, plus these
"click stop" rows where the numbers would suddenly be
super easy, like 1/2 and 1/2 for both sine and cosine.

Calculators don't give us that kind of output, but earlier office
computing machines did have paper i/o, called a tape, usually
a scroll mounted on a spool and fed through a small printer.

As one added numbers, one printed to tape, perhaps a running
total.  The tape itself was a valuable item (especially once it
had the data on it).

Large computers came with line printers that hit on continuous
feed paper with holes along both sides, often with green and
white stripes.  I will not try to recapitulate the long history
of printing devices, except to point out that computers
inherited them while slide rules and calculators did not.

The equivalent in Python is stdout and/or some file in storage,
on the hard drive or memory stick.  The program output
shown below would be an example of this kind of i/o.

Notice that unless a file name is given (optional), the data
is to stdout.

I'm going to do a full 90 degrees, just to remind myself of
the patterns students got in the old days, before trig tables
were replaced with calculators, much as dial watches were
replaced with digital ones (not necessarily a smart move
in all cases).

>>> imp.reload(newprint)
<module 'newprint' from 'C:\Python26\lib\site-packages\newprint.py'>
>>> newprint.trigtable(range(91), "trigtable.txt")

The contents of trigtable.txt:

    0      1.000000000    0.000000000    0.000000e+00
    1      0.999847695    0.017452406    1.745506e-02
    2      0.999390827    0.034899497    3.492077e-02
    3      0.998629535    0.052335956    5.240778e-02
    4      0.997564050    0.069756474    6.992681e-02
    5      0.996194698    0.087155743    8.748866e-02
    6      0.994521895    0.104528463    1.051042e-01
    7      0.992546152    0.121869343    1.227846e-01
    8      0.990268069    0.139173101    1.405408e-01
    9      0.987688341    0.156434465    1.583844e-01
   10      0.984807753    0.173648178    1.763270e-01
   11      0.981627183    0.190808995    1.943803e-01
   12      0.978147601    0.207911691    2.125566e-01
   13      0.974370065    0.224951054    2.308682e-01
   14      0.970295726    0.241921896    2.493280e-01
   15      0.965925826    0.258819045    2.679492e-01
   16      0.961261696    0.275637356    2.867454e-01
   17      0.956304756    0.292371705    3.057307e-01
   18      0.951056516    0.309016994    3.249197e-01
   19      0.945518576    0.325568154    3.443276e-01
   20      0.939692621    0.342020143    3.639702e-01
   21      0.933580426    0.358367950    3.838640e-01
   22      0.927183855    0.374606593    4.040262e-01
   23      0.920504853    0.390731128    4.244748e-01
   24      0.913545458    0.406736643    4.452287e-01
   25      0.906307787    0.422618262    4.663077e-01
   26      0.898794046    0.438371147    4.877326e-01
   27      0.891006524    0.453990500    5.095254e-01
   28      0.882947593    0.469471563    5.317094e-01
   29      0.874619707    0.484809620    5.543091e-01
   30      0.866025404    0.500000000    5.773503e-01
   31      0.857167301    0.515038075    6.008606e-01
   32      0.848048096    0.529919264    6.248694e-01
   33      0.838670568    0.544639035    6.494076e-01
   34      0.829037573    0.559192903    6.745085e-01
   35      0.819152044    0.573576436    7.002075e-01
   36      0.809016994    0.587785252    7.265425e-01
   37      0.798635510    0.601815023    7.535541e-01
   38      0.788010754    0.615661475    7.812856e-01
   39      0.777145961    0.629320391    8.097840e-01
   40      0.766044443    0.642787610    8.390996e-01
   41      0.754709580    0.656059029    8.692867e-01
   42      0.743144825    0.669130606    9.004040e-01
   43      0.731353702    0.681998360    9.325151e-01
   44      0.719339800    0.694658370    9.656888e-01
   45      0.707106781    0.707106781    1.000000e+00
   46      0.694658370    0.719339800    1.035530e+00
   47      0.681998360    0.731353702    1.072369e+00
   48      0.669130606    0.743144825    1.110613e+00
   49      0.656059029    0.754709580    1.150368e+00
   50      0.642787610    0.766044443    1.191754e+00
   51      0.629320391    0.777145961    1.234897e+00
   52      0.615661475    0.788010754    1.279942e+00
   53      0.601815023    0.798635510    1.327045e+00
   54      0.587785252    0.809016994    1.376382e+00
   55      0.573576436    0.819152044    1.428148e+00
   56      0.559192903    0.829037573    1.482561e+00
   57      0.544639035    0.838670568    1.539865e+00
   58      0.529919264    0.848048096    1.600335e+00
   59      0.515038075    0.857167301    1.664279e+00
   60      0.500000000    0.866025404    1.732051e+00
   61      0.484809620    0.874619707    1.804048e+00
   62      0.469471563    0.882947593    1.880726e+00
   63      0.453990500    0.891006524    1.962611e+00
   64      0.438371147    0.898794046    2.050304e+00
   65      0.422618262    0.906307787    2.144507e+00
   66      0.406736643    0.913545458    2.246037e+00
   67      0.390731128    0.920504853    2.355852e+00
   68      0.374606593    0.927183855    2.475087e+00
   69      0.358367950    0.933580426    2.605089e+00
   70      0.342020143    0.939692621    2.747477e+00
   71      0.325568154    0.945518576    2.904211e+00
   72      0.309016994    0.951056516    3.077684e+00
   73      0.292371705    0.956304756    3.270853e+00
   74      0.275637356    0.961261696    3.487414e+00
   75      0.258819045    0.965925826    3.732051e+00
   76      0.241921896    0.970295726    4.010781e+00
   77      0.224951054    0.974370065    4.331476e+00
   78      0.207911691    0.978147601    4.704630e+00
   79      0.190808995    0.981627183    5.144554e+00
   80      0.173648178    0.984807753    5.671282e+00
   81      0.156434465    0.987688341    6.313752e+00
   82      0.139173101    0.990268069    7.115370e+00
   83      0.121869343    0.992546152    8.144346e+00
   84      0.104528463    0.994521895    9.514364e+00
   85      0.087155743    0.996194698    1.143005e+01
   86      0.069756474    0.997564050    1.430067e+01
   87      0.052335956    0.998629535    1.908114e+01
   88      0.034899497    0.999390827    2.863625e+01
   89      0.017452406    0.999847695    5.728996e+01
   90      0.000000000    1.000000000    1.633124e+16

Here's the print function I used to generate the above.

                print("{0:>5g}      {1:.9f}    {2:.9f}    {3:e}".format(
                            row,        cos(theta), sin(theta),tan(theta)),
                      end="\n", file= thefile)

My module starts with:

from __future__ import printfunction

which is why I get to use this in 2.6

So why use Python as a calculator again?  Because it's more like
an old office machine with a tape, and that restores some of what
was lost when lookup tables went out of style.  I should do log10
next, using range with a step or something....  Also, the trig tape
should probably be 0-360 but I didn't want to waste paper. :)

Kirby


More information about the Edu-sig mailing list