Python 3.x 舍入行为 [英] Python 3.x rounding behavior

查看:51
本文介绍了Python 3.x 舍入行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚重新阅读了Python 3.0 的新功能,它指出:

I was just re-reading What’s New In Python 3.0 and it states:

round() 函数的舍入策略和返回类型已更改.确切的中途情况现在四舍五入到最接近的偶数结果远离零.(例如,round(2.5) 现在返回 2 而不是3.)

The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.)

round 的文档:

对于支持 round() 的内置类型,值四舍五入到最接近 10 的幂减 n 的倍数;如果两个倍数是同样接近,四舍五入是朝着偶数选择

For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus n; if two multiples are equally close, rounding is done toward the even choice

因此,在 v2.7.3 下:

In [85]: round(2.5)
Out[85]: 3.0

In [86]: round(3.5)
Out[86]: 4.0

正如我所料.但是,现在在 v3.2.3 下:

as I'd have expected. However, now under v3.2.3:

In [32]: round(2.5)
Out[32]: 2

In [33]: round(3.5)
Out[33]: 4

这似乎违反直觉,与我的理解相反四舍五入(并且必然会绊倒人).英语不是我的母语,但直到我读到这个,我以为我知道四舍五入意味着什么:-/我确定在引入 v3 时,肯定有一些讨论这个,但我在搜索中找不到很好的理由.

This seems counter-intuitive and contrary to what I understand about rounding (and bound to trip up people). English isn't my native language but until I read this I thought I knew what rounding meant :-/ I am sure at the time v3 was introduced there must have been some discussion of this, but I was unable to find a good reason in my search.

  1. 有没有人知道为什么会变成这样?
  2. 是否有任何其他主流编程语言(例如,C、C++、Java、Perl, ..)进行这种(对我来说不一致的)四舍五入?
  1. Does anyone have insight into why this was changed to this?
  2. Are there any other mainstream programming languages (e.g., C, C++, Java, Perl, ..) that do this sort of (to me inconsistent) rounding?

我在这里遗漏了什么?

更新:@Li-aungYip 对银行家的舍入"的评论给了我正确的搜索词/关键字来搜索,我发现了这个问题:为什么.NET 使用银行家的舍入作为默认值?,所以我会仔细阅读.

UPDATE: @Li-aungYip's comment re "Banker's rounding" gave me the right search term/keywords to search for and I found this SO question: Why does .NET use banker's rounding as default?, so I will be reading that carefully.

推荐答案

Python 3 的方式(称为四舍五入到偶数"或银行家的四舍五入")现在被认为是标准的四舍五入方法,尽管一些语言实现还没有出现.

Python 3's way (called "round half to even" or "banker's rounding") is considered the standard rounding method these days, though some language implementations aren't on the bus yet.

简单的总是向上取整 0.5"技术导致对较高数字的轻微偏差.对于大量计算,这可能很重要.Python 3.0 方法消除了这个问题.

The simple "always round 0.5 up" technique results in a slight bias toward the higher number. With large numbers of calculations, this can be significant. The Python 3.0 approach eliminates this issue.

常用的舍入方法不止一种.IEEE 754 是浮点数学的国际标准,定义了五种不同的舍入方法(Python 3.0 使用的一个是默认值).还有还有其他的.

There is more than one method of rounding in common use. IEEE 754, the international standard for floating-point math, defines five different rounding methods (the one used by Python 3.0 is the default). And there are others.

这种行为并没有像应有的那样广为人知.如果我没记错的话,AppleScript 是这种舍入方法的早期采用者.round 命令 提供了几个选项,但在 IEEE 754 中,round-toward-even 是默认设置.显然是实现 round<的工程师/code> 命令对让它像我在学校学到的那样工作"的所有请求都感到厌烦了;他就是这样实现的:round 2.5 rounding as in school 是一个有效的 AppleScript 命令.:-)

This behavior is not as widely known as it ought to be. AppleScript was, if I remember correctly, an early adopter of this rounding method. The round command in AppleScript offers several options, but round-toward-even is the default as it is in IEEE 754. Apparently the engineer who implemented the round command got so fed up with all the requests to "make it work like I learned in school" that he implemented just that: round 2.5 rounding as taught in school is a valid AppleScript command. :-)

这篇关于Python 3.x 舍入行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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