Skip to content
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

Add isClose, a replacement for approxEqual #7241

Merged
merged 1 commit into from Dec 30, 2019
Merged

Add isClose, a replacement for approxEqual #7241

merged 1 commit into from Dec 30, 2019

Conversation

ghost
Copy link

@ghost ghost commented Oct 21, 2019

This is a replacement for PR #7187, because I had to remove my old account on GitHub.

My original comment was:

As suggested in PR #7173, I wrote a new version of approxEqual (called isClose to avoid naming conflicts), which is based on the thoughts given in [1]. The purpose of both functions is the same, namely providing a utility for comparing floating point numbers. This is necessary, because operations on floating point numbers inevitably produce rounding errors and therefore cannot be compared reasonable using ==.

The main difference is, that this function has much stronger default values for the error allowed, based on the capabilities of the used floating point type. This helps in catching errors, especially as approxEqual is mainly used in unittests. This addresses issue #15881. Together with PR #7180 this can be considered a fix for that issue.

The new function is furthermore symmetric, to avoid users being confused or have to remember the order of the operands. This addresses issue #15763.

After this PR has been approved I think, it would be wise to continue with the following roadmap:

  1. replace all occurences of approxEqual in Phobos by isClose. With one exception, approxEqual is only used in unittests. The exception is secantMethod in std/numerical.d.
  2. deprecate approxEqual in favour of isClose. (I don't know if I'm in the position to deprecate anything, nor to ask for it, but I think, this would be a good idea in the long-run.)
  3. After some time, remove approxEqual alltogether.

[1] https://www.python.org/dev/peps/pep-0485/

@ghost ghost requested a review from ibuclaw as a code owner October 21, 2019 09:59
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @berni44! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + phobos#7241"

std/math.d Outdated Show resolved Hide resolved
std/math.d Outdated Show resolved Hide resolved
Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for missing this.

@ghost
Copy link
Author

ghost commented Dec 30, 2019

Don't worry. It's not that important to me (else I would have pinged) - I thought, it might be a problem, because a new name is introduced.

Patches are on the way.

@thewilsonator
Copy link
Contributor

Thanks.

std/math.d Outdated Show resolved Hide resolved
Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise looks good.

std/math.d Outdated Show resolved Hide resolved
@dlang-bot dlang-bot merged commit d6540d7 into dlang:master Dec 30, 2019
@quickfur
Copy link
Member

This is a pretty significant change, and IMO notable enough to be included in the release notes. Can you submit a follow-up PR to add the corresponding file in changelog/ describing this change? (We're still using that for generating release notes, right?)

@ghost
Copy link
Author

ghost commented Dec 30, 2019

I'm currently working on replacing all occurences of approxEqual in Phobos with corresponding calls to isClose; see #7340. Essentially I'm through, but from my first try I remember, that some tests in std.complex failed on 32bit computers, because the calculation of log() is less accurate. I have to experiment a little with the autotesters to get that right but maybe I'll manage to still finish that this year. :-)

After that PR has been accepted, I plan to file another, which deprecates approxEqual and adds a changelog-entry (I actually planed to ask if I should do so, but thanks to your post now I know I should :-) ). OK?

Use $(LREF feqrel) to get the number of equal bits in the mantissa.
*/
bool isClose(T, U, V = CommonType!(FloatingPointBaseType!T,FloatingPointBaseType!U))
(T lhs, U rhs, V maxRelDiff = CommonDefaultFor!(T,U), V maxAbsDiff = 0.0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CommonDefaultFor needs to be public and documented if it used as the default value in a public API. Currently it has visibility in the math package, not public visibility.

In this case, not having it exposed means that callers cannot provide the default for the maxRelDiff parameter if they only want to change the maxAbsDiff parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants