Skip to content

Accessing float field gives different value than provided #515

@aabmass

Description

@aabmass

I had a look around but couldn't find an existing issue. There is some strange rounding with float fields when the value is accessed on an instance vs encoded:

>>> import proto
...
... class Animal(proto.Message):
...     size = proto.Field(proto.FLOAT, number=1)
...
>>> a = Animal(size=0.95)
>>> a
size: 0.95

>>> a.size
0.949999988079071

Why is a.size significantly different from the value I provided? Also the textproto produced by stringifying and the value produced by to_dict() are correct:

>>> Animal.to_dict(a)
{'size': 0.95}
>>> Animal.to_dict(a)["size"] == a.size
False

Environment details

  • Programming language: Python 3.13
  • OS: Linux
  • Language runtime version: 3.13
  • Package version: proto-plus==1.25.0

Steps to reproduce

>>> import proto
...
... class Animal(proto.Message):
...     size = proto.Field(proto.FLOAT, number=1)
...
>>> Animal(size=0.95)
size: 0.95

>>> Animal(size=0.95).size
0.949999988079071

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions