From cdb8930b09a780bea982880dc43f20c6bf8912bb Mon Sep 17 00:00:00 2001 From: Mehak Abid Date: Mon, 23 Mar 2026 08:08:24 +0530 Subject: [PATCH] Add helper functions for number checks Added helper functions to check if a number is close to an integer and if it is a power of a base. --- lib/matplotlib/ticker.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 83e13841677a..212b70c2cbc0 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -145,6 +145,23 @@ _log = logging.getLogger(__name__) +# Fixed helper functions +def _is_close_to_int(x): + """Return True if x is close to an integer.""" + return math.isclose(x, round(x), rel_tol=1e-9) + +def _is_decade(x, *, base=10, rtol=None): + """Return True if *x* is an integer power of *base*.""" + if not np.isfinite(x): + return False + if x == 0.0: + return True + lx = np.log(abs(x)) / np.log(base) + if rtol is None: + return np.isclose(lx, np.round(lx)) + else: + return np.isclose(lx, np.round(lx), rtol=rtol) + __all__ = ('TickHelper', 'Formatter', 'FixedFormatter', 'NullFormatter', 'FuncFormatter', 'FormatStrFormatter', 'StrMethodFormatter', 'ScalarFormatter', 'LogFormatter',