PHP float calculation accuracy

$fooValue = 100.68; $cowValue = 100.67; $diffValue = $fooValue - $cowValue; if($diffValue <= 0.01) { echo("success"); } else { echo("error"); } 

This will show a โ€œmistakeโ€.

I know what to do in Java. But I am not good at PHP, especially with this calculation of things.

Please help me. I mean, how to succeed?

+4
source share
1 answer

Float is an inaccurate data type (like all floating point data types), because you may lose precision when converting to and from a binary file. That is why you should not use floating point arithmetic when you need high (accurate) accuracy.

In PHP, go to the BC Math or GMP library. The latter will work only with integers, but it has high performance, and sometimes it can be converted to and from integers without loss of accuracy.

+6
source