import numpy as np
import math


def CalcOffset(a, oom) :
    if a.min() < 0.0 :
        return math.ceil(a.max()/10**oom)*10**oom
    else :
        return math.floor(a.min()/10**oom)*10**oom
    
def common_sigdigs(a) :
    return math.ceil(math.log10(max(a) - min(a)))

if __name__ == '__main__' :
    testdata = ([ 0.4538,  0.4559,  0.4553,  0.4578],
                [ 3789.12,  3782.89,  3783.1 ],
                [ 45124.3,   45831.34,  45831.75],
                [ 0.000721,   0.0007283,  0.0007243],
                [ 12592.82,  12599.1,   12593.5,   12591.43],
                [  9.,  10.,  11.,  12.],
                [  900., 1000., 1100., 1200.],
                [  1900., 1000., 1100., 1200.],
                [ 0.99, 1.01],
                [ 9.99, 10.01],
                [ 99.99, 100.01],
                [ 5.99, 6.01],
                [ 15.99, 16.01],
                [-0.452, -0.4, 0.411, 0.492])

    for a in testdata :
        a = np.array(a)
        common_oom = common_sigdigs(a)
        offset = CalcOffset(a, common_oom)
        print a, '\t', common_oom, offset

        common_oom = common_sigdigs(-a)
        offset = CalcOffset(-a, common_oom)
        print -a, '\t', common_oom, offset
        print ' '
        
