New math.floor() parameter

Hi. I suggest a new parameter for the math.floor() function so we can specify the location.

Example:

a = 547.193
math.floor(a) -> 547
math.floor(a, 1) -> 547.1
math.floor(a, 2) -> 547.19
math.floor(a, 9) -> 547.193
math.floor(a, -1) -> 540
math.floor(a, -9) -> 500

A lot of the time, the math module is a simple wrapper around the math module in C. floor() in C doesn’t have this type of functionality, so doing this specifically for Python would be a special case. On one hand

I like that it would be similar to round(). If we were to do this, we should also do it for ceil().

On the other hand, there are other ways to do this already: rounding - round down to 2 decimal in python - Stack Overflow

I figure its probably best to continue to use one of the ways in the stack overflow post.

One of problems is that 547.193 < 547193/1000

>>> from fractions import Fraction as F
>>> F(547.193) - F(547193, 1000)
Fraction(-9, 549755813888000)

So math.floor(547.193, 3) could return 547.192 or 547.1930000000001, depending on the used algorithm. Perhaps not what you expected.

3 Likes