Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127019 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 32B9A1A00BC for ; Tue, 1 Apr 2025 20:17:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1743538483; bh=yNdya6aL6NnPddvvELjopqstrE7a9yVCtBw8/Ws1wmQ=; h=Date:From:To:Subject:In-Reply-To:References:From; b=aCpLBWATCQFyMrg5d4+s+WBBdnPfvx0RsIXtlDDSJj9H4OU/LLhSXo9dm7GKptRGS otT/rORYOm7YBEQCG8NG1HBE5Ftci4qXxdsXghoYL9yCfaxO3fWert4FT1G8dagsr+ a1P2oFYvLdZLJLwdmIz0DX/7D3tyCPG6gck6cxHSzcpWxKKQCd1SC0uroRugvaFvEa x49Gl01cC3aaub157TM1UtXO8g6rjBmkFmxKCUDfh9BsHFSGBjlrp+CmfHaTBtM9Rq WxWPy9Vrdxh2buA8ncS9ffupuVyEi/wHnffzC/GlKjCRmRy5J+ciNd7oMtfSuPEus6 bzIwPN7sJx0eA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9B0151804F4 for ; Tue, 1 Apr 2025 20:14:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: *** X-Spam-Status: No, score=3.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_PASS, SPF_SOFTFAIL autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from xdebug.org (xdebug.org [82.113.146.227]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 1 Apr 2025 20:14:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1743538627; bh=yNdya6aL6NnPddvvELjopqstrE7a9yVCtBw8/Ws1wmQ=; h=Date:From:To:Subject:In-Reply-To:References:From; b=BEtjH4ZCUUpE+Q5eackLvT512YB+G8dzL+HpqPlwOewq693fAimfUt4lhwrPw7fUN vWjkz25IG6OBccF7ydFCehlfv8TOyI+7KUWlxLUAR7KfBOIpstQB2e/c8osivgPwJB N1LNIi+TKJxyetc2Zj3m7MkKG46SQQN3J/UmdY7S8ibh6ovtX4f5E34NaiwJ7/tNgr TrNAKkIqZuHLvfty6FtAR6zgOooSECjVbL4KpHvHEixIzOv7DZWGHY9o2sGQiIicti EDUO3jKHFbcZOTS71RNeh11rLOLo1xOzKYw3FkyX/bobtSFKewKDNWWKPFkjQlWCFy xvbNBv9dIhgzA== Received: from [127.0.0.1] (host109-150-25-108.range109-150.btcentralplus.com [109.150.25.108]) by xdebug.org (Postfix) with ESMTPSA id F068F10C027; Tue, 01 Apr 2025 21:17:06 +0100 (BST) Date: Tue, 01 Apr 2025 21:17:05 +0100 To: internals@lists.php.net, Larry Garfield Subject: Re: [PHP-DEV] [RFC brainstorm] Approximately equals operator User-Agent: K-9 Mail for Android In-Reply-To: <799bd54a-2136-4410-823a-9233ccbeb16a@app.fastmail.com> References: <4a3c6ce7-102d-4cfe-a7a8-35630715b870@gmail.com> <799bd54a-2136-4410-823a-9233ccbeb16a@app.fastmail.com> Message-ID: <89495A22-BCBD-4779-835D-56B90257FF51@php.net> Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: derick@php.net (Derick Rethans) On 1 April 2025 20:52:32 BST, Larry Garfield wro= te: >On Mon, Mar 31, 2025, at 5:03 PM, Niels Dossche wrote: >> Hi internals! >> >> I'm excited to share what I've been working on! >> I had an epiphany=2E I realized what we truly need to revolutionize PHP= :=20 >> a new operator=2E >> >> Hear me out=2E >> We live in an imperfect world, and we often approximate data, but=20 >> neither `=3D=3D` nor `=3D=3D=3D` are ideal comparison operators to deal= with=20 >> these kinds of data=2E >> >> Introducing: the "approximately equal" (or "approx-equal") operator=20 >> `~=3D` (to immitate the maths symbol =E2=89=83)=2E >> This combines the power of type coercion with approximating equality=2E >> Who cares if things are actually equal, close enough amirite? >> >> First of all, if `$a =3D=3D $b` holds, then `$a ~=3D $b` obviously=2E >> The true power lies where the data is not exactly the same, but "close = enough"! >> >> Here are some examples: >> >> We all had situations where we wanted to compare two floating point=20 >> numbers and it turns out that due to the non-exact representation,=20 >> seemingly-equal numbers don't match! Gone are those days because the=20 >> `~=3D` operator nicely rounds the numbers for you before comparing them= =2E >> This also means that the "Fundamental Theorem of Engineering" now holds= ! >> i=2Ee=2E 2=2E7 ~=3D 3 and 3=2E14 ~=3D 3=2E Of course also 2=2E7 ~=3D 3= =2E14=2E But this is=20 >> false obviously: 2 ~=3D 1=2E >> >> Ever had trouble with users mistyping something? Say no more! >> "This is a tpyo" ~=3D "This is a typo"=2E It's typo-resistant! >> However, if the strings are too different, then they're not=20 >> approx-equal=2E >> For example: "vanilla" ~=3D "strawberry" gives false=2E >> How does this work? >> * The strings are equal if their levenshtein ratio is <=3D 50%, so it's= =20 >> adaptive to the length=2E >> * If the ratio is > 50%, then the shortest string comes first in the=20 >> comparison, such that if we ever get a `~<` operator, then "vanilla" ~<= =20 >> "strawberry"=2E >> >> There is of course a PoC implementation available at:=20 >> https://github=2Ecom/php/php-src/pull/18214 >> You can see more examples on GitHub in the tests, here is a copy: >> ```php >> // Number compares >> var_dump(2 ~=3D 1); // false >> var_dump(1=2E4 ~=3D 1); // true >> var_dump(-1=2E4 ~=3D -1); // true >> var_dump(-1=2E5 ~=3D -1=2E8); // true >> var_dump(random_int(1, 1) ~=3D 1=2E1); // true >> >> // Array compares (just compares the lengths) >> var_dump([1, 2, 3] ~=3D [2, 3, 4]); // true >> var_dump([1, 2, 3] ~=3D [2, 3, 4, 5]); // false >> >> // String / string compares >> var_dump("This is a tpyo" ~=3D "This is a typo"); // true >> var_dump("something" ~=3D "different"); // false >> var_dump("Wtf bro" ~=3D "Wtf sis"); // true >> >> // String / different type compares >> var_dump(-1=2E5 ~=3D "-1=2Ea"); // true >> var_dump(-1=2E5 ~=3D "-1=2Eaaaaaaa"); // false >> var_dump(NULL ~=3D "blablabla"); // false >> ``` >> >> Note that this does not support all possible Opcache optimizations=20 >> _yet_, nor does it support the JIT yet=2E >> However, there are no real blockers to add support for that=2E >> >> I look forward to hearing you! >> >> Have a nice first day of the month ;) >> Kind regards >> Niels > >Naturally, the degree of closeness for strings or for floats should be co= ntrolled by an ini setting=2E Maximum flexibility! > >--Larry Garfield You got to be joking! Everybody knows ini settings make things unportable= =2E I suggest we introduce AI to determine the closeness instead=2E cheers Derick