Skip to content
Snippets Groups Projects
Commit 79474c6b authored by Damien George's avatar Damien George
Browse files

py: Remove unnecessary extra handling of padding of nan/inf.

C's printf will pad nan/inf differently to CPython.  Our implementation
originally conformed to C, now it conforms to CPython's way.

Tests for this are also added in this patch.
parent 2cae0f62
Branches
No related tags found
No related merge requests found
...@@ -363,21 +363,11 @@ int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, c ...@@ -363,21 +363,11 @@ int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, c
if ((flags & PF_FLAG_PAD_AFTER_SIGN) && buf[0] < '0') { if ((flags & PF_FLAG_PAD_AFTER_SIGN) && buf[0] < '0') {
// We have a sign character // We have a sign character
s++; s++;
if (*s <= '9' || (flags & PF_FLAG_PAD_NAN_INF)) {
// We have a number, or we have a inf/nan and PAD_NAN_INF is set
// With '{:06e}'.format(float('-inf')) you get '-00inf'
chrs += mp_print_strn(print, &buf[0], 1, 0, 0, 1); chrs += mp_print_strn(print, &buf[0], 1, 0, 0, 1);
width--; width--;
len--; len--;
} }
}
if (*s > 'A' && (flags & PF_FLAG_PAD_NAN_INF) == 0) {
// We have one of the inf or nan variants, suppress zero fill.
// With printf, if you use: printf("%06e", -inf) then you get " -inf"
// so suppress the zero fill.
fill = ' ';
}
chrs += mp_print_strn(print, s, len, flags, fill, width); chrs += mp_print_strn(print, s, len, flags, fill, width);
return chrs; return chrs;
......
...@@ -37,8 +37,7 @@ ...@@ -37,8 +37,7 @@
#define PF_FLAG_PAD_AFTER_SIGN (0x040) #define PF_FLAG_PAD_AFTER_SIGN (0x040)
#define PF_FLAG_CENTER_ADJUST (0x080) #define PF_FLAG_CENTER_ADJUST (0x080)
#define PF_FLAG_ADD_PERCENT (0x100) #define PF_FLAG_ADD_PERCENT (0x100)
#define PF_FLAG_PAD_NAN_INF (0x200) #define PF_FLAG_SHOW_OCTAL_LETTER (0x200)
#define PF_FLAG_SHOW_OCTAL_LETTER (0x400)
typedef void (*mp_print_strn_t)(void *data, const char *str, mp_uint_t len); typedef void (*mp_print_strn_t)(void *data, const char *str, mp_uint_t len);
......
...@@ -1200,7 +1200,6 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa ...@@ -1200,7 +1200,6 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa
type = 'g'; type = 'g';
} }
flags |= PF_FLAG_PAD_NAN_INF; // '{:06e}'.format(float('-inf')) should give '-00inf'
switch (type) { switch (type) {
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
case 'e': case 'e':
......
...@@ -23,6 +23,10 @@ test("{:10.4F}", -123.456) ...@@ -23,6 +23,10 @@ test("{:10.4F}", -123.456)
test("{:10.4G}", 123.456) test("{:10.4G}", 123.456)
test("{:10.4G}", -123.456) test("{:10.4G}", -123.456)
test("{:06e}", float("inf"))
test("{:06e}", float("-inf"))
test("{:06e}", float("nan"))
# The following fails right now # The following fails right now
#test("{:10.1}", 0.0) #test("{:10.1}", 0.0)
......
...@@ -17,3 +17,7 @@ print("%f" % 1.23456) ...@@ -17,3 +17,7 @@ print("%f" % 1.23456)
print("%F" % 1.23456) print("%F" % 1.23456)
print("%g" % 1.23456) print("%g" % 1.23456)
print("%G" % 1.23456) print("%G" % 1.23456)
print("%06e" % float("inf"))
print("%06e" % float("-inf"))
print("%06e" % float("nan"))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment