-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-127833: lexical analysis: Improve section on Numeric literals #134850
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
base: main
Are you sure you want to change the base?
Conversation
Instead, :ref:`complex numbers <typesnumeric>` can be written | ||
as adding the complex number's real part and imaginary part. | ||
|
||
.. productionlist:: python-grammar | ||
imagnumber: (`floatnumber` | `digitpart`) ("j" | "J") | ||
For example, the imaginary number 3.1+4\ *i* can be written as adding the | ||
real number 3.1 to the imaginary number 4\ *i*. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but this is a wrong picture.
The CPython has no pure-imaginary numbers and above decomposition is invalid in general. Simple counterexample:
>>> -0.0+0j
0j
>>> complex(-0.0, 0)
(-0+0j)
Previous description was valid: "An imaginary literal yields a complex number with a real part of 0.0."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key point is this is lexical analysis. Python indeed has no imaginary literals, but the tokeniser does -- 4+16j
is three tokens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think L1093 should say 'the complex number ...', though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but 16j
is not an imaginary number. It's a complex number with a real part +0.0
. And complex number in the Python currently can't be "written as adding the complex number's real part and imaginary part."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
16j
is not an imaginary number; but 16i is :)
I agree that this could be clearer. Will update. (Not sure when, as I recently volunteered to focus on f-strings first.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
16j is not an imaginary number; but 16i is :)
But you are talking not about mathematics, but Python's complex numbers.
Each section is expanded, and now progresses from simple cases and examples to more involved ones, ending with the formal grammar.
This uses meta-syntax that I want to add to the introduction in #134443. I'm marking it as draft until that's merged.
📚 Documentation preview 📚: https://cpython-previews--134850.org.readthedocs.build/