mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-01-31 00:24:13 +00:00
import mathutil from pyutil
This commit is contained in:
parent
6eaaf1fe9d
commit
f3f91105bd
36
src/allmydata/util/mathutil.py
Normal file
36
src/allmydata/util/mathutil.py
Normal file
@ -0,0 +1,36 @@
|
||||
# Copyright (c) 2005-2007 Bryce "Zooko" Wilcox-O'Hearn
|
||||
# mailto:zooko@zooko.com
|
||||
# http://zooko.com/repos/pyutil
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this work to deal in this work without restriction (including the rights
|
||||
# to use, modify, distribute, sublicense, and/or sell copies).
|
||||
|
||||
"""
|
||||
A few commonly needed functions.
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
def div_ceil(n, d):
|
||||
"""
|
||||
The smallest integer k such that k*d >= n.
|
||||
"""
|
||||
return (n/d) + (n%d != 0)
|
||||
|
||||
def next_multiple(n, k):
|
||||
"""
|
||||
The smallest multiple of k which is >= n.
|
||||
"""
|
||||
return div_ceil(n, k) * k
|
||||
|
||||
def pad_size(n, k):
|
||||
"""
|
||||
The smallest number that has to be added to n so that n is a multiple of k.
|
||||
"""
|
||||
if n%k:
|
||||
return k - n%k
|
||||
else:
|
||||
return 0
|
||||
|
||||
def is_power_of_k(n, k):
|
||||
return k**int(math.log(n, k) + 0.5) == n
|
Loading…
x
Reference in New Issue
Block a user