Skip to content

Commit 8767d07

Browse files
committed
py: complex_print uses format_float if single precision fp used.
1 parent bee17b0 commit 8767d07

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

py/objcomplex.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
#if MICROPY_ENABLE_FLOAT
1414

15+
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
16+
#include "formatfloat.h"
17+
#endif
18+
1519
typedef struct _mp_obj_complex_t {
1620
mp_obj_base_t base;
1721
mp_float_t real;
@@ -22,11 +26,24 @@ mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
2226

2327
STATIC void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
2428
mp_obj_complex_t *o = o_in;
29+
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
30+
char buf[32];
31+
if (o->real == 0) {
32+
format_float(o->imag, buf, sizeof(buf), 'g', 6, '\0');
33+
print(env, "%s", buf);
34+
} else {
35+
format_float(o->real, buf, sizeof(buf), 'g', 6, '\0');
36+
print(env, "(%s+", buf);
37+
format_float(o->real, buf, sizeof(buf), 'g', 6, '\0');
38+
print(env, "%sj)", buf);
39+
}
40+
#else
2541
if (o->real == 0) {
2642
print(env, "%.8gj", (double) o->imag);
2743
} else {
2844
print(env, "(%.8g+%.8gj)", (double) o->real, (double) o->imag);
2945
}
46+
#endif
3047
}
3148

3249
STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {

0 commit comments

Comments
 (0)