gcc float-equal error

397 views
Skip to first unread message

Seth Humphries

unread,
Mar 3, 2016, 1:42:00 PM3/3/16
to Protocol Buffers
I am using proto 3.0.0-beta-1 with a proto file that looks like this:

```
syntax = "proto3";

message SomeMsg {
string query = 1;
int32 integer = 2;
float nothernum = 3;
}
```

I am using gcc 4.9.2 with cmake 3.4.0. I use cmake to enable all gcc warnings and treat all warnings as errors.

I get the follow errors
```
error: comparing floating point with == or != is unsafe [-Werror=float-equal]
if (this->nothernum() != 0) {
                                  ^
```
and
```
error: comparing floating point with == or != is unsafe [-Werror=float-equal]
if (from.nothernum() != 0) {
                                  ^
```

I have included the proto file in my hpp like ``` #include <SomeMsg.pb.h>``` and still get the error.

I can suppress the float-equal error but I would prefer to leave all warnings on. The error goes away also if I use proto2 and make everything optional.

Anyone have other ideas of how to resolve this without suppressing the warning and still use proto3?

Seth Humphries

unread,
Mar 4, 2016, 7:42:54 PM3/4/16
to Protocol Buffers
I forgot to add that this occurs after proto buffer generates the pb.h and pb.c files. This error occurs inside the generated files when I attempt to compile them.  ie the error gives the .pb.c file and line number.

Feng Xiao

unread,
Mar 7, 2016, 5:41:49 PM3/7/16
to Seth Humphries, Protocol Buffers
Hi Seth,

Could you help submit this as a github issue? https://github.com/google/protobuf/issues

Do you know how we can modify the generated code to avoid this "float-equal" warning? If you know of a solution, feel free to send us a patch to fix the code:

On Fri, Mar 4, 2016 at 4:42 PM, Seth Humphries <set...@gmail.com> wrote:
I forgot to add that this occurs after proto buffer generates the pb.h and pb.c files. This error occurs inside the generated files when I attempt to compile them.  ie the error gives the .pb.c file and line number.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Seth Humphries

unread,
Mar 7, 2016, 6:53:01 PM3/7/16
to Protocol Buffers, set...@gmail.com
I will post as an issue if the discussion fails to shed more light on this

In syntax proto2, I mark each element as optional. In proto3, all elements are considered optional by default. The error seems to be from proto3 improperly checking for element existence. 

Feng Xiao

unread,
Mar 7, 2016, 7:23:34 PM3/7/16
to Seth Humphries, Protocol Buffers
On Mon, Mar 7, 2016 at 3:53 PM, Seth Humphries <set...@gmail.com> wrote:
I will post as an issue if the discussion fails to shed more light on this

In syntax proto2, I mark each element as optional. In proto3, all elements are considered optional by default. The error seems to be from proto3 improperly checking for element existence. 
In proto2, when a message is serialized, the serialization routine will check the has-bits to determine whether a field is set and only serialize set fields. However, in proto3, we removed has-bits, so the serialization routine can't check the has-bits. As a workaround, we instead check whether the field is set to a non-zero value. That's why proto3 has these "field == 0" comparisons but proto2 doesn't. They are there intentionally, but we may be able to write it in a way that can avoid the float-equal warnings.

Bo Yang

unread,
Mar 7, 2016, 8:10:47 PM3/7/16
to Seth Humphries, Protocol Buffers
You mentioned "The error goes away also if I use proto2 and make everything optional.".
proto2 and proto3 has the same API for float fields. I don't think using proto2 is the reason to make the error go away. 
Is it possible you disabled float waring when using proto2?

On Fri, Mar 4, 2016 at 4:43 PM Seth Humphries <set...@gmail.com> wrote:
I forgot to add that this occurs after proto buffer generates the pb.h and pb.c files. This error occurs inside the generated files when I attempt to compile them.  ie the error gives the .pb.c file and line number.

--

Bo Yang

unread,
Mar 7, 2016, 8:10:48 PM3/7/16
to Seth Humphries, Protocol Buffers
if (this->nothernum() != 0) {
                                  ^

Do you mean this code is in proto's generated code instead of your own code?

Bo Yang

unread,
Mar 7, 2016, 8:10:50 PM3/7/16
to Seth Humphries, Protocol Buffers
It doesn't seem like a problem of protobuf. Could you double-check you are using the same configuration for compiling?

--

Halil Şen

unread,
Apr 8, 2019, 12:11:02 PM4/8/19
to Protocol Buffers

On Tuesday, March 8, 2016 at 1:23:34 AM UTC+1, Feng Xiao wrote:
In proto2, when a message is serialized, the serialization routine will check the has-bits to determine whether a field is set and only serialize set fields. However, in proto3, we removed has-bits, so the serialization routine can't check the has-bits. As a workaround, we instead check whether the field is set to a non-zero value. That's why proto3 has these "field == 0" comparisons but proto2 doesn't. They are there intentionally, but we may be able to write it in a way that can avoid the float-equal warnings.

Hi,

I am sorry to revive this old topic; however, I have the described problem (i.e., proto generated files giving a warning when compiled with -Wfloat-equal).

I managed to have the same check using std::fpclassify(field) == FP_ZERO (instead of field == 0) which avoids float-equal warning. However, it includes <cmath> if that is undesirable, equality for floats fields can be checked with two inequalities "field <= 0 && field >= 0" which again avoids float-equal warning.

I would be happy to send a PR if you think it is appropriate.

Best,
Halil

Halil Şen

unread,
Apr 9, 2019, 3:16:25 AM4/9/19
to Protocol Buffers
I went ahead and created the following patch https://github.com/protocolbuffers/protobuf/pull/6000


On Monday, March 7, 2016 at 11:41:49 PM UTC+1, Feng Xiao wrote:
Hi Seth,

Could you help submit this as a github issue? https://github.com/google/protobuf/issues

Do you know how we can modify the generated code to avoid this "float-equal" warning? If you know of a solution, feel free to send us a patch to fix the code:
On Fri, Mar 4, 2016 at 4:42 PM, Seth Humphries <set...@gmail.com> wrote:
I forgot to add that this occurs after proto buffer generates the pb.h and pb.c files. This error occurs inside the generated files when I attempt to compile them.  ie the error gives the .pb.c file and line number.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prot...@googlegroups.com.

Seth Humphries

unread,
Apr 9, 2019, 11:25:55 PM4/9/19
to Protocol Buffers
I think Halil is correct and that the proposed solution provides a way around the compiler warning. Thank you for the fix. 

However, I am of the opinion that the design of the proto3 syntax is incorrect with respect to default values or values being absent. For example, I want to pass a float that represents probability [0 1]. If my program decides not to send the probability, how does the recipient know if the probability is really 0 or was not sent? With proto2 syntax, it was clear if a value was present or not. Using a sentinel value for this purpose is fine if it is not also one of the expected possible values. That said, discussing the pros and cons of the syntax was not part of the original thread and I will rant elsewhere about that. 
Reply all
Reply to author
Forward
0 new messages