@@ -144,22 +144,40 @@ mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_im
144144 lhs_imag -= rhs_imag ;
145145 break ;
146146 case MP_BINARY_OP_MULTIPLY :
147- case MP_BINARY_OP_INPLACE_MULTIPLY :
148- {
149- mp_float_t real = lhs_real * rhs_real - lhs_imag * rhs_imag ;
147+ case MP_BINARY_OP_INPLACE_MULTIPLY : {
148+ mp_float_t real ;
149+ multiply :
150+ real = lhs_real * rhs_real - lhs_imag * rhs_imag ;
150151 lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real ;
151152 lhs_real = real ;
152153 break ;
153154 }
154- /* TODO floor(?) the value
155155 case MP_BINARY_OP_FLOOR_DIVIDE :
156- case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break;
157- */
158- /* TODO
156+ case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE :
157+ nlr_raise ( mp_obj_new_exception_msg_varg ( & mp_type_TypeError , "can't do truncated division of a complex number" ));
158+
159159 case MP_BINARY_OP_TRUE_DIVIDE :
160- case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: val = lhs_val / rhs_val; break;
161- */
162- return NULL ; // op not supported
160+ case MP_BINARY_OP_INPLACE_TRUE_DIVIDE :
161+ if (rhs_imag == 0 ) {
162+ if (rhs_real == 0 ) {
163+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ZeroDivisionError , "complex division by zero" ));
164+ }
165+ lhs_real /= rhs_real ;
166+ lhs_imag /= rhs_real ;
167+ } else if (rhs_real == 0 ) {
168+ mp_float_t real = lhs_imag / rhs_imag ;
169+ lhs_imag = - lhs_real / rhs_imag ;
170+ lhs_real = real ;
171+ } else {
172+ mp_float_t rhs_len_sq = rhs_real * rhs_real + rhs_imag * rhs_imag ;
173+ rhs_real /= rhs_len_sq ;
174+ rhs_imag /= - rhs_len_sq ;
175+ goto multiply ;
176+ }
177+ break ;
178+
179+ default :
180+ return MP_OBJ_NULL ; // op not supported
163181 }
164182 return mp_obj_new_complex (lhs_real , lhs_imag );
165183}
0 commit comments