Skip to content

PHP "float" serialization loses precision #8165

@bshaffer

Description

@bshaffer

In both the protobuf native PHP library and PHP C-extension, I am seeing a loss of precision with floats:

message TestMessage {
  float foo = 1;
}
$initialMessage = new TestMessage();
$initialMessage->setFoo(-8.6107481E7);
echo "Initial message: ";
var_dump($initialMessage->getFoo());

echo "Serialized message: ";
$serializedMessage = new TestMessage();
$serializedMessage->mergeFromString($initialMessage->serializeToString());
var_dump($serializedMessage->getFoo());

When the C-Extension is enabled, the loss of precision happens immediately. When the C-extension is not enabled, the loss of precision happens when the message is serialized and deserialized:

// Without protobuf C-Extension
Initial message: float(-86107481)
Serialized message: float(-86107480)

// With protobuf C-Extension
Initial message: float(-86107480)
Serialized message: float(-86107480)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions