php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #42635 var_dump() should render more precise floats
Submitted: 2007-09-12 03:31 UTC Modified: 2014-03-09 17:01 UTC
Votes:5
Avg. Score:4.2 ± 0.7
Reproduced:5 of 5 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (40.0%)
From: daniel dot oconnor at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.4 OS: Irrelevant
Private report: No CVE-ID: None
 [2007-09-12 03:31 UTC] daniel dot oconnor at gmail dot com
Description:
------------
var_dump() does not show the actual value of floats; but rather performs rounding before rendering.

This can lead to hard to decipher loss of precision bugs. If you then use var_dump() to try to compare output, you aren't going to find the *actual* values of the numbers you are comparing.

For this reason, I'd like to ask var_dump() renders the complete representation of the number; where needed in scientific notation.
 

Reproduce code:
---------------
<?php
function variance($a, $b) {
    return (double)100.00 -  (double)(($a/$b) * 100.00);
}

var_dump(12.99);                   //12.99
var_dump(variance(87.01, 100.00)); //12.99

var_dump(variance(87.01, 100.00) - 12.99); //difference      <-5.3290705182008E-15>


Expected result:
----------------
---------- php ----------
float(12.99)
float(12.99) // should be 12.99 + -5.3290705182008E-15
float(-5.3290705182008E-15)

Output completed (0 sec consumed) - Normal Termination

Actual result:
--------------
---------- php ----------
float(12.99)
float(12.99)
float(-5.3290705182008E-15)

Output completed (0 sec consumed) - Normal Termination

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-03-09 16:14 UTC] jan dot kahoun at heureka dot cz
This bug is really annoying! I spend lot of time to find, that var_dump is rounding float values. It was reported 6 year ago, why this was not fixed yet?
 [2014-03-09 17:01 UTC] rasmus@php.net
-Status: Open +Status: Not a bug -Package: Feature/Change Request +Package: *General Issues
 [2014-03-09 17:01 UTC] rasmus@php.net
This is not a bug. Do a ini_set('precision',32) at the top of your script, for example, if you want a higher precision estimation of the float.
 [2014-03-09 17:23 UTC] jan dot kahoun at heureka dot cz
Sorry my mistake, i thougth that this is different bug :(
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 23:01:34 2025 UTC