Skip to content

Commit 46521c3

Browse files
committed
address review:
* more tests for multiplication * rename to _Py_convert_int_to_double * rename to real_to_float/complex * slightly optimize code
1 parent 3980363 commit 46521c3

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

Include/internal/pycore_floatobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern PyObject* _Py_string_to_number_with_underscores(
5454

5555
extern double _Py_parse_inf_or_nan(const char *p, char **endptr);
5656

57-
extern int _Py_convert_to_double(PyObject **v, double *dbl);
57+
extern int _Py_convert_int_to_double(PyObject **v, double *dbl);
5858

5959

6060
#ifdef __cplusplus

Lib/test/test_complex.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,12 @@ def test_sub(self):
249249
def test_mul(self):
250250
self.assertEqual(1j * int(20), complex(0, 20))
251251
self.assertEqual(1j * int(-1), complex(0, -1))
252-
self.assertComplexesAreIdentical(complex(INF, NAN) * 2,
253-
complex(INF, NAN))
254-
self.assertComplexesAreIdentical(2 * complex(INF, NAN),
255-
complex(INF, NAN))
252+
for c, r in [(2, complex(INF, 2)), (INF, complex(INF, INF)),
253+
(0, complex(NAN, 0)), (-0.0, complex(NAN, -0.0)),
254+
(NAN, complex(NAN, NAN))]:
255+
with self.subTest(c=c, r=r):
256+
self.assertComplexesAreIdentical(complex(INF, 1) * c, r)
257+
self.assertComplexesAreIdentical(c * complex(INF, 1), r)
256258
self.assertRaises(OverflowError, operator.mul, 1j, 10**1000)
257259
self.assertRaises(TypeError, operator.mul, 1j, None)
258260
self.assertRaises(TypeError, operator.mul, None, 1j)

Objects/complexobject.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "Python.h"
99
#include "pycore_call.h" // _PyObject_CallNoArgs()
1010
#include "pycore_complexobject.h" // _PyComplex_FormatAdvancedWriter()
11-
#include "pycore_floatobject.h" // _Py_convert_to_double()
11+
#include "pycore_floatobject.h" // _Py_convert_int_to_double()
1212
#include "pycore_long.h" // _PyLong_GetZero()
1313
#include "pycore_object.h" // _PyObject_Init()
1414
#include "pycore_pymath.h" // _Py_ADJUST_ERANGE2()
@@ -475,31 +475,31 @@ complex_hash(PyComplexObject *v)
475475
}
476476

477477
/* This macro may return! */
478-
#define TO_COMPLEX(obj, c) \
479-
if (PyComplex_Check(obj)) \
480-
c = ((PyComplexObject *)(obj))->cval; \
481-
else if (to_complex(&(obj), &(c)) < 0) \
478+
#define TO_COMPLEX(obj, c) \
479+
if (PyComplex_Check(obj)) \
480+
c = ((PyComplexObject *)(obj))->cval; \
481+
else if (real_to_complex(&(obj), &(c)) < 0) \
482482
return (obj)
483483

484484
static int
485-
to_float(PyObject **pobj, double *dbl)
485+
real_to_float(PyObject **pobj, double *dbl)
486486
{
487487
PyObject *obj = *pobj;
488488

489489
if (PyFloat_Check(obj)) {
490490
*dbl = PyFloat_AS_DOUBLE(obj);
491491
}
492-
else if (_Py_convert_to_double(pobj, dbl) < 0) {
492+
else if (_Py_convert_int_to_double(pobj, dbl) < 0) {
493493
return -1;
494494
}
495495
return 0;
496496
}
497497

498498
static int
499-
to_complex(PyObject **pobj, Py_complex *pc)
499+
real_to_complex(PyObject **pobj, Py_complex *pc)
500500
{
501501
pc->imag = 0.0;
502-
return to_float(pobj, &(pc->real));
502+
return real_to_float(pobj, &(pc->real));
503503
}
504504

505505
/* Complex arithmetic rules implement special mixed-mode case: combining
@@ -523,7 +523,7 @@ to_complex(PyObject **pobj, Py_complex *pc)
523523
static PyObject *
524524
complex_add(PyObject *v, PyObject *w)
525525
{
526-
if (PyComplex_Check(w)) {
526+
if (!PyComplex_Check(v)) {
527527
PyObject *tmp = v;
528528
v = w;
529529
w = tmp;
@@ -536,7 +536,7 @@ complex_add(PyObject *v, PyObject *w)
536536
Py_complex b = ((PyComplexObject *)(w))->cval;
537537
a = _Py_c_sum(a, b);
538538
}
539-
else if (to_float(&w, &b) < 0) {
539+
else if (real_to_float(&w, &b) < 0) {
540540
return w;
541541
}
542542
else {
@@ -559,19 +559,19 @@ complex_sub(PyObject *v, PyObject *w)
559559
errno = 0;
560560
a = _Py_c_diff(a, b);
561561
}
562-
else if (to_float(&v, &a.real) < 0) {
562+
else if (real_to_float(&v, &a.real) < 0) {
563563
return v;
564564
}
565565
else {
566-
a = (Py_complex) {a.real, -b.imag};
567566
a.real -= b.real;
567+
a.imag = -b.imag;
568568
}
569569
}
570570
else {
571571
a = ((PyComplexObject *)(v))->cval;
572572
double b;
573573

574-
if (to_float(&w, &b) < 0) {
574+
if (real_to_float(&w, &b) < 0) {
575575
return w;
576576
}
577577
a.real -= b;
@@ -583,7 +583,7 @@ complex_sub(PyObject *v, PyObject *w)
583583
static PyObject *
584584
complex_mul(PyObject *v, PyObject *w)
585585
{
586-
if (PyComplex_Check(w)) {
586+
if (!PyComplex_Check(v)) {
587587
PyObject *tmp = v;
588588
v = w;
589589
w = tmp;
@@ -596,7 +596,7 @@ complex_mul(PyObject *v, PyObject *w)
596596
Py_complex b = ((PyComplexObject *)(w))->cval;
597597
a = _Py_c_prod(a, b);
598598
}
599-
else if (to_float(&w, &b) < 0) {
599+
else if (real_to_float(&w, &b) < 0) {
600600
return w;
601601
}
602602
else {
@@ -621,7 +621,7 @@ complex_div(PyObject *v, PyObject *w)
621621
else {
622622
double b;
623623

624-
if (to_float(&w, &b) < 0) {
624+
if (real_to_float(&w, &b) < 0) {
625625
return w;
626626
}
627627
if (b) {

Objects/floatobject.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,16 @@ PyFloat_AsDouble(PyObject *op)
306306
obj is not of float or int type, Py_NotImplemented is incref'ed,
307307
stored in obj, and returned from the function invoking this macro.
308308
*/
309-
#define CONVERT_TO_DOUBLE(obj, dbl) \
310-
if (PyFloat_Check(obj)) \
311-
dbl = PyFloat_AS_DOUBLE(obj); \
312-
else if (_Py_convert_to_double(&(obj), &(dbl)) < 0) \
309+
#define CONVERT_TO_DOUBLE(obj, dbl) \
310+
if (PyFloat_Check(obj)) \
311+
dbl = PyFloat_AS_DOUBLE(obj); \
312+
else if (_Py_convert_int_to_double(&(obj), &(dbl)) < 0) \
313313
return obj;
314314

315315
/* Methods */
316316

317317
int
318-
_Py_convert_to_double(PyObject **v, double *dbl)
318+
_Py_convert_int_to_double(PyObject **v, double *dbl)
319319
{
320320
PyObject *obj = *v;
321321

0 commit comments

Comments
 (0)