a little math problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Hunter

    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(bas e, x):
    if x%base!=0:
    return nearest_gt(base , x)
    else:
    return x

    def nearest_leq(bas e, 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


Working...