From 49b5533000f87f000e080a634f173046c3d9dbe3 Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Tue, 25 Feb 2014 15:11:17 -0800 Subject: [PATCH] Fix floating point printing of negative numbers in the range (0, -1.0) --- stm/printf.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/stm/printf.c b/stm/printf.c index 7bac07ab72..cfe4204b61 100644 --- a/stm/printf.c +++ b/stm/printf.c @@ -219,9 +219,19 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) { mp_float_t d = va_arg(args, double); int left = (int)d; int right = (int)((d - (mp_float_t)(int)d) * 1000000.0); - chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width); - chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width); - chrs += pfenv_print_int(pfenv, right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6); + if (right < 0) { + if (left == 0) { + chrs += pfenv_print_strn(pfenv, "-0", 2, flags, width); + } else { + chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width); + } + chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width); + chrs += pfenv_print_int(pfenv, -right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6); + } else { + chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width); + chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width); + chrs += pfenv_print_int(pfenv, right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6); + } break; } default: