GeezerVison is showing github.com — gh-117999: fixed invalid small integer powers of real±0j · skirpichev/cpython@af7b478
File treeExpand file treeCollapse file tree Open diff view settings Misc/NEWS.d/next/Core and Builtins
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_complex.py +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -330,6 +330,13 @@ def test_pow_with_small_integer_exponents(self): | 330 | 330 | self.assertEqual(str(float_pow), str(int_pow)) | 331 | 331 | self.assertEqual(str(complex_pow), str(int_pow)) | 332 | 332 |
| | 333 | + self.assertFloatsAreIdentical(pow(complex(1,+0.0), 0).imag, +0.0) | | 334 | + self.assertFloatsAreIdentical(pow(complex(1,-0.0), 0).imag, +0.0) | | 335 | + self.assertFloatsAreIdentical(pow(complex(1,+0.0), 2).imag, +0.0) | | 336 | + self.assertFloatsAreIdentical(pow(complex(1,-0.0), 2).imag, -0.0) | | 337 | + self.assertFloatsAreIdentical(pow(complex(1,+0.0), -3).imag, -0.0) | | 338 | + self.assertFloatsAreIdentical(pow(complex(1,-0.0), -3).imag, +0.0) | | 339 | + | 333 | 340 | def test_boolcontext(self): | 334 | 341 | for i in range(100): | 335 | 342 | self.assertTrue(complex(random() + 1e-6, random() + 1e-6)) | |
Collapse file Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +Fixed invalid small integer powers for complex numbers with ``±0.0`` in | | 2 | +imaginary component. Patch by Sergey B Kirpichev. |
Collapse file Copy file name to clipboardExpand all lines: Objects/complexobject.c +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -162,6 +162,8 @@ c_powu(Py_complex x, long n) | 162 | 162 | Py_complex r, p; | 163 | 163 | long mask = 1; | 164 | 164 | r = c_1; | | 165 | + if (n) | | 166 | + r.imag = copysign(0.0, x.imag); | 165 | 167 | p = x; | 166 | 168 | while (mask > 0 && n >= mask) { | 167 | 169 | if (n & mask) | @@ -177,9 +179,10 @@ c_powi(Py_complex x, long n) | 177 | 179 | { | 178 | 180 | if (n > 0) | 179 | 181 | return c_powu(x,n); | 180 | | - else | | 182 | + else { | | 183 | + c_1.imag = -copysign(0.0, x.imag); | 181 | 184 | return _Py_c_quot(c_1, c_powu(x,-n)); | 182 | | - | | 185 | + } | 183 | 186 | } | 184 | 187 |
| 185 | 188 | double | |
|