wulfric.compare_with_tolerance#

wulfric.compare_with_tolerance(x, condition, y, eps=None, rtol=1e-05, atol=1e-08)[source]#

Compares two numbers with given accuracy.

The formal definition 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 \(10^{-5}\)

Relative tolerance.

atolfloat, default \(10^{-8}\)

Absolute tolerance.

Returns:
result: bool

Whether the condition is satisfied within given tolerance.

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.

Examples

>>> import wulfric
>>> wulfric.compare_with_tolerance(1, "==", 1.0000001, eps=1e-6)
True
>>> wulfric.compare_with_tolerance(1, "==", 1.0000001, eps=1e-8)
False