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/objfloat.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'py/objfloat.c') diff --git a/py/objfloat.c b/py/objfloat.c index f74e12f9d..ffb3c7196 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -37,31 +37,24 @@ #if MICROPY_PY_BUILTINS_FLOAT #include - -#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT #include "py/formatfloat.h" -#endif STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { (void)kind; mp_obj_float_t *o = o_in; #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT char buf[16]; - mp_format_float(o->value, buf, sizeof(buf), 'g', 7, '\0'); - mp_print_str(print, buf); - if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) { - // Python floats always have decimal point (unless inf or nan) - mp_print_str(print, ".0"); - } + const int precision = 7; #else char buf[32]; - sprintf(buf, "%.16g", (double) o->value); + const int precision = 16; +#endif + mp_format_float(o->value, buf, sizeof(buf), 'g', precision, '\0'); mp_print_str(print, buf); if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) { // Python floats always have decimal point (unless inf or nan) mp_print_str(print, ".0"); } -#endif } STATIC mp_obj_t float_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