Reputation: 3368
I have a small math problem I am trying to solve
Given a number x and resolution y, I need to find the next x' with the required resolution.
e.g.
x = 1.002 y = 0.1 x'= 1.1
x = 0.348 y = 0.1 x'= 0.4
x = 0.50 y = 1 x'= 1
x = 0.32 y = 0.05 x'= 0.35
Is there any smart way of doing this in Python?
Upvotes: 6
Views: 2143
Reputation: 375814
import math
def next_multiple(x, y):
return math.ceil(x/y)*y
def try_it(x, y):
print x, y, next_multiple(x, y)
for x, y in [
(1.002, 0.1),
(0.348, 0.1),
(0.50, 1),
(0.32, 0.05)
]:
try_it(x, y)
produces:
1.002 0.1 1.1
0.348 0.1 0.4
0.5 1 1.0
0.32 0.05 0.35
I think your first example output is wrong, The correct answer for x' is 1.1, right?
Upvotes: 12