py/formatfloat: Fix rounding of %f format with edge-case FP values. · Coderah/circuitpython@bc12eca · GitHub
Skip to content

Commit bc12eca

Browse files
committed
py/formatfloat: Fix rounding of %f format with edge-case FP values.
Prior to this patch the %f formatting of some FP values could be off by up to 1, eg '%.0f' % 123 would return "122" (unix x64). Depending on the FP precision (single vs double) certain numbers would format correctly, but others wolud not. This patch should fix all cases of rounding for %f.
1 parent 90e719a commit bc12eca

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

py/formatfloat.c

Lines changed: 1 addition & 1 deletion

tests/float/float_format.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# test float formatting
2+
3+
# general rounding
4+
for val in (116, 1111, 1234, 5010, 11111):
5+
print('%.0f' % val)
6+
print('%.1f' % val)
7+
print('%.3f' % val)
8+
9+
# make sure rounding is done at the correct precision
10+
for prec in range(8):
11+
print(('%%.%df' % prec) % 6e-5)

tests/unix/extra_coverage.py.exp

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)