-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Approximately equals operator #18214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
You had me in the first half, not gonna lie |
Thanks for this draft. I didn't had a quality laugh for a long time. |
var_dump(1.4 ~= 1); // true | ||
var_dump(-1.4 ~= -1); // true | ||
var_dump(-1.5 ~= -1.8); // true | ||
var_dump(random_int(1, 1) ~= 1.1); // true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test for the common floating point example to make sure it is correctly handled:
var_dump(random_int(1, 1) ~= 1.1); // true | |
var_dump(random_int(1, 1) ~= 1.1); // true | |
var_dump(0.1 + 0.2 ~= 0.3); // true |
// AST dump test | ||
assert(function() { return 1 ~= 2; } && false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// AST dump test | |
assert(function() { return 1 ~= 2; } && false); | |
try { | |
// AST dump test | |
assert(function() { return 1 ~= 2; } && false); | |
} catch (AssertionError $e) { | |
echo $e->getMessage(), PHP_EOL; | |
} |
The stacktrace is just noise.
See y'all next year when I finally get rid of type checking, TypeErrors are so yesterday! |
@nielsdos Shhhhh... I want |
Below is a copy to my email to internals: https://externals.io/message/126989
I'm excited to share what I've been working on!
I had an epiphany. I realized what we truly need to revolutionize PHP: a new operator.
Hear me out.
We live in an imperfect world, and we often approximate data, but neither
==
nor===
are ideal comparison operators to deal with these kinds of data.Introducing: the "approximately equal" (or "approx-equal") operator
~=
(to immitate the maths symbol ≃).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 == $b
holds, then$a ~= $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 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.
Ever had trouble with users mistyping something? Say no more!
"This is a tpyo" ~= "This is a typo". It's typo-resistant!
However, if the strings are too different, then they're not approx-equal.
For example: "vanilla" ~= "strawberry" gives false.
How does this work?
~<
operator, then "vanilla" ~< "strawberry".There is of course a PoC implementation available at: [GitHub]
You can see more examples on GitHub in the tests, here is a copy:
Note that this does not support all possible Opcache optimizations 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 ;)