1 # Copyright (c) 2005-2007 Bryce "Zooko" Wilcox-O'Hearn
2 # mailto:zooko@zooko.com
3 # http://zooko.com/repos/pyutil
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this work to deal in this work without restriction (including the rights
6 # to use, modify, distribute, sublicense, and/or sell copies).
9 A few commonly needed functions.
16 The smallest integer k such that k*d >= n.
18 return (n/d) + (n%d != 0)
20 def next_multiple(n, k):
22 The smallest multiple of k which is >= n.
24 return div_ceil(n, k) * k
28 The smallest number that has to be added to n so that n is a multiple of k.
35 def is_power_of_k(n, k):
36 return k**int(math.log(n, k) + 0.5) == n
38 def next_power_of_k(n, k):
42 x = int(math.log(n, k) + 0.5)
50 return sum(l) / len(l)
54 The smallest integer k such that b^k >= n.
56 log_ceil(n, 2) is the number of bits needed to store any of n values, e.g.
57 the number of bits needed to store any of 128 possible values is 7.
68 The largest integer k such that b^k <= n.
77 def round_sigfigs(f, n):
78 fmt = "%." + str(n-1) + "e"