Bug #78688 [Nab]: Invalid float serialization
Edit report at https://bugs.php.net/bug.php?id=78688&edit=1
ID: 78688
User updated by: eirik at nymedia dot no
Reported by: eirik at nymedia dot no
Summary: Invalid float serialization
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Linux/OSX
PHP Version: 7.3.10
Block user comment: N
Private report: N
New Comment:
Thanks for responding! I appreciate that. However, I do find it strange that this provides different
results:
var_export(440.0);
print PHP_EOL;
var_export((float) "440.0");
print PHP_EOL;
var_export("44.0" * 10);
print PHP_EOL;
var_export("0.44" * 1000);
print PHP_EOL;
var_export("4.4" * 100);
print PHP_EOL;
var_export("4.400000" * 100);
print PHP_EOL;
This prints
440.0
440.0
440.0
440.0
440.00000000000006
440.00000000000006
And speaking about string representation. I do not know the internals of php at all, but for all of
the above, using other functions like print_r, var_dump and so on - even casting them to string,
produces the same result for all of the floats.
And to add to this, replacing the float 440.0 with something else. Say 666.0. Produces the expected
output.
Just wanted to add this to the conversation. I am not well enough versed in internals and floating
points to say if the above is also expected, so feel free to keep as "Not a bug" if it
fits the description well :)
Previous Comments:
------------------------------------------------------------------------
[2019-10-18 11:39:08] requinix@php.net
Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.
If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/
Thank you for your interest in PHP.
------------------------------------------------------------------------
[2019-10-18 11:33:58] eirik at nymedia dot no
Description:
------------
I noticed when encoding a json object that the float 440.0 was not encoded correctly. So I looked
into it, and when the float 440.0 is constructed with the following multiplication it fails to
serialize, var_export and json_encode properly (and probably others):
$var = "4.400000" * 100;
Test script:
---------------
$var = "4.400000" * 100;
var_export($var);
print PHP_EOL;
print json_encode($var);
print PHP_EOL;
print serialize($var);
print PHP_EOL;
// Output:
// 440.00000000000006
// 440.00000000000006
// d:440.00000000000006;
Expected result:
----------------
440.0
440
d:440;
Actual result:
--------------
440.00000000000006
440.00000000000006
d:440.00000000000006;
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78688&edit=1
Thread (4 messages)