# [Numpy-discussion] Equvalent function for Ceil() and Floor()

Bakhtiyor Zokhidov bakhtiyor_zokhidov at mail.ru
Mon May 20 11:54:26 EDT 2013

``` thanks a lot

>>>new_ceil(-0.24, 0.25)
-0.0
???

Понедельник, 20 мая 2013, 16:37 +01:00 от Robert Kern <robert.kern at gmail.com>:
>On Mon, May 20, 2013 at 4:21 PM, Bakhtiyor Zokhidov
>< bakhtiyor_zokhidov at mail.ru > wrote:
>> Hello,
>>
>> I am using ceil() and floor() function to get upper and lower value of some
>> numbers. Let's say:
>>
>> import math
>> x1 = 0.35
>> y1 = 4.46
>>>>> math.ceil(x1)
>> 1.0
>>
>>>>> math.floor(y1)
>> 4.0
>>
>> The problem is that If I want to get upper and lower values for the certain
>> step, for example, step = 0.25, ceil() function should give:
>> new_ceil(x1, step) => 0.5
>> new_floor(y1, step) => 4.25
>> Because, the step is 0.25
>>
>> Question: How I can I achieve those results by using ceil() and floor()
>> function, or Is there any equvalent function for that?
>
>For most purposes, the following functions suffice:
>
>def new_ceil(x, step):
>    return math.ceil(x / step) * step
>
>def new_floor(x, step):
>    return math.floor(x / step) * step
>
>
>Alternately:
>
>def new_ceil(x, step):
>    quotient = x // step
>    remainder = x % step
>    return (quotient + (remainder > 0)) * step
>
>def new_floor(x, step):
>    quotient = x // step
>    return quotient * step
>
>
>Floating point representation errors and accumulated floating point
>arithmetic inaccuracies may give you unexpected results in many cases,
>so be careful.
>
>--
>Robert Kern
>_______________________________________________
>NumPy-Discussion mailing list
>NumPy-Discussion at scipy.org
>http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130520/d5a6f136/attachment.html>
```