I have a number base, float or int. I need routines that return the
largest multiple of base < x, largest multiple of base <=x, smallest
multiple of base > x and smallest multiple of base >= x.
For integer base, I have been doing
def nearest_geq(base, x):
if x%base!=0:
return nearest_gt(base, x)
else:
return x
def nearest_leq(base, x):
return int(x)//base * base
def nearest_gt(base, x):
return int(x+base)//base * base
def nearest_lt(base, x):
return int(x-base)//base * base
Is there a more elegant solution that works well for floats too?
JDH |