The accuracy of PHP float calculate

Float is an inexact datatype (as all floating-point datatypes are), because you may lose precision when converting to and from binary. This is why you shouldn’t use floating-point arithmetic when you need high (exact) precision.

In PHP, check out BC Math or the GMP library. The latter will only work with integers, but it has high performance and sometimes it’s possible to convert to and from integers without losing precision.

Leave a Comment