diff --git a/mizani/utils.py b/mizani/utils.py index e57364b..cff985a 100644 --- a/mizani/utils.py +++ b/mizani/utils.py @@ -235,6 +235,51 @@ def precision(x: FloatArrayLike | float) -> float: return min(1, res) +def precision_test(x: FloatArrayLike | float) -> float: + """ + + Examples + -------- + >>> precision_test([0.08, 0.09]) + 0.009999999999999995 + -2.0 + -2.0 + 0.001 + [80. 90.] + [80. 90.] + [0. 0.] + [ True True] + True + 0.01 + """ + x = np.asarray(x) + x = x[~(np.isnan(x) | np.isinf(x))] + + if len(x) <= 1: + return 1 + + smallest_diff = np.diff(np.sort(x))[0] + if smallest_diff < np.sqrt(EPSILON): # pragma: nocover + return 1 + else: + res = 10 ** int(np.floor(np.log10(smallest_diff)) - 1) + has_extra_zeros = (np.round(x / res) % 10 == 0).all() + print(smallest_diff) + print(np.log10(smallest_diff)) + print(np.floor(np.log10(smallest_diff))) + print(res) + print(x / res) + print(np.round(x / res)) + print(np.round(x / res) % 10) + print((np.round(x / res) % 10 == 0)) + print(has_extra_zeros) + if has_extra_zeros: + res *= 10 + + # 1 comes first so that min(1, 1.0) returns an integer + return min(1, res) + + def same_log10_order_of_magnitude(x, delta=0.1): """ Return true if range is approximately in same order of magnitude