Dumb python questions

Chris Barker chrishbarker at home.net
Wed Aug 15 18:12:31 EDT 2001


Paul Rubin wrote:

> Oh cool.  Thanks.  Btw, is there any reason for the apparently
> unnecessary complex contagion as soon as you touch cmath?  For
> example, cmath.sqrt(1) is 1+0j instead of 1.0.

This is pretty much a function of Python's current numeric types
approach: types are automatically coerced alot, but they remain
distinct. This may change in the future in a way that could fix your
example. See:

http://python.sourceforge.net/peps/pep-0228.html

for the PEP (Python enhancement Proposal).

It works the way it does now because the cmath routimes are designed for
working woith complex numbers, so they always return complex numbers.
I'm poretty sure that the inout values are coerced to complex, and then
operated on, returning a complex result. You really would't want all
complex numbers whose imaginary component happed to be zero to be
coerced back to a float, or this would crash:

z2 = cmath.sqrt(z).imag

as a float does not have the imag attribute. If Python's nuber types are
unified, this kind of problem would disappear.

In general, you use cmath because you want complex numbers, so it makes
sense for it to return complex numbers. When I'm doing totally symbolic
math, I still think of a complex number as  a complex number, even if
the imaginary component is zero. What would you want: cmath.sqrt(1+0j)
to return?

-Chris




-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list