473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1791
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Edwin Young | last post by:
Hi, I think there might (emphasize might) be a minor bug in Python's cmath library. Basically, Python says cmath.asin(0) = 0-0j (+0 real part, -0 imaginary part). I think it should be 0+0j...
10
by: pauldepstein | last post by:
I am writing a program which will use the ceiling and floor of square roots. If a positive integer is an exact square (and is not overly huge), does sqrt return an integer-valued real? Or...
0
by: outofstep | last post by:
Hit me up on AIM/YIM (oipaloi). I'm not a code monkey and would like to get some advice. Or hit up this thread right here. I'm using DEV C++ as my compiler. I'm trying to calculate...
0
by: Kszu | last post by:
Hi. I need a little help with cmath.h I wan't to make a dComplex number from 2 doubles Here are some lines from my program: #include <vcl.h> #pragma hdrstop #include <iostream.h> #include...
11
by: The Cool Giraffe | last post by:
I have trouble making the following line compile. int a = round (2.6); I have, of course, included <cmathand i can go flooring and ceiling with no problems. However, the rounding seems...
16
by: giaro | last post by:
Hi, I'm doing some maths and I've just found that the standard library does not provide a standard PI value. In older post I see everyone says you must define it by yourself, but as some of the...
4
by: Peng Yu | last post by:
Hi, I need to look up the constants defined in cmath. Although I could read math.h directly, I'm wondering if there is a man page for it on linux system? Thanks, Peng
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.