Beispiel #1
0
def ceil2digit(x, digit=1, multiplier=1):
    """Round up number x to first signicant digit."""
    if x == 0: return 0
    x = float(x)
    e = int(floor(log(abs(x), 10))) - (digit - 1)
    if multiplier > 1: e = e - ceil(log(multiplier, 10))
    return ceil(x / multiplier / (10.**e)) * (10.**e) * multiplier
Beispiel #2
0
def magnitude(x):
    """Get magnitude of a number. E.g. 45 is 2, 2304 is 4, 0.84 is -1"""
    if x == 0: return 0
    if x == 1: return 1
    logx = round(log10(abs(x)) * 1e6) / 1e6
    if x % 10 == 0: return int(logx) + 1
    if x < 1: return int(floor(logx))
    return int(ceil(logx))
Beispiel #3
0
def partition(mylist, nparts):
    """Partion list into given number of chunks, as evenly sized as possible."""
    nleft = len(mylist)
    divider = float(nparts)
    parts = []
    findex = 0
    for i in range(0, nparts):  # partition recursively
        nnew = int(ceil(nleft / divider))
        lindex = findex + nnew
        parts.append(mylist[findex:lindex])
        nleft -= nnew
        divider -= 1
        findex = lindex
        #print nnew
    return parts