radtools.compare_numerically#

radtools.compare_numerically(x, condition, y, eps=None, rtol=0.0001, atol=1e-08)[source]#

Compare two numbers numerically.

The approach is taken from [1]:

\[\begin{split}\begin{matrix} x < y & x < y - \varepsilon \\ x > y & y < x - \varepsilon \\ x \le y & \text{ not } (y < x - \varepsilon) \\ x \ge y & \text{ not } (x < y - \varepsilon) \\ x = y & \text{ not } (x < y - \varepsilon \text{ or } y < x - \varepsilon) \\ x \ne y & x < y - \varepsilon \text{ or } y < x - \varepsilon \end{matrix}\end{split}\]
Parameters:
xfloat

First number.

conditionstr

Condition to compare with. One of "<", ">", "<=", ">=", "==", "!=".

yfloat

Second number.

epsfloat, optional

Tolerance. Used for the comparison if provided. If None, then computed as:

eps = atol + rtol * abs(y)
rtolfloat, default 1e-04

Relative tolerance.

atolfloat, default 1e-08

Absolute tolerance.

Returns:
result: bool

Whether the condition is satisfied.

Raises:
ValueError

If condition is not one of "<", ">", "<=", ">=", "==", "!=".

References

[1]

Grosse-Kunstleve, R.W., Sauter, N.K. and Adams, P.D., 2004. Numerically stable algorithms for the computation of reduced unit cells. Acta Crystallographica Section A: Foundations of Crystallography, 60(1), pp.1-6.