summaryrefslogtreecommitdiffstatshomepage
path: root/py/objcomplex.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/objcomplex.c')
-rw-r--r--py/objcomplex.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/py/objcomplex.c b/py/objcomplex.c
index 96be25255c..5f9183f0e7 100644
--- a/py/objcomplex.c
+++ b/py/objcomplex.c
@@ -50,7 +50,11 @@ STATIC void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_
mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in);
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
char buf[16];
+ #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
+ const int precision = 6;
+ #else
const int precision = 7;
+ #endif
#else
char buf[32];
const int precision = 16;
@@ -80,7 +84,7 @@ STATIC mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, si
case 1:
if (MP_OBJ_IS_STR(args[0])) {
// a string, parse it
- mp_uint_t l;
+ size_t l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_decimal(s, l, true, true, NULL);
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
@@ -117,6 +121,7 @@ STATIC mp_obj_t complex_unary_op(mp_uint_t op, mp_obj_t o_in) {
mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in);
switch (op) {
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->real != 0 || o->imag != 0);
+ case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag));
case MP_UNARY_OP_POSITIVE: return o_in;
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
default: return MP_OBJ_NULL; // op not supported
@@ -222,8 +227,8 @@ mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t
// = exp(x3)*(cos(y3) + i*sin(y3))
mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real*lhs_real + lhs_imag*lhs_imag);
if (abs1 == 0) {
- if (rhs_imag == 0) {
- lhs_real = 1;
+ if (rhs_imag == 0 && rhs_real >= 0) {
+ lhs_real = (rhs_real == 0);
rhs_real = 0;
} else {
mp_raise_msg(&mp_type_ZeroDivisionError, "0.0 to a complex power");