On 2/6/06, Aahz <aahz at pythoncraft.com> wrote: ... > > def areclose(x, y, relative_err = 1.e-5, absolute_err=1.e-8): > > diff = abs(x - y) > > ave = (abs(x) + abs(y))/2 > > return diff < absolute_err or diff/ave < relative_err > > > > Also, separating the two terms with 'or' rather than '+' makes the > > two error terms mean more what they are named. The '+' mixes the two > > effects and even though the result is basically the same, it makes it > > difficult to explain when the test will be true. > > Yes, that's a big help. I was a bit concerned that this would have no > utility for numbers with large magnitude. Alex, given your focus on > Python readability, I'm a bit surprised you didn't write this to start > with! As I said, I was just copying the definition in Numeric, which is well-tried by long use. Besides, this "clear expression" could present problems, such as possible overflows or divisions by zero when ave is 0 or very small; much as I care about readability, I care about correctness even more. Once it comes to readability, I prefer Numeric's choice to call the two terms "tolerances", rather than (as here) "errors"; maybe that depends on my roots being in engineering, where an error means a mistake (like it does in real life), while tolerance's a good and useful thing to have (ditto), rather than some scientific discipline where terms carry different nuances. Alex
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4