Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8062

MicroPython • Re: Error when converting a float to an int

$
0
0
perhaps this helps:
viewtopic.php?t=319525
Using stock MicroPython -

Code:

MicroPython v1.26.0-preview.142.ga1ee42cd3.dirty on 2025-05-21; Raspberry Pi Pico 2 W with RP2350Type "help()" for more information.>>> 1234.20 * 100123420.0>>> int(1234.20 * 100)123419>>> int("{:.0f}".format(1234.20 * 100))123420
I am sure it does ultimately come down to using single-precision but I recall there was discussion on the main problem being how MicroPython converts floats to integers using 'int'.

As can be seen in that last case MicroPython is entirely capable of doing the conversion correctly, it's just that using 'int' on a float doesn't.

Going to double-precision works around that issue but means having to build and use non-standard firmware and one's code won't work for any other user who hasn't done that. I would recommend using a workround as above.

Statistics: Posted by hippy — Fri Jul 04, 2025 2:53 pm



Viewing all articles
Browse latest Browse all 8062

Trending Articles