python-for-android icon indicating copy to clipboard operation
python-for-android copied to clipboard

`str(0.1)` aka float.__format__ outputs are different to another platform

Open kuri65536 opened this issue 6 years ago • 0 comments

in x86 system

>>> str(0.1)
"0.1"

in py4a

>>> str(0.1)
'0.1000000000000000056'

This is not precision issue, this is float.format behavior difference. I couldn't find the this behavior is guaranteed at all platform in python documents.

I think this was caused by bionic library and python program combination but I did not have many time to solve and investigate this prlaboblem.

case

This problem is found and reported by @gaurav , When he try to use uncompyle6 library .

uncompyle6 contains below code

src = {"1.5": "somevalue", "1.6": "somevalue"}
key = (1.5, 1.6)
src[str(key[1])]
>>> cause KeyError: key '1.6000000000000000888' not found

work around

round the value up. don't use loose str, you can specify the format you want.

>>> "%3.1f" % 1.6
"1.6"

so previous example might be

>>> src = {"1.5": "somevalue", "1.6": "somevalue"}
>>> key = (1.5, 1.6)
>>> src["%3.1f" % key[1]]
"somevalue"

kuri65536 avatar Dec 30 '18 02:12 kuri65536