Doc #66866 [Opn]: Same output for float numbers when using variable handling functions
Edit report at https://bugs.php.net/bug.php?id=66866&edit=1
ID: 66866
User updated by: jan dot kahoun at heureka dot cz
Reported by: jan dot kahoun at heureka dot cz
Summary: Same output for float numbers when using variable
handling functions
Status: Open
Type: Documentation Problem
Package: Variables related
Operating System: CentOS release 6.4 (Final)
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Yes, that's true. So maybe it will be better to have same value for setting the
"precision" and "serialize_precision" at default? I realy don't know why is
better to have lower precision when displaying value? What are the usecases? I thought that for
precision purpouses, there are relevant functions like round(), format_number(), ceil() and floor().
I'm not the only one who is facing with this strange behaviour. ;-)
Previous Comments:
------------------------------------------------------------------------
[2014-03-09 18:10:37] rasmus@php.net
echo/print() are used for debugging too all over the place. Having print_r() show a different value
would lead to the exact same confusion. The best we can do is do a better job explaining the
precision settings on the relevant doc pages.
------------------------------------------------------------------------
[2014-03-09 17:58:23] jan dot kahoun at heureka dot cz
But functions var_dump() and print_r() are often used for debuging purpouses. So if you want to see
what is actually in the variable you will not expect the rounded value. This is realy confusing.
I think, that this should be at least mentioned on the all doc pages of the functions var_dump(),
print_r() and var_export().
Thank you.
------------------------------------------------------------------------
[2014-03-09 17:31:44] rasmus@php.net
var_export() is fundamentally different from print_r() and var_dump() in that it is often used to
read data back in. As such it is closer to serialize() than the others which are display-only
functions. For display purposes PHP has the 'precision' ini setting and for
serialize-purposes we have serialize_precision. In you script, if you do:
echo ini_set('precision',ini_get('serialize_precision'));
At the top you should get the same value for all three.
But yes, this probably should be mentioned on the var_export doc page.
------------------------------------------------------------------------
[2014-03-09 17:21:23] jan dot kahoun at heureka dot cz
Description:
------------
I know that the floating point values have a limited precision and hence a value might not have the
same string representation after any processing. But it is possible to make var_dump(), print_r()
and var_export() functions to output same result for float numbers? Some times the different output
results leads to hard debuging ;-)
If this is not possible, please mention this "feature" in the documentation of the
functions.
Thank you.
Test script:
---------------
$x = 0.399999999999999855671006798729649744927883148193359375;
print_r($x);
var_export($x);
var_dump($x);
Expected result:
----------------
Same result for all functions:
print_r() = 0.399999999999999855671006798729649744927883148193359375
var_export() = 0.399999999999999855671006798729649744927883148193359375
var_dump() = float(0.399999999999999855671006798729649744927883148193359375)
Actual result:
--------------
print_r() = 0.4
var_export() = 0.399999999999999855671006798729649744927883148193359375
var_dump() = float(0.4)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66866&edit=1
Thread (5 messages)