1

Got really strange behaviour for quite simple arithmetical operation:

Here is what I do:

<?php

$a = 5;
$b = 4.8;
$c = 0.2;

echo '<pre>';
var_dump($a - $b - $c);
echo '</pre>';

echo '<pre>';
var_dump($a - ($b + $c));
echo '</pre>';

echo '<pre>';
var_dump($a - $c - $b);
echo '</pre>';
?>

And as I result I got the following:

float(1.6653345369377E-16)
float(0)
float(0)

The first line is not what I expected. Mathematically all 3 operations should result in 0. I've tried both casting the inputs as string or as float or double, but there is no difference.

I tried with multiple versions of php from 5.5 to 7.4 but that doesn't matter, the result is the same.

I resolved the problem already by using the second result approach, but still puzzled why this is happening.

Nik Chankov
  • 6,049
  • 1
  • 20
  • 30

0 Answers0