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

Floats in ruby/protobuf are not in fact equal #16807

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

shawnfrench
Copy link

@shawnfrench shawnfrench commented May 9, 2024

The existing test is misleading:

m.optional_float = 0.5
assert_equal 0.5, m.optional_float

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 shawnfrench requested a review from a team as a code owner May 9, 2024 18:20
@shawnfrench shawnfrench requested review from JasonLunn and removed request for a team May 9, 2024 18:20
@JasonLunn JasonLunn added 🅰️ safe for tests Mark a commit as safe to run presubmits over ruby labels May 9, 2024
@github-actions github-actions bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label May 9, 2024
@JasonLunn JasonLunn added the 🅰️ safe for tests Mark a commit as safe to run presubmits over label May 9, 2024
@github-actions github-actions bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label May 9, 2024
@JasonLunn
Copy link
Contributor

JasonLunn commented May 9, 2024

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?

@haberman
Copy link
Member

@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.

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

Successfully merging this pull request may close these issues.

None yet

3 participants