By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

a little math problem

P: n/a

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)
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?

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.