470,643 Members | 1,551 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,643 developers. It's quick & easy.

cmath, __float__ and __complex__

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.
>>from cmath import sqrt
class NumericType1(object):
.... def __float__(self): return 10.0
....
>>class NumericType2(object):
.... def __complex__(self): return 10+0j
....
>>print sqrt(NumericType1())
(3.16227766017+0j)
>>print sqrt(NumericType2())
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

Feb 16 '07 #1
1 1697
Mark Dickinson wrote:
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

Feb 16 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Edwin Young | last post: by
10 posts views Thread by pauldepstein | last post: by
reply views Thread by Kszu | last post: by
11 posts views Thread by The Cool Giraffe | last post: by
16 posts views Thread by giaro | last post: by
4 posts views Thread by Peng Yu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.