Skip to content

Commit

Permalink
Use approximation to idenfify ints in log_format
Browse files Browse the repository at this point in the history
  • Loading branch information
has2k1 committed Sep 28, 2022
1 parent 3458383 commit ccb2f86
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions mizani/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,9 @@ def __call__(self, x):
else:
def _exp(num, base):
e = np.log(num)/np.log(base)
if float.is_integer(e):
e = int(e)
e_round = np.round(e)
if np.isclose(e, e_round):
e = int(e_round)
else:
e = np.round(e, 3)
return e
Expand Down
3 changes: 3 additions & 0 deletions mizani/tests/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def test_log_format():
formatter = log_format(base=8)
assert formatter([1, 4, 8, 64]) == ['8^0', '8^0.667', '8^1', '8^2']

formatter = log_format(base=5)
assert formatter([1, 5, 25, 125]) == ['5^0', '5^1', '5^2', '5^3']

formatter = log_format(base=np.e)
assert formatter([1, np.pi, np.e**2, np.e**3]) == \
['e^0', 'e^1.145', 'e^2', 'e^3']
Expand Down

0 comments on commit ccb2f86

Please sign in to comment.