From 861670ba2ad6fe575de4a31c95c01070ed900391 Mon Sep 17 00:00:00 2001 From: stijn Date: Sat, 16 May 2015 10:54:19 +0200 Subject: py: Implement mp_format_float for doubles and use where appropriate This allows using (almost) the same code for printing floats everywhere, removes the dependency on sprintf and uses just snprintf and applies an msvc-specific fix for snprintf in a single place so nan/inf are now printed correctly. --- py/objcomplex.c | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) (limited to 'py/objcomplex.c') diff --git a/py/objcomplex.c b/py/objcomplex.c index 8a424f7f2..b790b0563 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -37,10 +37,7 @@ #if MICROPY_PY_BUILTINS_COMPLEX #include - -#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT #include "py/formatfloat.h" -#endif typedef struct _mp_obj_complex_t { mp_obj_base_t base; @@ -53,33 +50,23 @@ STATIC void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_ mp_obj_complex_t *o = o_in; #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT char buf[16]; - if (o->real == 0) { - mp_format_float(o->imag, buf, sizeof(buf), 'g', 7, '\0'); - mp_printf(print, "%sj", buf); - } else { - mp_format_float(o->real, buf, sizeof(buf), 'g', 7, '\0'); - mp_printf(print, "(%s", buf); - if (o->imag >= 0 || isnan(o->imag)) { - mp_print_str(print, "+"); - } - mp_format_float(o->imag, buf, sizeof(buf), 'g', 7, '\0'); - mp_printf(print, "%sj)", buf); - } + const int precision = 7; #else char buf[32]; + const int precision = 16; +#endif if (o->real == 0) { - sprintf(buf, "%.16g", (double)o->imag); + mp_format_float(o->imag, buf, sizeof(buf), 'g', precision, '\0'); mp_printf(print, "%sj", buf); } else { - sprintf(buf, "%.16g", (double)o->real); + mp_format_float(o->real, buf, sizeof(buf), 'g', precision, '\0'); mp_printf(print, "(%s", buf); if (o->imag >= 0 || isnan(o->imag)) { mp_print_str(print, "+"); } - sprintf(buf, "%.16g", (double)o->imag); + mp_format_float(o->imag, buf, sizeof(buf), 'g', precision, '\0'); mp_printf(print, "%sj)", buf); } -#endif } STATIC mp_obj_t complex_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { -- cgit v1.2.3