是int(round(val))安全吗? [英] is int(round(val)) safe?

查看:75
本文介绍了是int(round(val))安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这可能是一个愚蠢的问题,但是......使用int(round(val))将
四舍五入到最接近的整数是否安全?


我怀疑它没问题,但是想确保在某些平台上出现奇怪的浮点数/ b $ b表示可能会使它不安全(即获得

错误值由于浮点值是近似值而且

如果是这样,是否有更好的方法)。


- Russell

I realize this probably a stupid question, but...is it safe to
round to the nearest integer by using int(round(val))?

I suspect it is fine, but wanted to be sure that weird floating point
representation on some platform might make it unsafe there (i.e. get the
wrong value due to the floating point value being an approximation) and
if so, is there a Better Way).

-- Russell

推荐答案

Russell E. Owen写道:
Russell E. Owen wrote:
我意识到这可能是一个愚蠢的问题,但......这是否安全?使用int(round(val))到最接近的整数?


定义安全。

我怀疑它很好,但是想确保在某些平台上出现奇怪的浮点表示可能使那里不安全(即由于浮点值是一个近似值得到错误的值)和
如果是这样,是否有更好的方法)。
I realize this probably a stupid question, but...is it safe to
round to the nearest integer by using int(round(val))?
Define "safe".
I suspect it is fine, but wanted to be sure that weird floating point
representation on some platform might make it unsafe there (i.e. get the
wrong value due to the floating point value being an approximation) and
if so, is there a Better Way).



因为round()返回一个整数,并且因为(实际上忽略了
大整数,因为我怀疑你关心它们

这里)浮动点值可以处理*整数*值

非常好,没有理由它不应该做你想要的b $ b。

带浮点的问题*是不准确的表示

某些_fractional_值,而不是整数值。


-Peter


*嗯,_one_的问题。 ;-)



Since round() returns an integer, and since (ignoring really
large integers, since I doubt you''re concerned about them
here) floating point values can handle *integer* values
perfectly well, there''s no reason it shouldn''t do what you
want.

The problem* with floating point is inaccurate representation
of certain _fractional_ values, not integer values.

-Peter

* Well, _one_ of the problems. ;-)


Peter Hansen写道:
Peter Hansen wrote:
因为round()返回一个整数
Since round() returns an integer




只是为了澄清以防其他人误读这个,我相信这个意图

这里是说圆(f) - 也就是说,只有一个回合

参数 - 返回一个没有小数部分的浮点数
小数点后的
。结果仍然是浮点数,但不是int:



Just to clarify in case anyone else misreads this, I belive the intent
here was to say that round(f) -- that is, round with only a single
argument -- returns a floating point number with no fractional component
after the decimal point. The result is still a float, not an int though:

round(3.14159265359)
3.0 round (3.14159265359,2)
round(3.14159265359) 3.0 round(3.14159265359, 2)



3.1400000000000001


我认为其余部分的意图是明确的,但我认为

对于任何像我这样误读它的新手来澄清这一点并不会有什么坏处。


Steve


3.1400000000000001

I think the intent was clear from the rest of the post, but I figured it
wouldn''t hurt to clarify this for any newbies who misread it like I did.

Steve


Steven Bethard写道:
Steven Bethard wrote:
Peter Hansen写道:
Peter Hansen wrote:
因为round()返回一个整数
Since round() returns an integer


这就是说圆(f) - 也就是说,只用一个
参数回合 - 返回一个浮点小数点后没有小数分量的数字。结果仍然是浮点数,而不是整数:



Just to clarify in case anyone else misreads this, I belive the intent
here was to say that round(f) -- that is, round with only a single
argument -- returns a floating point number with no fractional component
after the decimal point. The result is still a float, not an int though:

>>> round(3.14159265359)3.0>>> round(3.14159265359,2)
>>> round(3.14159265359) 3.0 >>> round(3.14159265359, 2)


3.1400000000000001

我觉得这个帖子的其余部分的意图很清楚,但我认为它不会'对于那些像我一样误读它的新手来说,这是有害的。


3.1400000000000001

I think the intent was clear from the rest of the post, but I figured it
wouldn''t hurt to clarify this for any newbies who misread it like I did.




一切都是真的。


我想知道是否沿着''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

显然,数学家会对这个问题说些什么。在计算机中,1.0可能不是整数数据类型,但

我认为在数学中它仍被视为整数。我绝对不会在这方面声称拥有权威,但是,

因为作为工程师我认为1.0和1只是等于一个

第一次近似值。 < wink>


-Peter



All true.

I wonder if it would be appropriate to say something along
the lines of ''''''round() returns an integer, but not an "int".''''''

Obviously the mathematicians will have something to say about
this. In computers, 1.0 may not be an integer data type, but
I think in math it''s still considered an integer. I am most
definitely not going to claim authority in this area, however,
since as an engineer I consider 1.0 and 1 merely "equal to a
first approximation". <wink>

-Peter


这篇关于是int(round(val))安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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