[Edu-sig] using Python as a calculator

kirby urner kirby.urner at gmail.com
Sun Apr 11 00:08:18 CEST 2010


On Fri, Apr 9, 2010 at 9:41 AM, Edward Cherlin <echerlin at gmail.com> wrote:

> [sigh]
>
> Do math tables in a math array language.
>
> degrees =. i. 91  NB. 0..90
>
> radians =. degrees * o. % 180
>
> table =. |: degrees, 1 2 3 o./ radians
>
> where
>
> =. is assignment
> i. creates a list of consecutive numbers starting at 0.
> NB. is the comment marker
> o. x is pi times x
> % x is reciprocal of x, so o. % 180 is pi/180
> |: is transpose
> , appends an array to another. It turns a list into a table in order
> to match dimensions.
> 1 2 3 o. x gives sine, cosine, tangent of x
> / creates a table with the given function (o.) applied to two list
> arguments
>
>
Thanks Ed.  Nostalgia trip.

Yeah, my first reaction was similar to Christians:  if we need to learn a
whole new language to do a trig table, that's hardly productive-seeming.
 How to use the tools we already know?

And yet I've suggested a minimum of two languages, even if foreground
emphasis is given to just one.  The other might be purposely off-beat, like
COBOL or something.  REBOL anyone?  Or it might be a closer relative to
Python, such as JavaScript (for which Alan Kay has a lot of respect).

NB. for nota bene as I recall.  I always treasure the Italian influence.
 APL was (is) like Greek.

Below is the source for the trig table generator, a snap shot.  The inner
circle on this one, which includes Chairman Steve, is debating whether
TypeError should really be caught.

On the "aff side" (as debaters say, when arguing in the affirmative), a
capricious, not necessarily malicious user might feed in a filename of like
3, or type([ ]).  That's not a filename at all, so catch it with TypeError.


On the "neg side", there's a school of thought which says exceptions are
about catching "honest mistakes" i.e. the exception suite is showing what
one might legitimately expect as an error:  in this case an IOError because
the file cannot be created (e.g. is write-protected, is specified in a
non-existent directory or something of that nature).

I forget how J handles exceptions of this nature (wrong path,
write-protected file) -- I seem to recall a bunch of system call features,
but it has been some years...

Kirby

===

def trigtable(therange, fileout = None):
    """
    Print a trig table, to stdout or to a file
    http://mail.python.org/pipermail/edu-sig/2010-April/009890.html

    Rewrite with "with statement"?
    """
    if fileout:
        try:
            thefile = open(fileout, 'w')  #lets hope for a legal filename
(optional path part)
        except (TypeError, IOError) as exc:
            print("Unable to open file: {0}".format(fileout))
            raise
    else:
        thefile = sys.stdout

    for row in therange:
        theta = radians(row)
        print("{0:>5g}      {1:.9f}    {2:.9f}    {3:e}".format(
                row,cos(theta),sin(theta),tan(theta)),file= thefile)
    if fileout:
        thefile.close()


> The result is a 91 row, 4 column table of angles and trig function values.
>
> --
> Edward Mokurai (默雷/धर्ममेघशब्दगर्ज/دھرممیگھشبدگر ج) Cherlin
> Silent Thunder is my name, and Children are my nation.
> The Cosmos is my dwelling place, the Truth my destination.
> http://www.earthtreasury.org/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20100410/9455671e/attachment-0001.html>


More information about the Edu-sig mailing list