protobuf does not encode and decode double value correctly

36 views Asked by At
  • protobuf Version: libprotoc 27.0-dev (obtained with command "protoc --version")
  • Language version: proto3
  • programming language: C++

Details of issue:

When "1.0002" is set to one of the repeated value (variable type is double) and sent through UDP, the listener node receive "4.45015e-312" as a value and the length of the repeated value is 1 even if sender sends three values (all of their values are 1.0002). However, everything works correctly when 1.003 is sent. Why protobuf decodes 1.0002 does not work right when 1.002 is sent?

(message is serialized by SerializeToString)

Messsage definition:

syntax = "proto3";
message Motor{
    message Feedback{
        double vel = 1;
        double pos = 2;
    }
    repeated Feedback feedback = 1;
}


Talker:

void sendMotorFdbk() {
    Motor motor_msg;
    motor_msg.clear_feedback();
    Motor::Feedback *feedback_handler;
    for(int i=0; i<3;i++){
        feedback_handler = motor_msg.add_feedback();
        feedback_handler->set_pos(1.0002);
        feedback_handler->set_vel(0.123456789);
    }
    std::string array1;
    motor_msg.SerializeToString(&array1);
    int buffer_length=strlen(array1.c_str());
    int sent_byte = sendto(sockfd_snd_, array1.data(), buffer_length, 0, (const struct sockaddr *)&sendingFdbkTo_, sizeof(sendingFdbkTo_));
    if(sent_byte != buffer_length){
        std::cout<<"sending message failure "<<errno<<std::endl;
        exit(EXIT_FAILURE);
    }
}

Listener:

void rcvFeedBackMes() {
Motor motor;
    int data;
    socklen_t len;
    len = sizeof(clientaddr_fdbk);
    data = recvfrom(sockfd_rcv_fdbk, (char *)rcv_buffer, sizeof(rcv_buffer), 0, (struct sockaddr *) &clientaddr_fdbk, &len);
    if(data > 0 ){
        motor.ParseFromString(rcv_buffer);
        char *strAdd = inet_ntoa(clientaddr_fdbk.sin_addr);

        for(int i=0; i< motor.feedback_size(); i++){
std::cout<<"[receve message] pos: "<<motor.feedback()[i].pos()<<" "<<strAdd<<std::endl;
        }
    }

  • I tried to send different numbers. 1.0 is decoded as "0" and "1.01" is decoded correctly
  • Protobuf is installed by following the instruction for building from source using CMake and main branch
  • message definition (.proto) is compiled with the following commands protoc -I=./src/ --cpp_out=./dest/ ./src/motorcontrol.proto
0

There are 0 answers