将数字舍入到最接近的偶数 [英] Rounding a number to nearest even

查看:86
本文介绍了将数字舍入到最接近的偶数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内置函数round()将始终向上舍入,即1.5是

舍入为2.0,2.5舍入为3.0。


如果我想要舍入到最近的偶数,那就是


my_round(1.5)= 2#正如预期的那样

my_round(2.5)= 2 #Not 3,这是一个奇怪的数字


我对舍入x.5形式的数字很感兴趣。取决于

x是奇数还是偶数。关于如何实现它的任何想法?

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 ?

推荐答案

On 11 avr,12:14,bdsatish< bdsat ... @ gmail.comwrote:
On 11 avr, 12:14, bdsatish <bdsat...@gmail.comwrote:

内置函数round()将始终向上舍入,即1.5是

四舍五入为2.0,2.5为四舍五入到3.0。


如果我想要舍入到最近的偶数,那就是


my_round(1.5)= 2#正如预期的那样/>
my_round(2.5)= 2#不是3,这是一个奇怪的数字


我对舍入x.5形式的数字感兴趣取决于

x是奇数还是偶数。有关如何实现它的任何想法?
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 ?



当你说舍入到最近的偶数时,你的意思是new_round(3)< 3?


是这样,你可以尝试:


在[37]中:def new_round(x):

....:返回回合(x / 2。 )* 2

....:


在[38]中:new_round(1.5)

Out [38]: 2.0


在[39]:new_round(2.5)

Out [39]:2.0


在[ 40]:new_round(3)

Out [40]:4.0

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


4月11日下午3:27,colas.fran .. 。@ gmail.com写道:
On Apr 11, 3:27 pm, colas.fran...@gmail.com wrote:

11 avr,12:14,bdsatish< bdsat ... @ gmail.comwrote:
On 11 avr, 12:14, bdsatish <bdsat...@gmail.comwrote:

内置函数round()将始终向上舍入,即1.5是

舍入为2.0,2.5舍入为3.0。
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#正如预期的那样

my_round(2.5)= 2#Not 3,这是一个奇数num
my_round(1.5) = 2 # As expected
my_round(2.5) = 2 # Not 3, which is an odd num


我对四舍五入x.5形式的数字很感兴趣。取决于

x是奇数还是偶数。有关如何实现它的任何想法?
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 ?



当你说舍入到最近的偶数时,你的意思是new_round(3)< 3?


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



完全没有。条款最接近偶数只有当数字的形式为x.5时,才会出现图片。或者它与内置round()相同。

new_round(3.0)必须是3.0本身。这是我想要的数学定义




如果''n''是一个整数,


new_round(n + 0.5)= n如果n / 2是整数

new_round(n + 0.5)=(n + 1)if(n + 1)/ 2是整数


在所有其他情况下,new_round()的行为与round()相似。这是

我期望的结果:


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


内置功能无法满足我对圆形(-2.5)或

轮(2.5)的需求

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)


你不能这么做。


#untested

new_round(n):

回答=回合(n)

#现在回答奇数

如果回答%2:

返回答案 - 1

else:

返回答案
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆