Most web applications deal with decimal numbers at some stage — currency, coordinates, measurements, scientific arithmetic — but PHP does not provide a standard or clear recommendation for dealing with numbers when accuracy is important. There are of course cases where accuracy is not important, or at least not as important as performance.

PHP’s float is an IEEE 754–1985 double under the hood, which is a binary floating-point number, and is therefore not capable of representing certain numbers, such as 0.2. This is because the value is usually stored as an integer multiplied by 2 to the power of an exponent: a * 2^b = c, where a and b are integers. When there are no integer solutions for a and b, the resulting number will be an approximation. This results in counter-intuitive behavior:

The number system we are taught in school and use in our day-to-day lives is of course decimal. When we use float, we are using a binary type to represent a decimal number, because we can perform binary calculations very quickly with hardware. To calculate a power of 2, all we need to do is set a single bit:

In order to represent decimal numbers accurately, we would need to use a power of 10 instead of 2. Unfortunately, most architectures do not support native base-10 arithmetic, so we would need to calculate that power.

The trade-off here is performance for accuracy.

When accuracy is important, this trade-off is easy to make because you can allocate more CPU power to offset the performance hit, but you can not change the underlying architectural limitations. Most PHP applications will have a bottleneck at the filesystem or network layer anyway. ¯\_(ツ)_/¯