diff options
| author | stijn | 2015-05-16 10:54:19 +0200 |
|---|---|---|
| committer | Damien George | 2015-05-17 21:47:11 +0100 |
| commit | 861670ba2ad6fe575de4a31c95c01070ed900391 (patch) | |
| tree | 4a62df6a4e95183f472a38ee06c4b8729f2d51c1 /py/mpprint.c | |
| parent | f5dd6f7f3707b67acbd1dbfe71cad2b958d5d7be (diff) | |
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.
Diffstat (limited to 'py/mpprint.c')
| -rw-r--r-- | py/mpprint.c | 25 |
1 files changed, 2 insertions, 23 deletions
diff --git a/py/mpprint.c b/py/mpprint.c index 99d0357af..78a0b7aa5 100644 --- a/py/mpprint.c +++ b/py/mpprint.c @@ -35,10 +35,6 @@ #include "py/objint.h" #include "py/runtime.h" -#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE -#include <stdio.h> -#endif - #if MICROPY_PY_BUILTINS_FLOAT #include "py/formatfloat.h" #endif @@ -340,29 +336,12 @@ int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, c if (flags & PF_FLAG_SPACE_SIGN) { sign = ' '; } - int len; -#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT - len = mp_format_float(f, buf, sizeof(buf), fmt, prec, sign); -#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE - char fmt_buf[6]; - char *fmt_s = fmt_buf; - *fmt_s++ = '%'; - if (sign) { - *fmt_s++ = sign; - } - *fmt_s++ = '.'; - *fmt_s++ = '*'; - *fmt_s++ = fmt; - *fmt_s = '\0'; - - len = snprintf(buf, sizeof(buf), fmt_buf, prec, f); + int len = mp_format_float(f, buf, sizeof(buf), fmt, prec, sign); if (len < 0) { len = 0; } -#else -#error Unknown MICROPY FLOAT IMPL -#endif + char *s = buf; if ((flags & PF_FLAG_ADD_PERCENT) && (size_t)(len + 1) < sizeof(buf)) { |
