Skip to content

Commit 71888a4

Browse files
committed
Simplify _Py_rc_quot()?
1 parent 92972ae commit 71888a4

File tree

3 files changed

+14
-46
lines changed

3 files changed

+14
-46
lines changed

Doc/library/stdtypes.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,12 @@ The constructors :func:`int`, :func:`float`, and
266266
Python fully supports mixed arithmetic: when a binary arithmetic operator has
267267
operands of different numeric types, the operand with the "narrower" type is
268268
widened to that of the other, where integer is narrower than floating point.
269-
Arithmetic with complex and real operands is defined by the usual mathematical
270-
formula, for example::
269+
Arithmetic with mixed complex and real operands is defined by::
271270

272-
x + complex(u, v) = complex(x + u, v)
273-
x * complex(u, v) = complex(x * u, x * v)
271+
x + complex(u, v) = complex(u, v) + x = complex(x + u, v)
272+
x * complex(u, v) = complex(u, v) * x = complex(x * u, x * v)
273+
complex(u, v) / x = complex(u / x, v / x)
274+
x / complex(u, v) = complex(x, 0) / complex(u, v)
274275

275276
A comparison between numbers of different types behaves as though the exact
276277
values of those numbers were being compared. [2]_

Lib/test/test_complex.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,17 @@ def test_truediv(self):
187187
self.assertComplexesAreIdentical(float(1)/complex(-INF, -INF),
188188
complex(-0.0, 0))
189189
self.assertComplexesAreIdentical(float(1)/complex(INF, NAN),
190-
complex(0.0, -0.0))
190+
complex(0.0, 0.0))
191191
self.assertComplexesAreIdentical(float(1)/complex(-INF, NAN),
192192
complex(-0.0, -0.0))
193193
self.assertComplexesAreIdentical(float(1)/complex(NAN, INF),
194194
complex(0.0, -0.0))
195195
self.assertComplexesAreIdentical(float(INF)/complex(NAN, INF),
196196
complex(NAN, NAN))
197197

198+
self.assertComplexesAreIdentical(float(-1.0)/complex(0.0, 1.0),
199+
complex(0.0, 1))
200+
198201
def test_truediv_zero_division(self):
199202
for a, b in ZERO_DIVISION:
200203
with self.assertRaises(ZeroDivisionError):

Objects/complexobject.c

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ _Py_c_quot(Py_complex a, Py_complex b)
244244

245245
return r;
246246
}
247+
#ifdef _M_ARM64
248+
#pragma optimize("", on)
249+
#endif
247250

248251
Py_complex
249252
_Py_cr_quot(Py_complex a, double b)
@@ -260,51 +263,12 @@ _Py_cr_quot(Py_complex a, double b)
260263
return r;
261264
}
262265

263-
/* an equivalent of _Py_c_quot() function, when 1st argument is real */
264266
Py_complex
265267
_Py_rc_quot(double a, Py_complex b)
266268
{
267-
Py_complex r;
268-
const double abs_breal = b.real < 0 ? -b.real : b.real;
269-
const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
270-
271-
if (abs_breal >= abs_bimag) {
272-
if (abs_breal == 0.0) {
273-
errno = EDOM;
274-
r.real = r.imag = 0.0;
275-
}
276-
else {
277-
const double ratio = b.imag / b.real;
278-
const double denom = b.real + b.imag * ratio;
279-
r.real = a / denom;
280-
r.imag = (-a * ratio) / denom;
281-
}
282-
}
283-
else if (abs_bimag >= abs_breal) {
284-
const double ratio = b.real / b.imag;
285-
const double denom = b.real * ratio + b.imag;
286-
assert(b.imag != 0.0);
287-
r.real = (a * ratio) / denom;
288-
r.imag = (-a) / denom;
289-
}
290-
else {
291-
r.real = r.imag = Py_NAN;
292-
}
293-
294-
if (isnan(r.real) && isnan(r.imag) && isfinite(a)
295-
&& (isinf(abs_breal) || isinf(abs_bimag)))
296-
{
297-
const double x = copysign(isinf(b.real) ? 1.0 : 0.0, b.real);
298-
const double y = copysign(isinf(b.imag) ? 1.0 : 0.0, b.imag);
299-
r.real = 0.0 * (a*x);
300-
r.imag = 0.0 * (-a*y);
301-
}
302-
303-
return r;
269+
errno = 0;
270+
return _Py_c_quot((Py_complex){a, 0.0}, b);
304271
}
305-
#ifdef _M_ARM64
306-
#pragma optimize("", on)
307-
#endif
308272

309273
Py_complex
310274
_Py_c_pow(Py_complex a, Py_complex b)

0 commit comments

Comments
 (0)