This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Exception message in int() when trying to convert a complex
Type: behavior Stage:
Components: Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: aletornw, georg.brandl, mark.dickinson
Priority: normal Keywords:

Created on 2009-05-17 03:18 by aletornw, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg87955 - (view) Author: (aletornw) Date: 2009-05-17 03:18
Hi, I noticed that when trying to convert with the int() function a
complex number the exception error says "use int(abs(z))". I think that
insted it should say "use int(z.real)" because if I want to convert
1j**2 to int using the abs method, the result is 1 positive, when it
should be -1. With my method it works fine.
msg87974 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-05-17 09:21
That no unambiguous conversion between complex and int is defined is
exactly the reason for this error message.  You could want the absolute
value, the real part, the imaginary part, or even the polar angle...

int(abs(z)) works as intended, giving you the absolute value of the
complex number, which is 1 for 1j**2.
msg87976 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-05-17 09:49
I always found the "use int(abs(z))" part of that message odd,
as well.  As Georg points out, there are many possible ways that
one might want to convert complex to int;  it seems strange to
give advice for one particular one when that may well not match
what the user wanted.

I'd suggest just dropping the "use int(abs(z))" from the error message.
I think it's more likely to be confusing than helpful.

The same applies to float(complex).
msg87984 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-05-17 10:49
Removed "; use int(abs(z))" from the error message (and the
corresponding pieces from the error messages for long(z)
and float(z)) in r72718, r72719, r72720, r72722.

(Georg agreed to this change in a brief discussion on IRC.)
History
Date User Action Args
2022-04-11 14:56:49adminsetgithub: 50294
2009-05-17 10:49:25mark.dickinsonsetresolution: not a bug -> fixed
messages: + msg87984
2009-05-17 09:49:29mark.dickinsonsetnosy: + mark.dickinson
messages: + msg87976
2009-05-17 09:21:05georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg87974

resolution: not a bug
2009-05-17 03:18:00aletornwcreate