4

I would like to create a function floor(number, step), which acts like :

floor(0, 1) = 0
floor(1, 1) = 1
floor(1, 2) = 0
floor(5, 2) = 4
floor(.8, .25) = .75

What is the better way to do something like that ?

Thanks.

2
  • Is the step always going to be a power of two, as in your examples? Commented Aug 12, 2010 at 22:51
  • Nope. Just any number :) Commented Aug 17, 2010 at 11:40

3 Answers 3

6

You could do something like floor( val / step ) * step

Sign up to request clarification or add additional context in comments.

Comments

2

what you want is basically the same as

step * (x // step)

isn't ?

Comments

1

Something along the lines of the code below ought to do the job.

def stepped_floor (n, step=1):
    return n - (n % step)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.