Tuesday, 18 December 2018

math - PHP float calculation error when subtracting

I have a very strange issue. If I subtract 2 float vars where one is the result of a mathematical operation I get a wrong value.



Example:



var_dump($remaining);
var_dump($this->hours_sub['personal']);
echo $remaining-$this->hours_sub['personal'];



This it the output:



float 5.4
float 1.4
5.3290705182008E-15


5.4-1.4 should be 4
If I add the two values the result is correct.




Where is my mistake?
It can not be a rounding issue.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...