a little math problem

J

John Hunter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top