Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Rounding a number to nearest even (http://www.velocityreviews.com/forums/t612226-rounding-a-number-to-nearest-even.html)

bdsatish 04-11-2008 10:14 AM

Rounding a number to nearest even
 
The built-in function round( ) will always "round up", that is 1.5 is
rounded to 2.0 and 2.5 is rounded to 3.0.

If I want to round to the nearest even, that is

my_round(1.5) = 2 # As expected
my_round(2.5) = 2 # Not 3, which is an odd num

I'm interested in rounding numbers of the form "x.5" depending upon
whether x is odd or even. Any idea about how to implement it ?

colas.francis@gmail.com 04-11-2008 10:27 AM

Re: Rounding a number to nearest even
 
On 11 avr, 12:14, bdsatish <bdsat...@gmail.com> wrote:
> The built-in function round( ) will always "round up", that is 1.5 is
> rounded to 2.0 and 2.5 is rounded to 3.0.
>
> If I want to round to the nearest even, that is
>
> my_round(1.5) = 2 # As expected
> my_round(2.5) = 2 # Not 3, which is an odd num
>
> I'm interested in rounding numbers of the form "x.5" depending upon
> whether x is odd or even. Any idea about how to implement it ?


When you say "round to the nearest even", you mean new_round(3) <> 3?

Is so, you can try:

In [37]: def new_round(x):
....: return round(x/2.)*2
....:

In [38]: new_round(1.5)
Out[38]: 2.0

In [39]: new_round(2.5)
Out[39]: 2.0

In [40]: new_round(3)
Out[40]: 4.0

bdsatish 04-11-2008 11:07 AM

Re: Rounding a number to nearest even
 
On Apr 11, 3:27 pm, colas.fran...@gmail.com wrote:
> On 11 avr, 12:14, bdsatish <bdsat...@gmail.com> wrote:
>
> > The built-in function round( ) will always "round up", that is 1.5 is
> > rounded to 2.0 and 2.5 is rounded to 3.0.

>
> > If I want to round to the nearest even, that is

>
> > my_round(1.5) = 2 # As expected
> > my_round(2.5) = 2 # Not 3, which is an odd num

>
> > I'm interested in rounding numbers of the form "x.5" depending upon
> > whether x is odd or even. Any idea about how to implement it ?

>
> When you say "round to the nearest even", you mean new_round(3) <> 3?


No. not at all. The clause "nearest even" comes into picture only when
a number is of form "x.5" or else it's same as builtin round( ).
new_round(3.0) must be 3.0 itself. Here is the mathematical definition
of what I want:

If 'n' is an integer,

new_round(n+0.5) = n if n/2 is integer
new_round(n+0.5) = (n+1) if (n+1)/2 is integer

In all other cases, new_round() behave similarly as round( ). Here are
the results I expect:

new_round(3.2) = 3
new_round(3.6) = 4
new_round(3.5) = 4
new_round(2.5) = 2
new_round(-0.5) = 0.0
new_round(-1.5) = -2.0
new_round(-1.3) = -1.0
new_round(-1.8) = -2
new_round(-2.5) = -2.0

The built-in function doesnt meet my needs for round(-2.5) or
round(2.5)

cokofreedom@gmail.com 04-11-2008 11:19 AM

Re: Rounding a number to nearest even
 
couldn't you just do.

#untested
new_round(n):
answer = round(n)
# is answer now odd
if answer % 2:
return answer - 1
else:
return answer

cokofreedom@gmail.com 04-11-2008 11:24 AM

Re: Rounding a number to nearest even
 
On Apr 11, 1:19 pm, cokofree...@gmail.com wrote:
> couldn't you just do.
>
> #untested
> new_round(n):
> answer = round(n)
> # is answer now odd
> if answer % 2:
> return answer - 1
> else:
> return answer


Whoops, this also affects odd numbers...

Will try and find a GOOD solution later...

Strange request though, why do you need it that way, because 2.5 is
CLOSER to 3 than to 2...

bdsatish 04-11-2008 11:27 AM

Re: Rounding a number to nearest even
 
On Apr 11, 4:19 pm, cokofree...@gmail.com wrote:
> couldn't you just do.
>
> #untested
> new_round(n):
> answer = round(n)
> # is answer now odd
> if answer % 2:
> return answer - 1
> else:
> return answer


It fails for negative numbers: For -2.5 it gives -4.0 as answer
whereas I expect -2.0

bdsatish 04-11-2008 11:32 AM

Re: Rounding a number to nearest even
 
On Apr 11, 4:24 pm, cokofree...@gmail.com wrote:
> On Apr 11, 1:19 pm, cokofree...@gmail.com wrote:
>
> > couldn't you just do.

>
> > #untested
> > new_round(n):
> > answer = round(n)
> > # is answer now odd
> > if answer % 2:
> > return answer - 1
> > else:
> > return answer

>
> Whoops, this also affects odd numbers...
>
> Will try and find a GOOD solution later...
>
> Strange request though, why do you need it that way, because 2.5 is
> CLOSER to 3 than to 2...


It also fails for negative numbers. For -2.5 as input, I get -4.0
whereas I expect -2.0

This is a lengthy solution I came-up with:

def round_even(x):
temp = round(abs(x))
if (abs(x) - 0.5)%2.0 == 0.0: temp=temp-1
return signum(x)*temp

def signum(x):
if x>0: return 1
if x<0: return -1
return 0

But i guess there are better ways. I need it 'cos I'm translating some
code from Mathematica to Python. And Math..ica's Round[ ] behaves this
way (as I requested)

bdsatish 04-11-2008 11:42 AM

Re: Rounding a number to nearest even
 
On Apr 11, 4:37 pm, Scott David Daniels <Scott.Dani...@Acm.Org> wrote:
> bdsatish wrote:
> > The built-in function round( ) will always "round up", that is 1.5 is

> def rounded(v):
> rounded = round(v)
> if divmod(v, 1)[1] == .5 and divmod(rounded, 2)[1] == 1:
> if v > 0:
> return rounded - 1
> return rounded + 1
> return rounded
>
> last = None
> for n in range(-29, 28):
> x = n * .25
> r = xr(x)
> if r != last:
> last = r
> print
> print '%s->%s' % (x, xr(x)),
>


Hi Scott,
This is what I was looking for.. I forgot about divmod( ) thanks for
reminding.

Gerard Flanagan 04-11-2008 12:05 PM

Re: Rounding a number to nearest even
 
On Apr 11, 12:14 pm, bdsatish <bdsat...@gmail.com> wrote:
> The built-in function round( ) will always "round up", that is 1.5 is
> rounded to 2.0 and 2.5 is rounded to 3.0.
>
> If I want to round to the nearest even, that is
>
> my_round(1.5) = 2 # As expected
> my_round(2.5) = 2 # Not 3, which is an odd num
>
> I'm interested in rounding numbers of the form "x.5" depending upon
> whether x is odd or even. Any idea about how to implement it ?


------------------------------------------------
def myround(x):
n = int(x)
if abs(x - n) == 0.5:
if n % 2:
#it's odd
return n + 1 - 2 * int(n<0)
else:
return n
else:
return round(x)

assert myround(3.2) == 3
assert myround(3.6) == 4
assert myround(3.5) == 4
assert myround(2.5) == 2
assert myround(-0.5) == 0.0
assert myround(-1.5) == -2.0
assert myround(-1.3) == -1.0
assert myround(-1.8) == -2
assert myround(-2.5) == -2.0
------------------------------------------------

Gerard Flanagan 04-11-2008 12:14 PM

Re: Rounding a number to nearest even
 
On Apr 11, 2:05 pm, Gerard Flanagan <grflana...@gmail.com> wrote:
> On Apr 11, 12:14 pm, bdsatish <bdsat...@gmail.com> wrote:
>
> > The built-in function round( ) will always "round up", that is 1.5 is
> > rounded to 2.0 and 2.5 is rounded to 3.0.

>
> > If I want to round to the nearest even, that is

>
> > my_round(1.5) = 2 # As expected
> > my_round(2.5) = 2 # Not 3, which is an odd num

>
> > I'm interested in rounding numbers of the form "x.5" depending upon
> > whether x is odd or even. Any idea about how to implement it ?

>
> ------------------------------------------------
> def myround(x):
> n = int(x)
> if abs(x - n) == 0.5:
> if n % 2:
> #it's odd
> return n + 1 - 2 * int(n<0)
> else:
> return n
> else:
> return round(x)
>
> ------------------------------------------------


In fact you can avoid the call to the builtin round:

------------------------------------------------
def myround(x):
n = int(x)
if abs(x - n) >= 0.5 and n % 2:
return n + 1 - 2 * int(n<0)
else:
return n

assert myround(3.2) == 3
assert myround(3.6) == 4
assert myround(3.5) == 4
assert myround(2.5) == 2
assert myround(-0.5) == 0.0
assert myround(-1.5) == -2.0
assert myround(-1.3) == -1.0
assert myround(-1.8) == -2
assert myround(-2.5) == -2.0
------------------------------------------------


All times are GMT. The time now is 08:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.