Skip to content

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

@kuri65536

Description

@kuri65536

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions