← index #4411Issue #11141
Related · high · value 1.271
QUERY · ISSUE

f presentation type is not rounding

openby qoheniacopened 2019-01-19updated 2024-09-19
needs-info

In MicroPython (tested with version 1.9.3) the f presentation type (as in "{:.0f}".format(x)) is not rounding floating point numbers as CPython (tested with 3.7.2) does. Actually I'm not sure what it really does. It is not rounding, not flooring and not truncating as the following examples show:

print("{:.0f}".format(100.0))
CPython: >>> 100
MicroPython: >>> 100

print("{:.0f}".format(100.9))
CPython: >>> 101
MicroPython: >>> 100

print("{:.0f}".format(101.0))
CPython: >>> 101
MicroPython: >>> 100

print("{:.0f}".format(99.5))
CPython: >>> 100
MicroPython: >>> 99

print("{:.0f}".format(99.6))
CPython: >>> 100
MicroPython: >>> 100
CANDIDATE · ISSUE

Wrong float numbers in print()

closedby ondrejkarpisopened 2023-03-28updated 2023-03-28
bug

MicroPython v1.19.1 on 2022-06-18, ESP32 module
(esp32-20220618-v1.19.1.bin)

Tested code:
for i in range(0,1000):
print(1.0*i)

The above code should produce output in form
1.0
2.0
...
999.0

but, for some "i" it produces:
84.00001
89.00001
94.00001
99.00001
805.9999
809.9999
813.9999
817.9999
...

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied