Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > a little math problem

Reply
Thread Tools

a little math problem

 
 
John Hunter
Guest
Posts: n/a
 
      04-16-2004

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
math.h trig functions questions (and some forgotten high school math) Mark Healey C Programming 7 05-22-2006 10:42 AM
Re: Is still math.h the C++ math library ? AciD_X C++ 4 04-01-2004 07:29 PM
Why can I not use: Math a=new Math(); chirs Java 18 03-02-2004 06:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57