Skip to content

doc: Improve C API manual for complex number objects - #156914

Open
hpkfft wants to merge 1 commit into
python:mainfrom
hpkfft:doc-complex
Open

doc: Improve C API manual for complex number objects#156914
hpkfft wants to merge 1 commit into
python:mainfrom
hpkfft:doc-complex

Conversation

@hpkfft

@hpkfft hpkfft commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

The actual change is to note that

>>> (0j)**(0j)
(1+0j)

The other minor wording changes, I believe, increase consistency. Feel free to change it if you feel otherwise.

CC: @skirpichev @eendebakpt @serhiy-storchaka

@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34381966 | 📁 Comparing 27d9ed2 against main (0625799)

  🔍 Preview build  

1 file changed
± c-api/complex.html

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest looks ok for me.

Comment thread Doc/c-api/complex.rst
this function returns zero and sets :c:data:`errno` to :c:macro:`!EDOM`.

Set :c:data:`errno` to :c:macro:`!ERANGE` on overflows.
On overflow, this function sets :c:data:`errno` to :c:macro:`!ERANGE`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that old sentence was wrong or grammatically invalid. Lets keep it as is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old sentence is imperative. It's giving an order to the reader to do something. But, it's not intended for the reader to set anything. It's the function that sets errno. The sentence above is already correct. The mismatch between the two is jarring to a native English speaker. Let's make the change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old sentence is imperative. It's giving an order to the reader to do something.

Ok, up to other reviewers. This still looks as a code churn for me.

And, perhaps, this open a Pandora box... Look on PyLong_AsInt64():

Set *value and return 0 on success. Set an exception and return -1 on error.

Comment thread Doc/c-api/complex.rst
Return the absolute value of the complex number *num*.

Set :c:data:`errno` to :c:macro:`!ERANGE` on overflows.
On overflow, this function sets :c:data:`errno` to :c:macro:`!ERANGE`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Comment thread Doc/c-api/complex.rst
Comment thread Doc/c-api/complex.rst
Comment on lines +185 to +187
If *exp* is zero, this function returns ``{1.0, 0.0}``; otherwise
if *num* is zero and *exp* is not a positive real number,
this function returns zero and sets :c:data:`errno` to :c:macro:`!EDOM`.

@skirpichev skirpichev Sep 4, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If *exp* is zero, this function returns ``{1.0, 0.0}``; otherwise
if *num* is zero and *exp* is not a positive real number,
this function returns zero and sets :c:data:`errno` to :c:macro:`!EDOM`.
If *num* is zero and *exp* is not a nonnegative real number,
this function returns zero and sets :c:data:`errno` to :c:macro:`!EDOM`.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good to document the return value in this case because I do not think it's documented in the C standard. MPC, Python, GNU libc, Intel libm, FreeBSD-15 all do as I've written in this PR. (Older versions of FreeBSD did not, but it was changed when my colleague reported it to them.) I think AMD's libm returns (0, 0).

Numeric Types footnote 5 states:

Python defines pow(0, 0) and 0 ** 0 to be 1, as is common for programming languages.

It's not something we'd want to change in the future.

The phrase "not nonnegative" is awkward. I prefer my wording.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it's documented in the C standard.

Then perhaps it's a good reason to not document this?

We also have a lot of other corner cases for complex numbers with special components: #60200 (comment)

But anyway, we don't develop deprecated stuff, that includes soft-deprecated API like _Py_c_pow().

The phrase "not nonnegative" is awkward.

Well, then up to other reviewers. It's precise, there is no double negation, if you are about.

Comment thread Doc/c-api/complex.rst

If *num* is null and *exp* is not a positive real number,
this method returns zero and sets :c:data:`errno` to :c:macro:`!EDOM`.
If *exp* is zero, this function returns ``{1.0, 0.0}``; otherwise

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you prefer the following suggestion?

Suggested change
If *exp* is zero, this function returns ``{1.0, 0.0}``; otherwise
If *num* is zero and *exp* is zero, this function returns ``{1.0, 0.0}``; otherwise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants