[1.1.91] Floating point precision loss with some Blueprint string fields
Posted: Fri Sep 22, 2023 5:43 pm
I'm writing a JSON-schema compliant description of the Blueprint string format. To be thorough, I'm testing the numeric limits of each attribute to determine their underlying datatype (or, at least their acceptable range of values). Both `entity_number` and `version` (when set outside their allowed range) describe their limits as a unsigned 64-bit integer (0 -> 2^64-1), but you can actually set values for both that exceed the upper bound:
Despite the above values being outside of the allowed range, the following blueprint will import into Factorio just fine:
It seems like these numbers are getting converted to a floating point number at some step, making them suffer from floating point precision error. If I set the value to 2^65, the value does error, but the returned value in the error varies from the input value by about a thousand:

The output string also seems to display the interpreted result with decimal values, further corroborating this interpretation. I suspect this coercion might be caused by the blueprint string entering the Lua side of things and getting coerced to a `number`, which is likely a C double.
If this is a bug, it's impact is non-existent; no player is ever going to have a blueprint with 2^53 entities in it, and the Factorio major version would have to be absurdly high for version to suffer from precision loss. However, this very well seems like it could be a simple oversight where an input value is being coerced to the wrong datatype accidentally, making the tooltip which describes it's valid range strictly incorrect at the very least.
Code: Select all
test_dict = {
"blueprint": {
"item": "blueprint",
"entities": [
{
"name": "wooden-chest",
"position": {"x": 0.5, "y": 0.5},
"entity_number": 2**64 + 1000
}
],
"version": 2**64 + 1000
}
}
It seems like these numbers are getting converted to a floating point number at some step, making them suffer from floating point precision error. If I set the value to 2^65, the value does error, but the returned value in the error varies from the input value by about a thousand:

Code: Select all
36893488147419103232 (2**65)
36893488147419104982.000000 (returned)
If this is a bug, it's impact is non-existent; no player is ever going to have a blueprint with 2^53 entities in it, and the Factorio major version would have to be absurdly high for version to suffer from precision loss. However, this very well seems like it could be a simple oversight where an input value is being coerced to the wrong datatype accidentally, making the tooltip which describes it's valid range strictly incorrect at the very least.