-
-
Notifications
You must be signed in to change notification settings - Fork 29.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addition/subtraction clear sign from signed 0j #107854
Comments
Please find something in the docs that says the above is a bug. AFAIK, a |
@terryjreedy, probably you are right: the docs says there are no complex literals (complex numbers can be formed by adding a real number and an imaginary number), so the mechanics of current behaviour is transparent. On another hand:
|
To the originally reported issue: indeed this isn't a bug, and everything's working as designed here. As @terryjreedy says, addition simply adds the real parts and imaginary parts independently of each other, and addition between a On the On changing the language to add a complex literal: that's something that would need to be discussed at discuss.python.org first. |
@mdickinson, all linked issues are closed as well, while problem seems to be valid...
Will you +1 on this idea? BTW, there is a different option (probably it's too weird to be considered before): diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 0e96f54584..e362982719 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -473,6 +473,9 @@ complex_sub(PyObject *v, PyObject *w)
Py_complex a, b;
TO_COMPLEX(v, a);
TO_COMPLEX(w, b);
+ if (!PyComplex_CheckExact(v)) {
+ a.imag = copysign(0.0, -b.imag);
+ }
result = _Py_c_diff(a, b);
return PyComplex_FromCComplex(result);
} then >>> 1-0j
(1-0j)
>>> complex(1.0,-0.0)
(1-0j) On cons: this will break current |
Indeed they are! That's because (a) there's no bug here, and (b) no-one has proposed a behaviour change that's been accepted as both an improvement on the status quo and worth the cost of migration.
By "problem" here, I guess you're referring to the fact that
No idea: the simple-looking statement "add a complex literal to Python" hides a wealth of details that would have to be spelled out in any proposal. That's one reason that the discussion would be better over on https://discuss.python.org, so that those details and their consequences can be hashed out. But FWIW, I'd have a hard time supporting a proposal that broke referential transparency to give |
Of course.
Hardly it's the case here. Simplest solution is @mdickinson, what do you think if we restrict the
The docs seems to be more strict about this: "this function makes an attempt to return a string that would yield an object with the same value when passed to eval(); otherwise, the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information" (c) https://docs.python.org/3/library/functions.html#repr (There are round brackets, not angle...) |
Meh. I'm not excited by the idea - the The We've tinkered with the complex representation several times (mostly in Python 2.x); every time we did that it caused some disruption to existing code and third-party packages. I think what we have right now is a reasonable compromise between the various constraints (with backwards compatibility being one of those constraints), and I'm honestly not enthusiastic about making any further changes at this point; I think the time for that has passed. Any change at this point would need to demonstrate significant added value to compensate for the disruption.
That's now how I read those docs: I think the sentence you quote is descriptive rather than prescriptive (it starts with "For many types, ..."). And the doc statement is true, but If you want to take this further, please do start a discussion on discuss.python.org. As I think I've made clear, I'm opposed to making any changes to what we currently have, but if there's a strong consensus for changing the complex repr then obviously we should rethink. We're not going to find that consensus by discussing on a closed issue that no-one besides the two of us is reading, though. |
JFR: https://discuss.python.org/t/33433 PS:
Sorry, I've failed to find remnants of this in the mail lists (at least something, that will worth mentioning in the post). |
Complex numbers with signed zero as an imaginary part could be created from a string or with two-argument form of the complex() constructor:
It's also possible to create pure imaginary numbers with signed zero:
But it's impossible to create a complex number with signed zero as an imaginary part if addition/subtraction is used (i.e. adding a real number and an imaginary number):
The text was updated successfully, but these errors were encountered: