8000 gh-69639: add mixed-mode rules for complex arithmetic (C-like) by skirpichev · Pull Request #124829 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-69639: add mixed-mode rules for complex arithmetic (C-like) #124829

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

Merged
merged 29 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3980363
gh-69639: add mixed-mode rules for complex arithmetic (C-like)
skirpichev Apr 23, 2024
46521c3
address review:
skirpichev Oct 2, 2024
1323f4d
Update Objects/complexobject.c
skirpichev Oct 2, 2024
5021a9b
address review: -> real_to_double
skirpichev Oct 2, 2024
c7308ef
Add _Py_cd_* and _Py_dc_* functions
skirpichev Oct 2, 2024
ee1aa01
+ what's new
skirpichev Oct 2, 2024
da566dd
+ trying to document new coersion rules in the reference
skirpichev Oct 2, 2024
714b731
address review: expressions.rst
skirpichev Oct 3, 2024
7a04eb0
address review: reword what's new
skirpichev Oct 3, 2024
1d42c10
cleanup: just one macro
skirpichev Oct 4, 2024
d6e9d14
renamed c-api helpers: _dc_ -> _rc_ and _cd_ -> _cr_
skirpichev Oct 6, 2024
2db0072
1d42c10202 +1
skirpichev Oct 6, 2024
46bed69
+ tests for semi-private C-API
skirpichev Oct 6, 2024
d6e4504
Merge branch 'master' into complex-float-arith-69639
skirpichev Oct 6, 2024
cc298e5
Apply suggestions from code review
skirpichev Oct 8, 2024
ee22926
Apply suggestions from code review
skirpichev Oct 13, 2024
220c551
Apply suggestions from code review
skirpichev Oct 14, 2024
f068384
Apply suggestions from code review
skirpichev Oct 14, 2024
8cdd514
Merge branch 'main' into complex-float-arith-69639
skirpichev Oct 30, 2024
7fb1be1
+ move news
skirpichev Oct 30, 2024
badf492
address review:
skirpichev Oct 31, 2024
3ef5287
Merge branch 'master' into complex-float-arith-69639
skirpichev Oct 31, 2024
38762b3
address review: rename symbols in commentary to avoid clash with macro
skirpichev Oct 31, 2024
e685bd9
Update Objects/complexobject.c
skirpichev Oct 31, 2024
76bad15
Merge branch 'main' into complex-float-arith-69639
skirpichev Nov 19, 2024
de5f353
Merge branch 'master' into complex-float-arith-69639
skirpichev Nov 26, 2024
2b46a96
added sum() test
skirpichev Nov 26, 2024
759a6d0
Update Lib/test/test_builtin.py
serhiy-storchaka Nov 26, 2024
ed67dc2
I'LL NEVER USE WEB-EDITOR... I'LL NEVER USE WEB-EDITOR... I'LL NEVER …
skirpichev Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add _Py_cd_* and _Py_dc_* functions
  • Loading branch information
skirpichev committed Oct 2, 2024
commit c7308efca22497599bdee4382fc13946342b5e46
54 changes: 54 additions & 0 deletions Doc/c-api/complex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,36 @@ pointers. This is consistent throughout the API.
representation.


.. c:function:: Py_complex _Py_cd_sum(Py_complex left, double right)

Return the sum of complex and real number, using the C :c:type:`Py_complex`
representation.

.. versionadded:: 3.14


.. c:function:: Py_complex _Py_c_diff(Py_complex left, Py_complex right)

Return the difference between two complex numbers, using the C
:c:type:`Py_complex` representation.


.. c:function:: Py_complex _Py_dc_diff(double left, Py_complex right)

Return the difference of real and complex number, using the C
:c:type:`Py_complex` representation.

.. versionadded:: 3.14


.. c:function:: Py_complex _Py_cd_diff(Py_complex left, double right)

Return the difference of complex and real number, using the C
:c:type:`Py_complex` representation.

.. versionadded:: 3.14


.. c:function:: Py_complex _Py_c_neg(Py_complex num)

Return the negation of the complex number *num*, using the C
Expand All @@ -62,6 +86,14 @@ pointers. This is consistent throughout the API.
representation.


.. c:function:: Py_complex _Py_cd_prod(Py_complex left, double right)

Return the product of complex and real number, using the C
:c:type:`Py_complex` representation.

.. versionadded:: 3.14


.. c:function:: Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)

Return the quotient of two complex numbers, using 8000 the C :c:type:`Py_complex`
Expand All @@ -71,6 +103,28 @@ pointers. This is consistent throughout the API.
:c:data:`errno` to :c:macro:`!EDOM`.


.. c:function:: Py_complex _Py_dc_quot(double left, Py_complex right)

Return the quotent of real and complex number, using the C
:c:type:`Py_complex` representation.

If *divisor* is zero, this method returns zero and sets
:c:data:`errno` to :c:macro:`!EDOM`.

.. versionadded:: 3.14


.. c:function:: Py_complex _Py_cd_quot(Py_complex left, double right)

Return the quotient of complex and real number, using the C
:c:type:`Py_complex` representation.

If *divisor* is zero, this method returns zero and sets
:c:data:`errno` to :c:macro:`!EDOM`.

.. versionadded:: 3.14


.. c:function:: Py_complex _Py_c_pow(Py_complex num, Py_complex exp)

Return the exponentiation of *num* by *exp*, using the C :c:type:`Py_complex`
Expand Down
6 changes: 6 additions & 0 deletions Include/cpython/complexobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ typedef struct {

// Operations on complex numbers.
PyAPI_FUNC(Py_complex) _Py_c_sum(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) _Py_cd_sum(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_c_diff(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) _Py_dc_diff(double, Py_complex);
PyAPI_FUNC(Py_complex) _Py_cd_diff(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_c_neg(Py_complex);
PyAPI_FUNC(Py_complex) _Py_c_prod(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) _Py_cd_prod(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_c_quot(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) _Py_dc_quot(double, Py_complex);
PyAPI_FUNC(Py_complex) _Py_cd_quot(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_c_pow(Py_complex, Py_complex);
PyAPI_FUNC(double) _Py_c_abs(Py_complex);

Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def test_truediv(self):
z = complex(0, 0) / complex(denom_real, denom_imag)
self.assertTrue(isnan(z.real))
self.assertTrue(isnan(z.imag))
z = float(0) / complex(denom_real, denom_imag)
self.assertTrue(isnan(z.real))
self.assertTrue(isnan(z.imag))

self.assertComplexesAreIdentical(complex(INF, NAN) / 2,
complex(INF, NAN))
Expand Down Expand Up @@ -157,6 +160,39 @@ def test_truediv(self):
self.assertComplexesAreIdentical(complex(INF, 1)/complex(1, INF),
complex(NAN, NAN))

# mixed types
self.assertEqual((1+1j)/float(2), 0.5+0.5j)
self.assertEqual(float(1)/(1+2j), 0.2-0.4j)
self.assertEqual(float(1)/(-1+2j), -0.2-0.4j)
self.assertEqual(float(1)/(1-2j), 0.2+0.4j)
self.assertEqual(float(1)/(2+1j), 0.4-0.2j)
self.assertEqual(float(1)/(-2+1j), -0.4-0.2j)
self.assertEqual(float(1)/(2-1j), 0.4+0.2j)

self.assertComplexesAreIdentical(INF/(1+0j),
complex(INF, NAN))
self.assertComplexesAreIdentical(INF/(0.0+1j),
complex(NAN, -INF))
self.assertComplexesAreIdentical(INF/complex(2**1000, 2**-1000),
complex(INF, NAN))
self.assertComplexesAreIdentical(INF/complex(NAN, NAN),
complex(NAN, NAN))

self.assertComplexesAreIdentical(float(1)/complex(INF, INF), (0.0-0j))
self.assertComplexesAreIdentical(float(1)/complex(INF, -INF), (0.0+0j))
self.assertComplexesAreIdentical(float(1)/complex(-INF, INF),
complex(-0.0, -0.0))
self.assertComplexesAreIdentical(float(1)/complex(-INF, -INF),
complex(-0.0, 0))
self.assertComplexesAreIdentical(float(1)/complex(INF, NAN),
complex(0.0, -0.0))
self.assertComplexesAreIdentical(float(1)/complex(-INF, NAN),
complex(-0.0, -0.0))
self.assertComplexesAreIdentical(float(1)/complex(NAN, INF),
complex(0.0, -0.0))
self.assertComplexesAreIdentical(float(INF)/complex(NAN, INF),
complex(NAN, NAN))

def test_truediv_zero_division(self):
for a, b in ZERO_DIVISION:
with self.assertRaises(ZeroDivisionError):
Expand Down Expand Up @@ -242,6 +278,10 @@ def test_sub(self):
complex(-0.0, -0.0))
self.assertComplexesAreIdentical(-0.0 - complex(0.0, 0.0),
complex(-0.0, -0.0))
self.assertComplexesAreIdentical(complex(1, 2) - complex(2, 1),
complex(-1, 1))
self.assertComplexesAreIdentical(complex(2, 1) - complex(1, 2),
complex(1, -1))
self.assertRaises(OverflowError, operator.sub, 1j, 10**1000)
self.assertRaises(TypeError, operator.sub, 1j, None)
self.assertRaises(TypeError, operator.sub, None, 1j)
Expand Down
Loading
Loading
0