Hi
Am 2025-04-01 00:03, schrieb Niels Dossche:
We all had situations where we wanted to compare two floating point
numbers and it turns out that due to the non-exact representation,
seemingly-equal numbers don't match! Gone are those days because the
`~=` operator nicely rounds the numbers for you before comparing them.
This also means that the "Fundamental Theorem of Engineering" now
holds!
i.e. 2.7 ~= 3 and 3.14 ~= 3. Of course also 2.7 ~= 3.14. But this is
false obviously: 2 ~= 1.
Thank you for your proposal. I've tried it and I believe I found some
fundamental flaw in the current logic. Consider:
<?php
var_dump(1.499999 ~= 1.5);
var_dump(1.399999 ~= 1.4);
This currently prints:
bool(false)
bool(true)
which violates my expectations. If 1.499999 is approximately equal to
1.5 then 1.399999 should also be approximately equal to 1.4.
Best regards
Tim Düsterhus