-
Notifications
You must be signed in to change notification settings - Fork 15.5k
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
Is negative zero a default value for float/double fields? #7062
Comments
The desired behavior should probably be included in conformance tests for binary, text format, and JSON serializations. |
CC @gerben-s who was looking into this problem a couple months ago |
While we're at it, I wonder what we do with NaN and Inf? |
We ran into this issue while working on a Java-based mutator for Protobuf messages and have to rely on rather hacky workarounds. Is there a plan to settle this edge case? |
NaN and Inf are transferred correctly in all cases already. There's no problem there. The problem with
Or put different, is the default |
While |
In other words, all |
Over to @Logofile to work into our official docs |
There is an important case in which Is this still expected behavior? I would have expected |
I would have expected that as well. Honestly, this particular corner of our semantics is likely under covered in tests |
Let me add a quick quote from the specs here: https://protobuf.dev/programming-guides/encoding/
If we take this part of the spec at face value memcpy of the equivalent C types ... double) than all the behaviors that do not preserve the bit pattern are "wrong". Edit: However, it would appear that this is orthogonal to the default-values-are-not-serialized rule. Also, in Dec 2023 notes have been added to the spec (ln 522, 539) to clarify the +/- 0.0 behavior: https://protobuf.dev/programming-guides/proto3/#default :
Note that any |
As of 2024-02-13, this was changed:
I cannot tell if or when this has become true in which official protobuf libraries, or if or when this has existed in any part of the conformance tests. There have been at least some unit tests since 2021. it also seems to work in c++ at present... |
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment. This issue is labeled |
This issue should be closed as "answered"? There is the commit protocolbuffers/protocolbuffers.github.io@025f350 from that changed the current docs to the quote already mentioned above:
|
As mentioned, the text "For float and double types, -0.0 and 0.0 are treated as equivalent, and will round-trip" and "[i]f a |
Ummm.... so which is it? The current docs say:
but @Logofile just above claimed
|
Apologies: that additional text was removed in a later change. (I was looking at an earlier version of the If a float or double value is set to +0 it will not be serialized, but -0 will be serialized. |
Excellent! I think that's the behavior that most folks on this thread seem to have expected. Now that we have it documented, we can make that consistent across implementations. |
This question recently came up in discussion of apple/swift-protobuf#935:
Proto3 omits any field whose value is the default. For float and double fields, this means that zero values are not serialized. Does this apply to both negative and positive zero? Or should negative zero be serialized in order to ensure that all float and double values can be accurately communicated?
The text was updated successfully, but these errors were encountered: