Protobuf: Different behavior with float precision

Created on 27 Apr 2018  路  3Comments  路  Source: protocolbuffers/protobuf

I found the difference when dealing with float point between different version of python-protobuf (2.6.1 vs 3.5.0.post1)

test.proto

message A {
  optional float a = 1;
}
$ protoc --version # libprotoc 2.6.1
protoc test.proto --python_out=./

python-protobuf 2.6.1

A = test_pb2.A()
A.a = 0.01
print A.__str__() # a: 0.01

python-protobuf 3.5.0.post1

A = test_pb2.A()
A.a = 0.01
print A.__str__() # a: 0.00999999977648

Not sure if it's a bug or not.

What makes the difference?

Thank you

python question

Most helpful comment

In my case, I use pycaffe to read, modify and write 'xxx.prototxt' file. Similar as metioned in this issue https://github.com/google/protobuf/issues/3632, by setting the python protobuf's backend to python instead of the default cpp (I guess, please correct me if I am wrong), the float numbers in saved prototxt has the exactly same precision as expected:

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
python my_code.py

@twmht Now I use python2.7.12 with protobuf 3.5.2, and this environment setting also works for the testing code you put in this issue.

All 3 comments

In my case, I use pycaffe to read, modify and write 'xxx.prototxt' file. Similar as metioned in this issue https://github.com/google/protobuf/issues/3632, by setting the python protobuf's backend to python instead of the default cpp (I guess, please correct me if I am wrong), the float numbers in saved prototxt has the exactly same precision as expected:

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
python my_code.py

@twmht Now I use python2.7.12 with protobuf 3.5.2, and this environment setting also works for the testing code you put in this issue.

zchrissirhcz@ is right. We have a known issue for float type precision if it is using cpp extension:

Python does not have C-style float , it only has a C-style double. Thus pure python is using double precision for both float and double field, cpp extension is using float precision for float field.

Closing it for cleaning up. Feel free to reopen if you still have questions

Was this page helpful?
0 / 5 - 0 ratings