Accurate representation of some floating numbers is not just hard but impossible, with 0.1 being a well known example of that.This is a complete or consistent problem. Representing floating point is hard.
So we have to accept that 0.1 + 0.2 will often give a result of 0.30000000000000004
I don't have a problem with that. The issue IMO comes down to whether str(0.1 + 0.2) should deliver a "0.30000000000000004" or "0.3" result. I believe it should be the first. The first is an accurate representation of what value it actually held, while "0.3" is not.
It seems CPython exhibits similar behaviour so I'd also class that as a bug, funky unexpected behaviour -
Code:
>>> 0.1 + 0.20.30000000000000004Code:
>>> "{:.64f}".format(0.1 + 0.2).rstrip("0")'0.3000000000000000444089209850062616169452667236328125'This appears to work for both CPython and MicroPython, delivering what I want to see for 'str()' but not exhaustively tested -
Code:
def FloatToString(f): s = "{:.64f}".format(f).rstrip("0") if s.endswith("."): s += "0" return sstr = FloatToStringStatistics: Posted by hippy — Mon Apr 28, 2025 7:30 pm