fmod(floatval("314.6"), floatval("1.3"))
=> 1.1990408665952E-14
I understand more or less the underlying problem of representing the numbers in binary form and something with IEEE-754. But: How would I get a result that is usable in pratice?
Example what I want to achieve: A merchant can define the price P of his product, but the price has to be a multiple of x:
if (fmod(P, x) != 0) { echo "price must be a multiple of " . x; }
It would be so simple, but the approach fails whenever I get something like 1234E-18 as return value of fmod()
.
What to do in real life to check the price interval easily without using custom code?
This or similar questions have been around, but all I can find are explanations why fmod()
behaves like it does. Never an answer how to solve this real-life problem...