471,055 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,055 software developers and data experts.

a little math problem


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
Jul 18 '05 #1
0 795

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Frank Millman | last post: by
1 post views Thread by Reiner Apke | last post: by
5 posts views Thread by Ark | last post: by
6 posts views Thread by ng_mr | last post: by
110 posts views Thread by Gregory Pietsch | last post: by
11 posts views Thread by Sambo | last post: by
10 posts views Thread by David Coleman | last post: by
4 posts views Thread by =?Utf-8?B?UmVuZQ==?= | last post: by
21 posts views Thread by =?ISO-8859-1?Q?Rog=E9rio_Brito?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.