Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different behavior with float precision #4569

Closed
twmht opened this issue Apr 27, 2018 · 3 comments
Closed

Different behavior with float precision #4569

twmht opened this issue Apr 27, 2018 · 3 comments
Assignees

Comments

@twmht
Copy link

twmht commented Apr 27, 2018

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

@zchrissirhcz
Copy link

zchrissirhcz commented May 12, 2018

In my case, I use pycaffe to read, modify and write 'xxx.prototxt' file. Similar as metioned in this issue #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.

@anandolee
Copy link
Contributor

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.

@anandolee
Copy link
Contributor

anandolee commented May 21, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants