protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Floats in ruby/protobuf are not in fact equal

Open shawnfrench opened this issue 1 year ago • 2 comments

The existing test is misleading: https://github.com/protocolbuffers/protobuf/blob/82e83ddc95330bbd988ba874ea5aa37d343d4490/ruby/tests/common_tests.rb#L39-L40

This change adds a new test making the difference between ruby and protobuf floats explicit.

Something else to consider is currently the assignment of a ruby float returns the float with ruby precision. Perhaps the setter should return the float with protobuf precision?

shawnfrench avatar May 09 '24 18:05 shawnfrench

Interesting; this test exposes a behavior gap between the JRuby and JRuby FFI variants; the FFI-based implementation (with the passing test) uses the same C-based implementation under the hood, but the legacy JRuby implementation is built on top of the Java Protobuf jar and fails.

@haberman - does Java Protobuf handle float precision differently, or is this specific to JRuby's use of it?

JasonLunn avatar May 09 '24 18:05 JasonLunn

@haberman - does Java Protobuf handle float precision differently, or is this specific to JRuby's use of it?

I suspect this is specific to JRuby's use.

I suspect that JRuby is caching the Ruby object in this.fields when the user calls m.optional_float = 0.5. Then m.optional_float is returning the user's cached object, which will have the original precision since it was not actually derived from the Java message's value.

haberman avatar May 10 '24 17:05 haberman

We triage inactive PRs and issues in order to make it easier to find active work. If this PR should remain active, please add a comment.

This PR is labeled inactive because the last activity was over 90 days ago. This PR will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Aug 09 '24 10:08 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this PR should remain active or becomes active again, please reopen it.

This PR was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar Aug 23 '24 10:08 github-actions[bot]