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
Proposal for new is_approx() behaviour
#366
Conversation
See http://en.wikipedia.org/wiki/Relative_change_and_difference#Formulas for relative difference formula.
|
First, let me thank you for taking the point on this! I think it's an important improvement that needs to be done, and you've got a great start here. Here are some random thoughts on the patch: Why are $got and $expected Mu, but $tol is Num? My first instinct is that $tol should be a Real. Should $got and $expected be Num? Do they work if one or both of them are Complex? Do we want to allow non-Num types to be passed? Does your code work if $expected is 0? And should we take advantage of a switch to also switch it to is-approx? |
Good question. Due to "historical reasons" I guess. Actually this is sort of a leftover from the original implementation which uses
I don't believe so. This would be a good extension to the code though, thanks for mentioning it!
I don't know directly, however instinctively I would say non-
Well caught! The current version ( f904b21) doesn't work with 0. I'll fix this and push as soon as I can.
I think this is a very good idea in general. All functions should use the more Perl6-y way of naming variables. Such a change would break a lot of code (Rakudo, the spec tests, roast, moar etc. are all probably affected by such a change) so this needs to be a careful and well discussed before being carried out. At present, I'd say the current change to |
|
If there is an agreement to rename, then a possible strategy would be to introduce |
Thanks to @colomon++ for the suggestion in PR rakudo#366. The actual suggestion was to use Num or a Real, however Numeric is more general than a Real.
|
What jnthn said! -Scott On Thu, Feb 19, 2015 at 5:43 AM, Jonathan Worthington <
|
i.e. is_approx(a,b) == is_approx(b,a)
See http://c-faq.com/fp/fpequal.html for reasoning and implementation.
See the discussion about PEP 485 (https://www.python.org/dev/peps/pep-0485/), its implementation: https://github.com/PythonCHB/close_pep/blob/master/is_close.py as well as the `assert_allclose` behaviour: http://docs.scipy.org/doc/numpy/reference/generated/numpy.testing.assert_allclose.html#numpy.testing.assert_allclose for reasoning about this implementation.
Comparison of values around zeroI've added a version of Rename
|
Proposal for new `is_approx()` behaviour
Changes: Raku/nqp@2017.06-59-g61307bb...2017.06-62-g8cd835f 8cd835f Exclude empty messages from check c234cf4 Merge pull request #366 from MasterDuke17/fix_no_message_when_using_ll-exception 859b444 Fix no message printing when using ll-exception
While documenting the
is_approx()test function behaviour I was confused by a (potentially only perceived by me) difference between the specs and the actual behaviour. Namely, the specs say thatis_approxwill return True when two numbers are within 1e-5 of one another. This implies to me something like this:However, the implementation allows large numbers to be "approximately equal" if 6 significant digits (i.e. not "within 1e-5") are the same. In other words:
On the other hand, small numbers (e.g. smaller than 1e-6) are always "approximately equal" even if the values are wildly different:
The reason that this returns True is that both numbers are smaller than 1e-5.
Due to this confusion, and after comparing this behaviour with other testing frameworks (e.g. http://docs.scipy.org/doc/numpy/reference/generated/numpy.testing.assert_approx_equal.html#numpy.testing.assert_approx_equal), I propose changing the current behaviour of
is_approx()to consider two numbers to be approximately equal if they are equal up to a given number of significant digits. This allowsis_approx()to behave the same for the "large" and "small" numbers mentioned above and is thus a more general solution.The proposed changes given in this PR are based upon http://en.wikipedia.org/wiki/Relative_change_and_difference#Formulas and http://c-faq.com/fp/fpequal.html.
The PR includes "tests", which are not intended to be merged, but merely programmatically to show in a more detailed manner what I mean here; both the current and proposed behaviour.
By making this change in behaviour, it was also possible to make
is_approx()accept a variable tolerance. Thus it is now possible to write code like this:I realise this is a large change and could break a lot of code, thus I'd appreciate feedback as to how sensible this change is and whether or not it is appropriate. It would, however, be very good to remove the ambiguity in the current behaviour.