Skip to content

Add helper functions for number checks and validations#31350

Closed
mohdabid4333-dotcom wants to merge 1 commit intomatplotlib:mainfrom
mohdabid4333-dotcom:bugfix/ticker-vmax-check
Closed

Add helper functions for number checks and validations#31350
mohdabid4333-dotcom wants to merge 1 commit intomatplotlib:mainfrom
mohdabid4333-dotcom:bugfix/ticker-vmax-check

Conversation

@mohdabid4333-dotcom
Copy link

Added helper functions to check if a number is close to an integer and if it is a power of a base.

PR Title
Add helper functions for number checks and validations in ticker.py

PR Summary

This PR introduces two internal helper functions to lib/matplotlib/ticker.py to streamline numeric validations and improve code readability for future ticker enhancements.

Changes:

  • Added _is_close_to_int(x): A utility using math.isclose to determine if a value is effectively an integer within a relative tolerance of $1e-9$.
  • Added _is_decade(x, *, base=10, rtol=None): A utility to determine if a given value is an integer power of a specified base (e.g., $1, 10, 100$ for base 10). This handles finite checks and logarithmic comparisons with configurable relative tolerance.

Why is this change necessary?

These common checks are frequently needed when calculating tick positions and formatting labels, especially for logarithmic scales. Centralizing them as helper functions reduces code duplication and ensures consistent precision across the ticker module.

AI Disclosure

I used an AI assistant to help format this PR description based on the code changes I authored.

PR Checklist

Added helper functions to check if a number is close to an integer and if it is a power of a base.
@timhoffm
Copy link
Member

The two functions already exist in ticker.py. Duplicating makes absolutely no sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants