diff --git a/qutebrowser/browser/hints.py b/qutebrowser/browser/hints.py index cfa435b3e..c4b5f51af 100644 --- a/qutebrowser/browser/hints.py +++ b/qutebrowser/browser/hints.py @@ -21,7 +21,6 @@ import collections import functools -import math import os import re import html @@ -454,8 +453,7 @@ class HintManager(QObject): # Determine how many digits the link hints will require in the worst # case. Usually we do not need all of these digits for every link # single hint, so we can show shorter hints for a few of the links. - needed = max(min_chars, math.ceil( - math.log(len(elems), len(chars))-1e-13)) + needed = max(min_chars, utils.ceil_log(len(elems), len(chars))) # Short hints are the number of hints we can possibly show which are # (needed - 1) digits in length. if needed > min_chars and needed > 1: @@ -487,7 +485,7 @@ class HintManager(QObject): elems: The elements to generate labels for. """ strings = [] - needed = max(min_chars, math.ceil(math.log(len(elems), len(chars)))) + needed = max(min_chars, utils.ceil_log(len(elems), len(chars))) for i in range(len(elems)): strings.append(self._number_to_hint_str(i, chars, needed)) return strings diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index f2e19a743..a6c8e866c 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -712,3 +712,16 @@ def guess_mimetype(filename, fallback=False): else: raise ValueError("Got None mimetype for {}".format(filename)) return mimetype + + +def ceil_log(number, base): + """Compute max(1, ceil(log(number, base))). + + Use only integer arithmetic in order to avoid numerical error. + """ + result = 1 + accum = base + while accum < number: + result += 1 + accum *= base + return result