Skip to content

Commit f49782f

Browse files
committed
py: Fix cmath.log10; fix printing of complex number with negative imag.
1 parent 471b2a8 commit f49782f

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

py/modcmath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log_obj, mp_cmath_log);
9797
STATIC mp_obj_t mp_cmath_log10(mp_obj_t z_obj) {
9898
mp_float_t real, imag;
9999
mp_obj_get_complex(z_obj, &real, &imag);
100-
return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log10)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real));
100+
return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log10)(real*real + imag*imag), 0.4342944819032518 * MICROPY_FLOAT_C_FUN(atan2)(imag, real));
101101
}
102102
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10);
103103

py/objcomplex.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ STATIC void complex_print(void (*print)(void *env, const char *fmt, ...), void *
5858
print(env, "%sj", buf);
5959
} else {
6060
mp_format_float(o->real, buf, sizeof(buf), 'g', 7, '\0');
61-
print(env, "(%s+", buf);
61+
print(env, "(%s", buf);
62+
if (o->imag >= 0) {
63+
print(env, "+");
64+
}
6265
mp_format_float(o->imag, buf, sizeof(buf), 'g', 7, '\0');
6366
print(env, "%sj)", buf);
6467
}
@@ -69,7 +72,10 @@ STATIC void complex_print(void (*print)(void *env, const char *fmt, ...), void *
6972
print(env, "%sj", buf);
7073
} else {
7174
sprintf(buf, "%.16g", (double)o->real);
72-
print(env, "(%s+", buf);
75+
print(env, "(%s", buf);
76+
if (o->imag >= 0) {
77+
print(env, "+");
78+
}
7379
sprintf(buf, "%.16g", (double)o->imag);
7480
print(env, "%sj)", buf);
7581
}

0 commit comments

Comments
 (0)