-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
Implement Number.InDeltaRelative #306
Conversation
The linter is failing because I have a line that is 94 characters long, a bit over the 90 character limit. I think I've made it as short as I can while following the pattern of similar comment lines. Do you want me to try to shorten it some more or is it alright to raise the linter column limit to 94? |
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.
Thanks for PR! See a few comment below.
Yep, please shorten it. I understand this is a matter of taste, but I'm following this style in my projects, which allows editing code even on a smaller screen split in half vertically, like it's on one of my machines :] |
☔ The latest upstream change (presumably these) made this pull request unmergeable. Please resolve the merge conflicts. |
c7ce4f8
to
d57421f
Compare
Hello, I've implemented the requested changes as I understood them. I'm not sure I properly implemented the corner cases though. For example, I set the test to fail if any provided parameter is |
So let's break down the corner cases Given it another thought, mathematically, I think here's how it should work:
Here:
What do you think about changing formula from: math.Abs(n.value-value) / math.Abs(n.value) > delta to: math.Abs(n.value-value) > math.Abs(n.value) * delta This way zero will be handled automatically. If We'll still need to handle manually inf and nan. If either of values is nan, we should fail. If either of values is inf, we should succeed only if they're both infs of the same sign. BTW we should also check that delta is non-negative, and fail with AssertUsage otherwise. |
Hm, on the other hand, the formula I suggested will make precision worse. Then a better option is to use the old formula, but pretend that we're using the new one by handling zero specially. |
Alright, I've made some changes to try and accommodate all of the edge cases. I think the logic and tests are correct, but please review and confirm. Additional cases added:
A note on the Inf values: A note on the |
I think I can make the +-Inf cases work if I use the same rearranged formula used when target = 0. I'll try implementing that. |
It seems we can combine two checks: if n.value == 0 {
if math.Abs(n.value-value) > math.Abs(n.value)*delta { relativeDiff := math.Abs(n.value-value) / math.Abs(n.value)
if relativeDiff > delta { into one: if (n.value == 0 && value != 0) || math.Abs(n.value-value) / math.Abs(n.value) > delta {
...
} |
Or, generalizing it to inf too, as you mentioned above: if ((n.value == 0 || math.IsInf(n.value, 0)) && value != n.value) ||
math.Abs(n.value-value) / math.Abs(n.value) > delta {
...
} |
cb7fef5
to
948c2cf
Compare
Hello, I've made the requested changes related to Inf numbers, let me know what you think. The CI failed but it looks like maybe it's a transient issue that can be resolved with a retry. |
Thanks a lot for update! Here is a small follow-up commit: 2b1131c I've rearranged checks a bit and changed one bit of behavior: NotInDeltaRelative now succeeds if numbers are opposite infs. Also added a few more tests. |
This PR resolves #156.
Please let me know if anything needs to be changed!