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

MicroPython • Re: int() doing odd things.

$
0
0
I am solidly of the opinion there should be no rounding.
This is a complete or consistent problem. Representing floating point is hard. Canadian national treasure Julia Evans describes this quite well: Why does 0.1 + 0.2 = 0.30000000000000004?

Try this in a CPython REPL, and then in a MicroPython REPL:

Code:

a=0.1+0.2b=0.3a==b
CPython returns False, but MicroPython returns True. CPython is, according to IEEE double-precision floating point rules, correct but illogical/inconsistent. MicroPython is being logical, but I'm not going there whether it's correct or not.

CPython has the handy math.as_integer_ratio() and math.nextafter() functions to debug what's inside a floating point number. MicroPython doesn't have these. But we do know that MicroPython uses the standard log₂x = log(x)/log(2.0) identity with limited precision floating point values. And that's one of the OP's problems. That, and using int() when one really means round().

Statistics: Posted by scruss — Mon Apr 28, 2025 4:53 pm



Viewing all articles
Browse latest Browse all 6954

Trending Articles