Equvalent function for Ceil() and Floor()

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? -- Bakhti

On Mon, May 20, 2013 at 4:21 PM, Bakhtiyor Zokhidov <bakhtiyor_zokhidov@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?
def new_ceil(x, step): return np.ceil(x / step) * step ? -n

On Mon, May 20, 2013 at 4:21 PM, Bakhtiyor Zokhidov <bakhtiyor_zokhidov@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

new_ceil(-0.24, 0.25) -0.0 ???
thanks a lot what about the following example: thanks in advance for the reply Понедельник, 20 мая 2013, 16:37 +01:00 от Robert Kern <robert.kern@gmail.com>:
On Mon, May 20, 2013 at 4:21 PM, Bakhtiyor Zokhidov < bakhtiyor_zokhidov@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@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Mon, May 20, 2013 at 4:54 PM, Bakhtiyor Zokhidov <bakhtiyor_zokhidov@mail.ru> wrote:
thanks a lot
new_ceil(-0.24, 0.25) -0.0 ???
what about the following example: thanks in advance for the reply
What about it? What were you expecting to get? What do you think is wrong about the answer you did get? -- Robert Kern

On Mon, May 20, 2013 at 8:54 AM, Bakhtiyor Zokhidov <bakhtiyor_zokhidov@mail.ru> wrote:
what about the following example:
new_ceil(-0.24, 0.25) -0.0
ceil rounds toward +inf (and floor towards -inf) -- this is exactly what you want if you're doing what I think you are...(note that round() rounds towards and away from zero -- so different for negative numbers...) the minus sign means "minus 0.0", which is a seemingly odd floating point thing -- but FP numbers have a sign bit, which, in this case, is set to negative, even though, mathematically speaking, 0.0 doesn't have a sign. But it will compare to 0.0 as you'd expect: In [8]: -0.0 == 0.0 Out[8]: True In [9]: -0.0 < 0.0 Out[9]: False -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

ok... I think -0.0 is mathematically wrong but in a program it is true. What I suspect is that if -0.0 doesn't affect on result (e.g., 2*(-0.0 + 2) or (-0.0-2)*2 ) . If it does not affect results it would be good for me Thanks Понедельник, 20 мая 2013, 9:03 -07:00 от Chris Barker - NOAA Federal <chris.barker@noaa.gov>:
On Mon, May 20, 2013 at 8:54 AM, Bakhtiyor Zokhidov < bakhtiyor_zokhidov@mail.ru > wrote:
what about the following example:
new_ceil(-0.24, 0.25) -0.0
ceil rounds toward +inf (and floor towards -inf) -- this is exactly what you want if you're doing what I think you are...(note that round() rounds towards and away from zero -- so different for negative numbers...)
the minus sign means "minus 0.0", which is a seemingly odd floating point thing -- but FP numbers have a sign bit, which, in this case, is set to negative, even though, mathematically speaking, 0.0 doesn't have a sign. But it will compare to 0.0 as you'd expect:
In [8]: -0.0 == 0.0 Out[8]: True
In [9]: -0.0 < 0.0 Out[9]: False
-Chris
--
Christopher Barker, Ph.D. Oceanographer
Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Mon, May 20, 2013 at 6:58 PM, Bakhtiyor Zokhidov <bakhtiyor_zokhidov@mail.ru> wrote:
ok... I think -0.0 is mathematically wrong but in a program it is true.
What I suspect is that if -0.0 doesn't affect on result (e.g., 2*(-0.0 + 2) or (-0.0-2)*2 ) . If it does not affect results it would be good for me
It doesn't affect those computations, no. It does have different results for division (1/-0.0 -> -inf), and it will often determine which branch gets evaluated for a complex function that has a branch point at the origin. These are usually considered good things and are the primary reason that floating point includes signed zeros. http://en.wikipedia.org/wiki/Signed_zero -- Robert Kern
participants (4)
-
Bakhtiyor Zokhidov
-
Chris Barker - NOAA Federal
-
Nathaniel Smith
-
Robert Kern