Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127017 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 DDEA91A00BC for ; Tue, 1 Apr 2025 19:53:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1743537036; bh=iFI+nMnGc0Xh4WUddfzzHKk0WK2+EIm/UOcxA33KR2A=; h=Date:From:To:In-Reply-To:References:Subject:From; b=Az+/eGx+C3SFF8bx/uSHkJYUZpeK053fOhERm1Z/WQDst69DzDCHMEqu1loV6Z9j3 CltHgMC4XNoGVzn/e7Jn2ueUnLap10+FbL+d8HCXs69QjJ/Saztum8+YCS5mfFFnCk w77i3EjA8kH9164dXyklLj0OFB3Ufz2zSdCSfr4PAfj6DRPudUPvBAufsNe6Np419t /de6d2DdLGA80NYpQPg/ldkxs4Fjq76td1hwf8vzRX5KpWjtymhxi/7kMT1vBtt1Ct cdUlcV18z2RXNmaS1KYx1A9/F5IjUxfyEu7xP0imb23c4fWKm25bR6wvD5oL6rEWdQ 4RsZrsOewU5Zg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5A4451801D8 for ; Tue, 1 Apr 2025 19:50:35 +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=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b3-smtp.messagingengine.com (fhigh-b3-smtp.messagingengine.com [202.12.124.154]) (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 19:50:25 +0000 (UTC) Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfhigh.stl.internal (Postfix) with ESMTP id 763A825400AF for ; Tue, 1 Apr 2025 15:52:51 -0400 (EDT) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-04.internal (MEProxy); Tue, 01 Apr 2025 15:52:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to; s=fm2; t=1743537171; x=1743623571; bh=43+/yLyx35hSLTcw6sPvg nWQpBcmDqNTZV7Bmsj1i0o=; b=d6p9V2oBDrdCRCwbmGhFrhoS9kl9Ey5EuCGK4 9NUHOenlkVeGDElp8lTjn2+cSJTWd+t5lT5A9cS83621eG9lYATl4UYUDbV5Y4Oy t3tb9+Y8GfIQx/pbDwqOoovLwsCu+Ag3F8hQaiBgLxLpyJMmWR4zqzk9k6bgIpY4 J3JQmKQVN2PGhccp7ZTSAII3UStrW0sl6rwYnUFhidBITqvLmrPjNELfdH+SvjF3 M+rrGRDg4t7d+sYBUHNew7MOqaTvx4Qi9kXl1nJBAhEQigwHVfViHtd+D82tu8iD am8XEJYz7QthcPpdBTq83cEHRaL4BcWoookRg+YmP1l92/BMg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; t=1743537171; x=1743623571; bh=4 3+/yLyx35hSLTcw6sPvgnWQpBcmDqNTZV7Bmsj1i0o=; b=cvqSKUwgo3mj5DL12 Qrc79DI1X8Ymc+gYl5KiaCoDt1ZT22Cx7aJDxqHekdXiqwBxeZEAbOA8O7vlUn5c LWZCowrdXTmdJVGKJM95O1hzkEs3SEJvoGoCbYNJkJ6f787S0o00YWivt2r/cKhb Z5OKrZeijvmPKWUP1EMb34TYDt6dzLX5XX9PySKJOVLn9aimsRn7fyWHa3BVWeN5 XhZbckxVckOlymFllH6eqXWv/zJP+tfGwHpuWlWLnPZReDZumilqxpxQnHsoqIe5 tmzPUeqBe3saDbfOmJKa2N3QwTV8BTk7l4Fn/rczRk8qcBllcij3UIsDK6cOOpZP FiZBA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddukeefieejucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepofggfffhvffkjghfufgtgfesthhqredtredt jeenucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrh hfihgvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpeehjeefvefgfeduteff ffdvheeiudekieefleevvdduiefgkeehvdevheffvdegteenucffohhmrghinhepghhith hhuhgsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhf rhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhmpdhnsggprhgtphhtth hopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhssehl ihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 108DB29C006F; Tue, 1 Apr 2025 15:52:51 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: T316fce6ada98c66b Date: Tue, 01 Apr 2025 14:52:32 -0500 To: "php internals" Message-ID: <799bd54a-2136-4410-823a-9233ccbeb16a@app.fastmail.com> In-Reply-To: <4a3c6ce7-102d-4cfe-a7a8-35630715b870@gmail.com> References: <4a3c6ce7-102d-4cfe-a7a8-35630715b870@gmail.com> Subject: Re: [PHP-DEV] [RFC brainstorm] Approximately equals operator Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") 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. I realized what we truly need to revolutionize PHP:=20 > a new operator. > > Hear me out. > 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 dea= l with=20 > these kinds of data. > > Introducing: the "approximately equal" (or "approx-equal") operator=20 > `~=3D` (to immitate the maths symbol =E2=89=83). > This combines the power of type coercion with approximating equality. > Who cares if things are actually equal, close enough amirite? > > First of all, if `$a =3D=3D $b` holds, then `$a ~=3D $b` obviously. > 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 the= m. > This also means that the "Fundamental Theorem of Engineering" now hold= s! > i.e. 2.7 ~=3D 3 and 3.14 ~=3D 3. Of course also 2.7 ~=3D 3.14. But thi= s is=20 > false obviously: 2 ~=3D 1. > > Ever had trouble with users mistyping something? Say no more! > "This is a tpyo" ~=3D "This is a typo". It's typo-resistant! > However, if the strings are too different, then they're not=20 > approx-equal. > For example: "vanilla" ~=3D "strawberry" gives false. > How does this work? > * The strings are equal if their levenshtein ratio is <=3D 50%, so it'= s=20 > adaptive to the length. > * 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". > > There is of course a PoC implementation available at:=20 > https://github.com/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.4 ~=3D 1); // true > var_dump(-1.4 ~=3D -1); // true > var_dump(-1.5 ~=3D -1.8); // true > var_dump(random_int(1, 1) ~=3D 1.1); // 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.5 ~=3D "-1.a"); // true > var_dump(-1.5 ~=3D "-1.aaaaaaa"); // 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. > However, there are no real blockers to add support for that. > > 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 c= ontrolled by an ini setting. Maximum flexibility! --Larry Garfield