Bug #64760 [Com]: var_export() does not use full precision for floating-point numbers
Edit report at https://bugs.php.net/bug.php?id=64760&edit=1
ID: 64760
Comment by: whatthejeff@php.net
Reported by: cf0hay at gmail dot com
Summary: var_export() does not use full precision for
floating-point numbers
Status: Closed
Type: Bug
Package: Variables related
Operating System: Linux 64bit (PHP 64bit too)
PHP Version: 5.4.14
Assigned To: yohgaki
Block user comment: N
Private report: N
New Comment:
This change can break tests on some systems. See https://github.com/php/php-src/pull/525.
Previous Comments:
------------------------------------------------------------------------
[2013-10-29 11:29:37] yohgaki@php.net
Committed fix, but not auto closed. Closing
3cf2682083fc1c8635b02c4cf77bdf12c5e5da35
------------------------------------------------------------------------
[2013-10-23 09:09:26] yohgaki@php.net
The difference came from
var_export()
-------------------------------------------------------
case IS_DOUBLE:
tmp_len = spprintf(&tmp_str, 0,"%.*H", (int) EG(precision), Z_DVAL_PP(struc));
smart_str_appendl(buf, tmp_str, tmp_len);
efree(tmp_str);
break;-------------------------------------------------------
serialize()
-------------------------------------------------------
case IS_DOUBLE: {
char *s;
smart_str_appendl(buf, "d:", 2);
s = (char *) safe_emalloc(PG(serialize_precision), 1, MAX_LENGTH_OF_DOUBLE + 1);
php_gcvt(Z_DVAL_P(struc), PG(serialize_precision), '.', 'E', s);
smart_str_appends(buf, s);
smart_str_appendc(buf, ';');
efree(s);
return;
}
-------------------------------------------------------
var_export() uses precision=14 while serialize() uses precision=17 by default.
-------
[yohgaki@dev php-5.5]$ php -e precision=17 scr.php
error of serialize(): 0
error of var_export(): 0
-------
Since var_export() is better to behave like serialize(), var_export() is better to use
PG(serialize_precision) instead of PG(precision).
------------------------------------------------------------------------
[2013-05-02 12:12:44] cf0hay at gmail dot com
Description:
------------
var_export() truncates floating-point precision. serialize() does not have the same problem.
Test script:
---------------
<?php
$a = microtime(true);
echo "error of serialize(): ";
var_export($a - unserialize(serialize($a))); //This returns 0.
echo "\n";
echo "error of var_export(): ";
eval('$b = '.var_export($a,true).';');
var_export($a - $b); //Almost never 0.
echo "\n";
Expected result:
----------------
Exporting a floating-point value should generate enough digits to prevent precision loss. The script
above should always return 0 on the second case.
Actual result:
--------------
The difference between the original floating point value and the one exported differs. The error is
about 15 magnitude lower than the magnitude of the value itself.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64760&edit=1
Thread (7 messages)