math.nroot [was Re: A brief question.]

Tom Anderson twic at urchin.earth.li
Mon Jul 4 10:35:56 EDT 2005


On Sun, 3 Jul 2005, Tim Peters wrote:

> [Tom Anderson]
>> So, is there a way of generating and testing for infinities and NaNs
>> that's portable across platforms and versions of python?
>
> Not that I know of, and certainly no simple way.
>
>> If not, could we perhaps have some constants in the math module for them?
>
> See PEP 754 for this.

Looks perfect.

>>> Read the manual for the precedence rules.  -x**y groups as -(x**y). 
>>> -1.0 is the correct answer.  If you intended (-x)**y, then you need to 
>>> insert parentheses to force that order.
>
>> So i see. Any idea why that precedence order was chosen? It goes against
>> conventional mathematical notation, as well as established practice in
>> other languages.
>
> Eh?  For example, Fortran and Macsyma also give exponentiation higher 
> precedence than unary minus.  From my POV, Python's choice here was 
> thoroughly conventional.

I guess you're right. What really threw me is that in all the other 
languages i know (which is not many, i admit), -1 is a literal -1, not a 
literal 1 plus a unary minus.

>> Also, would it be a good idea for (-1.0) ** 0.5 to evaluate to 1.0j? It
>> seems a shame to have complex numbers in the language and then miss this
>> opportunity to use them!
>
> It's generally true in Python that complex numbers are output only if 
> complex numbers are input or you explicitly use a function from the 
> cmath module. [...] The presumption is that a complex result is more 
> likely the result of program error than intent for most applications. 
> The relative handful of programmers who expect complex results can get 
> them easily, though.

A reasonable presumption.

I always got really wound up that the calculator i had at school had this 
behaviour too, *even in complex number mode*! Come to think of it, i don't 
think it could do roots of imaginary numbers at all. However, python is 
not a calculator.

tom

-- 
A military-industrial illusion of democracy



More information about the Python-list mailing list