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: Allow rich comparisons for real-valued complex objects.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brandtbucher, eric.smith, mark.dickinson, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-02-26 08:04 by brandtbucher, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12048 closed brandtbucher, 2019-02-26 08:04
Messages (8)
msg336628 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2019-02-26 08:04
Currently, it isn't legal to perform <, >, <=, or >= rich comparisons on any complex objects, even though these operations are mathematically well-defined for real numbers.

The attached PR addresses this by defining rich comparisons for real-valued complex objects against subclasses of int and float, as well as for decimal.Decimal and fractions.Fraction types. They still raise TypeErrors when either of the operands has a nonzero imaginary part.
msg336629 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-02-26 08:21
I don't think we should do this. In numerical computation we should not depend on exact floating point values, because is affected by computation errors. It would be hard to debug the program when some complex numbers are orderable, but other are not, and the difference between formers and latters is insignificant.
msg336630 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-02-26 08:22
If you need to order real-valued complex object, convert them to float objects first.
msg336631 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2019-02-26 08:30
I don't really see, though, how comparing complex(42) == float(42) is any less dangerous than complex(42) <= float(42). It seems odd to me, personally, that real-valued complex objects are valid for *some* rich comparisons (but not others) when the math is unambiguous.
msg336632 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-02-26 08:43
-1 from me. The rules for when things are comparable or not should be kept simple.
msg336639 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-02-26 10:24
-1. We don't want to have objects that are orderable depending on their values. I can't think of anywhere else we do this.

It would be very easy to have a complex == 42+0.0000001j, after some calculation. This near-zero imaginary part would prevent it from being orderable, while if a similar calculation produced exactly 42+0j, then that instance would be orderable. An application relying on this would be a nightmare to write comprehensive tests for.

Whether something is orderable or not should depend solely on its type, not its value.
msg336678 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2019-02-26 14:37
> The rules for when things are comparable or not should be kept simple.

I think that the sort of user who uses complex numbers for their numerical calculations would still find this behavior "simple", but that may just be me.

> We don't want to have objects that are orderable depending on their values. I can't think of anywhere else we do this.

Well... tuples and lists behave this way. ;)

> An application relying on this would be a nightmare to write comprehensive tests for.

I'm not arguing that applications should rely on this behavior as core functionality, just that Python's more advanced math functionality deserves to be correct.

With that said, there are already so many weird limitations on complex numbers and floating-point values in general. I wouldn't expect users to treat comparing complex numbers any differently than they would treat comparing floats. Check check the .imag value in the former, check math.isnan in the latter.
msg336681 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-02-26 15:11
NaN and complex numbers are not orderable by definition. This is a feature, not a bug.
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80298
2019-02-26 15:11:40serhiy.storchakasetstatus: open -> closed
resolution: not a bug
messages: + msg336681

stage: patch review -> resolved
2019-02-26 15:07:49brandtbuchersettype: enhancement -> behavior
2019-02-26 14:37:55brandtbuchersetmessages: + msg336678
2019-02-26 10:24:03eric.smithsetnosy: + eric.smith
messages: + msg336639
2019-02-26 08:43:01mark.dickinsonsetmessages: + msg336632
2019-02-26 08:30:05brandtbuchersetmessages: + msg336631
2019-02-26 08:22:35serhiy.storchakasetmessages: + msg336630
2019-02-26 08:21:14serhiy.storchakasetnosy: + mark.dickinson, serhiy.storchaka
messages: + msg336629
2019-02-26 08:04:36brandtbuchersetkeywords: + patch
stage: patch review
pull_requests: + pull_request12073
2019-02-26 08:04:22brandtbuchercreate