34 #if MICROPY_PY_BUILTINS_COMPLEX 39 typedef struct _mp_obj_complex_t {
48 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 50 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C 51 const int precision = 6;
53 const int precision = 7;
57 const int precision = 16;
60 mp_format_float(o->imag, buf,
sizeof(buf),
'g', precision,
'\0');
63 mp_format_float(o->real, buf,
sizeof(buf),
'g', precision,
'\0');
65 if (o->imag >= 0 ||
isnan(o->imag)) {
68 mp_format_float(o->imag, buf,
sizeof(buf),
'g', precision,
'\0');
79 return mp_obj_new_complex(0, 0);
92 return mp_obj_new_complex(mp_obj_get_float(
args[0]), 0);
97 mp_float_t real, imag;
99 mp_obj_complex_get(
args[0], &real, &imag);
101 real = mp_obj_get_float(
args[0]);
105 mp_float_t real2, imag2;
106 mp_obj_complex_get(
args[1], &real2, &imag2);
110 imag += mp_obj_get_float(
args[1]);
112 return mp_obj_new_complex(real, imag);
120 case MP_UNARY_OP_BOOL:
return mp_obj_new_bool(o->real != 0 || o->imag != 0);
125 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(
sqrt)(o->real*o->real + o->imag*o->imag));
132 return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
141 if (
attr == MP_QSTR_real) {
142 dest[0] = mp_obj_new_float(self->real);
143 }
else if (
attr == MP_QSTR_imag) {
144 dest[0] = mp_obj_new_float(self->imag);
150 .
name = MP_QSTR_complex,
151 .print = complex_print,
152 .make_new = complex_make_new,
153 .unary_op = complex_unary_op,
154 .binary_op = complex_binary_op,
155 .attr = complex_attr,
158 mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
159 mp_obj_complex_t *o =
m_new_obj(mp_obj_complex_t);
166 void mp_obj_complex_get(
mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
174 mp_float_t rhs_real, rhs_imag;
175 mp_obj_get_complex(rhs_in, &rhs_real, &rhs_imag);
179 lhs_real += rhs_real;
180 lhs_imag += rhs_imag;
184 lhs_real -= rhs_real;
185 lhs_imag -= rhs_imag;
191 real = lhs_real * rhs_real - lhs_imag * rhs_imag;
192 lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
206 lhs_real /= rhs_real;
207 lhs_imag /= rhs_real;
208 }
else if (rhs_real == 0) {
209 mp_float_t real = lhs_imag / rhs_imag;
210 lhs_imag = -lhs_real / rhs_imag;
213 mp_float_t rhs_len_sq = rhs_real*rhs_real + rhs_imag*rhs_imag;
214 rhs_real /= rhs_len_sq;
215 rhs_imag /= -rhs_len_sq;
227 mp_float_t abs1 = MICROPY_FLOAT_C_FUN(
sqrt)(lhs_real*lhs_real + lhs_imag*lhs_imag);
229 if (rhs_imag == 0 && rhs_real >= 0) {
230 lhs_real = (rhs_real == 0);
235 mp_float_t ln1 = MICROPY_FLOAT_C_FUN(
log)(abs1);
236 mp_float_t arg1 = MICROPY_FLOAT_C_FUN(
atan2)(lhs_imag, lhs_real);
237 mp_float_t x3 = rhs_real * ln1 - rhs_imag * arg1;
238 mp_float_t y3 = rhs_imag * ln1 + rhs_real * arg1;
239 mp_float_t exp_x3 = MICROPY_FLOAT_C_FUN(
exp)(x3);
240 lhs_real = exp_x3 * MICROPY_FLOAT_C_FUN(
cos)(y3);
241 lhs_imag = exp_x3 * MICROPY_FLOAT_C_FUN(
sin)(y3);
246 case MP_BINARY_OP_EQUAL:
return mp_obj_new_bool(lhs_real == rhs_real && lhs_imag == rhs_imag);
251 return mp_obj_new_complex(lhs_real, lhs_imag);
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
const mp_obj_type_t mp_type_ZeroDivisionError
#define MP_OBJ_IS_TYPE(o, t)
STATIC const uint8_t attr[]
int mp_print_str(const mp_print_t *print, const char *str)
#define MP_OBJ_FROM_PTR(p)
void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw)
const mp_obj_type_t mp_type_complex
#define MP_OBJ_NEW_SMALL_INT(small_int)
const char * mp_obj_str_get_data(mp_obj_t self_in, size_t *len)
const mp_obj_type_t mp_type_type
mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex)
int mp_printf(const mp_print_t *print, const char *fmt,...)
NORETURN void mp_raise_TypeError(const char *msg)