Re: [RFC brainstorm] Approximately equals operator

From: Date: Tue, 01 Apr 2025 08:56:56 +0000
Subject: Re: [RFC brainstorm] Approximately equals operator
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
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

Thread (17 messages)

« previous php.internals (#126996) next »