Can global variable be passed into Python function?

Roy Smith roy at panix.com
Sat Mar 1 01:02:44 CET 2014


In article <0b414429-74ee-45dd-9465-c87e98c362f2 at googlegroups.com>,
 "Mark H. Harris" <harrismh777 at gmail.com> wrote:

> On Friday, February 28, 2014 3:03:25 PM UTC-6, Marko Rauhamaa wrote:
> > 
> > Marko
> 
>    ...  and between me and you, here is a snip from dmath.py from the atan(x) 
>    function:
> 
>     if (n**2 < D(1)):
>         a = __atan__(n)
>     elif (n == D(1)):
>         a = gpi/4
>     elif (n == D(-1)):
>         a = -(gpi/4)
>     elif (n < D(-1)):
>         a = __atan__Lt_neg1__(n)
>     else:
>         a = __atan__Gt_1__(n)
> 
>    This if--elif--else  is not only ugly, its just not readable either, and 
>    besides that, its not elegant, nor is it humanly helpful...   its does 
>    work though, and its absolutely necessary.   ugh.
> 
>    First, its not immediately clear what it does. Well, there isn't just one 
>    atan(x) routine,  there are at least four of them, depending on whether 
>    you're a purist, and they must be selected.

This kind of stuff is pretty common in numerical code.  Depending on the 
sign/magnitude/quadrant/whatever of the argument, you'll want to use one 
of several algorithms to minimize roundoff error, etc.

But, how would this be any nicer with switch?



More information about the Python-list mailing list