Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion py/parsenum.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool
}
} else if (in == PARSE_DEC_IN_INTG && dig == '.') {
in = PARSE_DEC_IN_FRAC;
// check for case of single '0' after decimal to avoid rounding error
if (str + 1 == top && *str == '0') {
str++;
break;
}
} else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
in = PARSE_DEC_IN_EXP;
if (str < top) {
Expand Down Expand Up @@ -309,7 +314,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool
// of slightly erroneous values.
if (exp_val < 0 && exp_val >= -EXACT_POWER_OF_10) {
dec_val /= MICROPY_FLOAT_C_FUN(pow)(10, -exp_val);
} else {
} else if (exp_val != 0) {
dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val);
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/float/float_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@
# check small decimals are as close to their true value as possible
for n in range(1, 10):
print(float('0.%u' % n) == n / 10)

# Issue #5831 - make sure trailing 0 does not cause rounding with 32-bit float
# 14187745 has exact 32-bit representation but 141877450 does not
print(int(float("14187745.")))
print(int(float("14187745.0")))