cmath, __float__ and __complex__

M

Mark Dickinson

I was a little surprised by the following behaviour:

Python 2.5 (r25, Oct 30 2006, 20:50:32)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information..... def __float__(self): return 10.0
........ def __complex__(self): return 10+0j
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a float is required

Having recently made the pleasant discovery that math.sqrt and friends
will happily accept any object with a working __float__ attribute, I
expected the same to be true with regards to cmath and __complex__.
The bit that I find really counter-intuitive is that the cmath
functions will use __float__ if available, but completely ignore
__complex__.

Does anyone know of a good reason for the above behaviour? Would a
patch to complexobject.c that `fixes' this be of any interest to
anyone but me? Or would it likely break something else?

Mark
 
Z

Ziga Seilnacht

Mark said:
Does anyone know of a good reason for the above behaviour? Would a
patch to complexobject.c that `fixes' this be of any interest to
anyone but me? Or would it likely break something else?

I think this is a bug in the PyComplex_AsCComplex function. To get
more feedback, submit your patch to the Python patch tracker:
http://sourceforge.net/patch/?group_id=5470

Patch submission guidelines can be found here:
http://www.python.org/dev/patches/

Ziga
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top