<HTML><BODY>thanks a lot<br><br>what about the following example:<br>>>>new_ceil(-0.24, 0.25)<br>-0.0<br>???<br>thanks in advance for the reply<br><br><br>Понедельник, 20 мая 2013, 16:37 +01:00 от Robert Kern <robert.kern@gmail.com>:<br>
<blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;" class="mailru-blockquote">
        <div id="">
        



    






        

        
        
        
        
        

        



<div class="js-helper js-readmsg-msg">
        <style type="text/css"></style>
        <div id="style_13690642720000000725" class="mr_read__body">
                <base target="_self" href="https://e.mail.ru/cgi-bin/">
                
                        <div id="style_13690642720000000725_BODY">On Mon, May 20, 2013 at 4:21 PM, Bakhtiyor Zokhidov<br>
<<a href="sentmsg?compose&To=bakhtiyor_zokhidov@mail.ru">bakhtiyor_zokhidov@mail.ru</a>> wrote:<br>
> Hello,<br>
><br>
> I am using ceil() and floor() function to get upper and lower value of some<br>
> numbers. Let's say:<br>
><br>
> import math<br>
> x1 = 0.35<br>
> y1 = 4.46<br>
>>>> math.ceil(x1)<br>
> 1.0<br>
><br>
>>>> math.floor(y1)<br>
> 4.0<br>
><br>
> The problem is that If I want to get upper and lower values for the certain<br>
> step, for example, step = 0.25, ceil() function should give:<br>
> new_ceil(x1, step) => 0.5<br>
> new_floor(y1, step) => 4.25<br>
> Because, the step is 0.25<br>
><br>
> Question: How I can I achieve those results by using ceil() and floor()<br>
> function, or Is there any equvalent function for that?<br>
<br>
For most purposes, the following functions suffice:<br>
<br>
def new_ceil(x, step):<br>
    return math.ceil(x / step) * step<br>
<br>
def new_floor(x, step):<br>
    return math.floor(x / step) * step<br>
<br>
<br>
Alternately:<br>
<br>
def new_ceil(x, step):<br>
    quotient = x // step<br>
    remainder = x % step<br>
    return (quotient + (remainder > 0)) * step<br>
<br>
def new_floor(x, step):<br>
    quotient = x // step<br>
    return quotient * step<br>
<br>
<br>
Floating point representation errors and accumulated floating point<br>
arithmetic inaccuracies may give you unexpected results in many cases,<br>
so be careful.<br>
<br>
--<br>
Robert Kern<br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="sentmsg?compose&To=NumPy%2dDiscussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</div>
                        
                
                <base target="_self" href="https://e.mail.ru/cgi-bin/">
        </div>

        
</div>


</div>
</blockquote>
<br></BODY></HTML>