Skip to content

Commit 79474c6

Browse files
committed
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.
1 parent 2cae0f6 commit 79474c6

File tree

5 files changed

+12
-16
lines changed

5 files changed

+12
-16
lines changed

py/mpprint.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -363,21 +363,11 @@ int mp_print_float(const mp_print_t *print, mp_float_t f, char fmt, int flags, c
363363
if ((flags & PF_FLAG_PAD_AFTER_SIGN) && buf[0] < '0') {
364364
// We have a sign character
365365
s++;
366-
if (*s <= '9' || (flags & PF_FLAG_PAD_NAN_INF)) {
367-
// We have a number, or we have a inf/nan and PAD_NAN_INF is set
368-
// With '{:06e}'.format(float('-inf')) you get '-00inf'
369-
chrs += mp_print_strn(print, &buf[0], 1, 0, 0, 1);
370-
width--;
371-
len--;
372-
}
366+
chrs += mp_print_strn(print, &buf[0], 1, 0, 0, 1);
367+
width--;
368+
len--;
373369
}
374370

375-
if (*s > 'A' && (flags & PF_FLAG_PAD_NAN_INF) == 0) {
376-
// We have one of the inf or nan variants, suppress zero fill.
377-
// With printf, if you use: printf("%06e", -inf) then you get " -inf"
378-
// so suppress the zero fill.
379-
fill = ' ';
380-
}
381371
chrs += mp_print_strn(print, s, len, flags, fill, width);
382372

383373
return chrs;

py/mpprint.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
#define PF_FLAG_PAD_AFTER_SIGN (0x040)
3838
#define PF_FLAG_CENTER_ADJUST (0x080)
3939
#define PF_FLAG_ADD_PERCENT (0x100)
40-
#define PF_FLAG_PAD_NAN_INF (0x200)
41-
#define PF_FLAG_SHOW_OCTAL_LETTER (0x400)
40+
#define PF_FLAG_SHOW_OCTAL_LETTER (0x200)
4241

4342
typedef void (*mp_print_strn_t)(void *data, const char *str, mp_uint_t len);
4443

py/objstr.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
12001200
type = 'g';
12011201
}
12021202

1203-
flags |= PF_FLAG_PAD_NAN_INF; // '{:06e}'.format(float('-inf')) should give '-00inf'
12041203
switch (type) {
12051204
#if MICROPY_PY_BUILTINS_FLOAT
12061205
case 'e':

tests/float/string_format.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def test(fmt, *args):
2323
test("{:10.4G}", 123.456)
2424
test("{:10.4G}", -123.456)
2525

26+
test("{:06e}", float("inf"))
27+
test("{:06e}", float("-inf"))
28+
test("{:06e}", float("nan"))
29+
2630
# The following fails right now
2731
#test("{:10.1}", 0.0)
2832

tests/float/string_format_modulo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
print("%F" % 1.23456)
1818
print("%g" % 1.23456)
1919
print("%G" % 1.23456)
20+
21+
print("%06e" % float("inf"))
22+
print("%06e" % float("-inf"))
23+
print("%06e" % float("nan"))

0 commit comments

Comments
 (0)