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

Tim Peters tim.peters at gmail.com
Sun Jul 3 23:04:20 CEST 2005


...

[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.

...

>> 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.

> 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.  For example,

>>> import math, cmath
>>> math.sqrt(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: math domain error
>>> cmath.sqrt(-1)
1j

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.



More information about the Python-list mailing list