Does anyone know why floor(61681) = 61681, but floor(616.81*100) = 61680 ?
I have tried many others values like
floor(716.81*100) = 71681
floor(816.81*100) = 81681
floor(916.81*100) = 91681
floor(616.83*100) = 61683
Anyone know why this happened?
Because of the nature of binary floating-point numbers, internal rounding inaccuracies will occur. There is no way to represent
616.81as a binary floating-point number exactly – its closest approximation is in fact slightly less than616.81.61681can be represented exactly, however.When the representation of
616.81is multiplied by100, the result is a tiny bit less than61681, and thus callingflooron it will return61680.http://codepad.org/H0wh2itg
If you want absolute precision, you can use PHP’s BC math functions, which can be as precise as you need at the cost of performance. For example: