From: Zooko O'Whielacronx Date: Thu, 1 Feb 2007 22:55:26 +0000 (-0700) Subject: add mathutil.next_power_of_k() and mathutil.ave() X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/simplejson/configuration.rst?a=commitdiff_plain;h=1373789463aaa744413c2777551be9ce87cea7ba;p=tahoe-lafs%2Ftahoe-lafs.git add mathutil.next_power_of_k() and mathutil.ave() --- diff --git a/src/allmydata/util/mathutil.py b/src/allmydata/util/mathutil.py index fa58ecc0..afcb3121 100644 --- a/src/allmydata/util/mathutil.py +++ b/src/allmydata/util/mathutil.py @@ -34,3 +34,18 @@ def pad_size(n, k): def is_power_of_k(n, k): return k**int(math.log(n, k) + 0.5) == n + +def next_power_of_k(n, k): + if n == 0: + x = 0 + else: + x = int(math.log(n, k) + 0.5) + r = k**x + if k**x < n: + return k**(x+1) + else: + return k**x + +def ave(l): + return sum(l) / len(l) +