@@ -48,14 +48,14 @@ static mp_obj_t complex_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *a
4848 {
4949 mp_float_t real , imag ;
5050 if (MP_OBJ_IS_TYPE (args [1 ], & complex_type )) {
51- mp_obj_get_complex (args [1 ], & real , & imag );
51+ mp_obj_complex_get (args [1 ], & real , & imag );
5252 } else {
5353 real = mp_obj_get_float (args [1 ]);
5454 imag = 0 ;
5555 }
5656 if (MP_OBJ_IS_TYPE (args [0 ], & complex_type )) {
5757 mp_float_t real2 , imag2 ;
58- mp_obj_get_complex (args [0 ], & real2 , & imag2 );
58+ mp_obj_complex_get (args [0 ], & real2 , & imag2 );
5959 real -= imag2 ;
6060 imag += real2 ;
6161 } else {
@@ -80,9 +80,41 @@ static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
8080}
8181
8282static mp_obj_t complex_binary_op (int op , mp_obj_t lhs_in , mp_obj_t rhs_in ) {
83- mp_float_t lhs_real , lhs_imag , rhs_real , rhs_imag ;
84- mp_obj_complex_get (lhs_in , & lhs_real , & lhs_imag );
85- mp_obj_complex_get (rhs_in , & rhs_real , & rhs_imag );
83+ mp_obj_complex_t * lhs = lhs_in ;
84+ return mp_obj_complex_binary_op (op , lhs -> real , lhs -> imag , rhs_in );
85+ }
86+
87+ const mp_obj_type_t complex_type = {
88+ { & mp_const_type },
89+ "complex" ,
90+ complex_print , // print
91+ complex_make_new , // make_new
92+ NULL , // call_n
93+ complex_unary_op , // unary_op
94+ complex_binary_op , // binary_op
95+ NULL , // getiter
96+ NULL , // iternext
97+ .methods = { { NULL , NULL }, },
98+ };
99+
100+ mp_obj_t mp_obj_new_complex (mp_float_t real , mp_float_t imag ) {
101+ mp_obj_complex_t * o = m_new_obj (mp_obj_complex_t );
102+ o -> base .type = & complex_type ;
103+ o -> real = real ;
104+ o -> imag = imag ;
105+ return o ;
106+ }
107+
108+ void mp_obj_complex_get (mp_obj_t self_in , mp_float_t * real , mp_float_t * imag ) {
109+ assert (MP_OBJ_IS_TYPE (self_in , & complex_type ));
110+ mp_obj_complex_t * self = self_in ;
111+ * real = self -> real ;
112+ * imag = self -> imag ;
113+ }
114+
115+ mp_obj_t mp_obj_complex_binary_op (int op , mp_float_t lhs_real , mp_float_t lhs_imag , mp_obj_t rhs_in ) {
116+ mp_float_t rhs_real , rhs_imag ;
117+ mp_obj_get_complex (rhs_in , & rhs_real , & rhs_imag ); // can be any type, this function will convert to float (if possible)
86118 switch (op ) {
87119 case RT_BINARY_OP_ADD :
88120 case RT_BINARY_OP_INPLACE_ADD :
@@ -115,32 +147,4 @@ static mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
115147 return mp_obj_new_complex (lhs_real , lhs_imag );
116148}
117149
118- const mp_obj_type_t complex_type = {
119- { & mp_const_type },
120- "complex" ,
121- complex_print , // print
122- complex_make_new , // make_new
123- NULL , // call_n
124- complex_unary_op , // unary_op
125- complex_binary_op , // binary_op
126- NULL , // getiter
127- NULL , // iternext
128- .methods = { { NULL , NULL }, },
129- };
130-
131- mp_obj_t mp_obj_new_complex (mp_float_t real , mp_float_t imag ) {
132- mp_obj_complex_t * o = m_new_obj (mp_obj_complex_t );
133- o -> base .type = & complex_type ;
134- o -> real = real ;
135- o -> imag = imag ;
136- return o ;
137- }
138-
139- void mp_obj_complex_get (mp_obj_t self_in , mp_float_t * real , mp_float_t * imag ) {
140- assert (MP_OBJ_IS_TYPE (self_in , & complex_type ));
141- mp_obj_complex_t * self = self_in ;
142- * real = self -> real ;
143- * imag = self -> imag ;
144- }
145-
146150#endif
0 commit comments