Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP floating point precision: Is var_dump secretly rounding and how can I debug precisley then?

That floating point numbers in PHP are inaccurate is well known (http://php.net/manual/de/language.types.float.php), however I am a bit unsatisfied after the following experiment:

var_dump((2.30 * 100)); // float(230)
var_dump(round(2.30 * 100)); // float(230)
var_dump(ceil(2.30 * 100)); // float(230)
var_dump(intval(2.30 * 100)); // int(229)
var_dump((int)(2.30 * 100)); // int(229)
var_dump(floor(2.30 * 100)); // float(229)

The internal representation must be something like 229.999998.

var_dump((2.30 * -100)); // float(-230)
var_dump(round(2.30 * -100)); // float(-230)
var_dump(ceil(2.30 * -100)); // float(-229)
var_dump(intval(2.30 * -100)); // int(-229)
var_dump((int)(2.30 * -100)); // int(-229)
var_dump(floor(2.30 * -100)); // float(-230)

The internal representation must be something like -229.999998.

Ok as far as I understand - integer casting as well as the intval function simply cut of everything behind the point. Good to know.

However var_dump() gives me a value of 230 though the real value must be different according to those results.

Now have a look at this:

$a = 230.0;

var_dump($a); // float(230)
var_dump((int) $a); // int(230)

That means the internal representation of the floating point number must be different here. If I want to know the exact value of a float therefore I can not debug using var_dump as I am used to right? How can I debug an exact float value?

like image 711
Blackbam Avatar asked Apr 23 '18 12:04

Blackbam


1 Answers

You can try to use number_format it won't be perfect since you have to provide number of decimals, but should help.

echo number_format(8-6.4, 50);

1.59999999999999964472863211994990706443786621093750

echo number_format(2.3*100, 50);

229.99999999999997157829056959599256515502929687500000

Edit: As the number of decimal places is varying (this also depends on the system used) the following might be useful - gets the full number for sure and removes trailing zeros:

echo rtrim(number_format(1.0/3.432, 100),0);

0.29137529137529138978379705804400146007537841796875

like image 111
mleko Avatar answered Oct 12 '22 14:10

mleko