Skip to content

gh-146151: memoryview supports 'F' and 'D' format types (complex)#146241

Merged
vstinner merged 5 commits intopython:mainfrom
skirpichev:support-complex-memoryview/146151
Mar 26, 2026
Merged

gh-146151: memoryview supports 'F' and 'D' format types (complex)#146241
vstinner merged 5 commits intopython:mainfrom
skirpichev:support-complex-memoryview/146151

Conversation

@skirpichev
Copy link
Member

@skirpichev skirpichev commented Mar 21, 2026

@skirpichev skirpichev requested a review from vstinner March 26, 2026 00:11
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM. The implementation looks complete and well tested.

memcpy(ptr, &x, sizeof(x));
}
else {
float x[2] = {(float)c.real, (float)c.imag};
Copy link
Member

Choose a reason for hiding this comment

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

I wasn't sure of the behavior on underflow or overflow of the double to float cast, so I tested:

>>> import struct, sys
>>> data=struct.pack('F', 0.0) * 4
>>> m=memoryview(bytearray(data)).cast('F')
>>> m[0]=math.nextafter(0, 1)
>>> m[1]=sys.float_info.min
>>> m[2]=sys.float_info.max
>>> m[3]=float("nan")
>>> m.tolist()
[0j, 0j, (inf+0j), (nan+0j)]

I got the values that I expected. I suppose that these conversions are well specified by IEEE 754.

Copy link
Member Author

Choose a reason for hiding this comment

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

The behavior here is same as for 'f' format, just component-wise, see PACK_SINGLE macro.

…e-146151.4-lhim.rst

Co-authored-by: Victor Stinner <vstinner@python.org>
@vstinner vstinner merged commit c68a194 into python:main Mar 26, 2026
51 checks passed
@vstinner
Copy link
Member

Merged, thanks for your contribution.

@skirpichev skirpichev deleted the support-complex-memoryview/146151 branch March 26, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants