py: Add .real and .imag attributes to complex numbers.
This commit is contained in:
@@ -132,6 +132,15 @@ STATIC mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
|||||||
return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
|
return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
STATIC void complex_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
||||||
|
mp_obj_complex_t *self = self_in;
|
||||||
|
if (attr == MP_QSTR_real) {
|
||||||
|
dest[0] = mp_obj_new_float(self->real);
|
||||||
|
} else if (attr == MP_QSTR_imag) {
|
||||||
|
dest[0] = mp_obj_new_float(self->imag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const mp_obj_type_t mp_type_complex = {
|
const mp_obj_type_t mp_type_complex = {
|
||||||
{ &mp_type_type },
|
{ &mp_type_type },
|
||||||
.name = MP_QSTR_complex,
|
.name = MP_QSTR_complex,
|
||||||
@@ -139,6 +148,7 @@ const mp_obj_type_t mp_type_complex = {
|
|||||||
.make_new = complex_make_new,
|
.make_new = complex_make_new,
|
||||||
.unary_op = complex_unary_op,
|
.unary_op = complex_unary_op,
|
||||||
.binary_op = complex_binary_op,
|
.binary_op = complex_binary_op,
|
||||||
|
.load_attr = complex_load_attr,
|
||||||
};
|
};
|
||||||
|
|
||||||
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
|
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
|
||||||
|
|||||||
@@ -136,7 +136,11 @@ Q(calcsize)
|
|||||||
Q(chr)
|
Q(chr)
|
||||||
Q(classmethod)
|
Q(classmethod)
|
||||||
Q(_collections)
|
Q(_collections)
|
||||||
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
||||||
Q(complex)
|
Q(complex)
|
||||||
|
Q(real)
|
||||||
|
Q(imag)
|
||||||
|
#endif
|
||||||
Q(dict)
|
Q(dict)
|
||||||
Q(dir)
|
Q(dir)
|
||||||
Q(divmod)
|
Q(divmod)
|
||||||
|
|||||||
Reference in New Issue
Block a user