a little math problem

Discussion in 'Python' started by John Hunter, Apr 16, 2004.

  1. John Hunter

    John Hunter Guest

    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
     
    John Hunter, Apr 16, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chirs
    Replies:
    18
    Views:
    788
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,132
    Jonathan Turkanis
    Apr 1, 2004
  3. Mark Healey
    Replies:
    7
    Views:
    1,521
    Tim Prince
    May 22, 2006
  4. ThaDoctor
    Replies:
    3
    Views:
    393
    Alan Woodland
    Sep 28, 2007
  5. VK
    Replies:
    15
    Views:
    1,217
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page