Numerical accuracy#
For the full reference see numerical.
Import#
>>> # Exact import
>>> from radtools.numerical import compare_numerically
>>> # Recommended import
>>> from radtools import compare_numerically
Comparison#
Compares two numbers within a given tolerance.
Tolerance for the comparison is given as:
\[\varepsilon = atol + rtol \cdot y\]
By default \(atol\) and \(rtol\) are equal to crystal.REL_TOL
and crystal.ABS_TOL
respectively. You can pass exact values for eps.
The conditions are translated as (source [1]):
Condition |
Numerical condition |
---|---|
\(x < y\) |
\(x < y - \varepsilon\) |
\(x > y\) |
\(y < x - \varepsilon\) |
\(x \le y\) |
not \((y < x - \varepsilon)\) |
\(x \ge y\) |
not \((x < y - \varepsilon)\) |
\(x = y\) |
not \((x < y - \varepsilon\) or \(y < x - \varepsilon)\) |
\(x \ne y\) |
\(x < y - \varepsilon\) or \(y < x - \varepsilon\) |
>>> compare_numerically(1.0, "==", 1.0 + 1e-10)
True
>>> compare_numerically(1.0, "==", 1.0 + 2e-4)
False
>>> compare_numerically(1.0, "!=", 1.0 + 2e-4, eps=1e-3)
False