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

Uses rtol instead of places in tests #148

Open
femtotrader opened this issue Jul 7, 2024 · 2 comments
Open

Uses rtol instead of places in tests #148

femtotrader opened this issue Jul 7, 2024 · 2 comments

Comments

@femtotrader
Copy link
Contributor

Hello,

Test with assertAlmostEqual uses places which is close to absolute tolerance (atol) 10**-places is atol
relative tolerance (rtol) would probably be a better idea.

See
https://github.com/nardew/talipp/pull/147/files#diff-356e9ecd00b59db50b3531a4b1143455b87bc4d213b491b642d19db6bff50ec9R51

Kind regards

@nardew
Copy link
Owner

nardew commented Jul 12, 2024

I am not sure I understand, could you please elaborate a bit more? What exactly is the problem with current version?

@femtotrader
Copy link
Contributor Author

The use of relative tolerance (rtol) instead of absolute tolerance (places) in unit tests, especially for floating-point comparisons, is often preferred for several reasons:

  1. Scale Independence:

rtol is scale-independent, meaning it works well across a wide range of magnitudes.
It allows for a consistent level of precision regardless of the size of the numbers being compared.

  1. Numerical Stability:

Floating-point arithmetic can introduce small errors due to how computers represent and operate on decimal numbers.
These errors can accumulate, especially in complex calculations.
rtol accounts for these small discrepancies more reliably than absolute tolerance.

  1. Meaningful Comparisons:

For very large or very small numbers, an absolute tolerance might be too strict or too lenient, respectively.
rtol ensures the comparison is meaningful relative to the magnitude of the numbers.

  1. Handling Different Units:

When dealing with calculations involving different units or scales, rtol provides a more consistent way to compare results.

  1. Compatibility with Scientific Computing:

In scientific and engineering applications, relative error is often more important than absolute error.

  1. Flexibility:

rtol allows for setting a percentage difference that's acceptable, which can be more intuitive in many cases.

Example:
Consider comparing 1.0 and 1.000001:

With places=6 (absolute tolerance), this would fail.
With rtol=1e-5 (relative tolerance), this would pass, as the relative difference is about 1e-6.

Now compare 1,000,000 and 1,000,001:

With places=6, this would pass, which might not be desirable for such large numbers.
With rtol=1e-5, this would still pass, maintaining consistent precision.

In practice, a combination of both relative (rtol) and absolute (atol) tolerance is often used for robust comparisons, especially in libraries like NumPy's isclose() function or pytest's approx().

(claude.ai answer)

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

No branches or pull requests

2 participants