简体   繁体   English

为什么舍入浮点数1.4999999999999999产生2?

[英]Why does rounding the floating-point number 1.4999999999999999 produce 2?

I've been reading a book Write Great code - Understanding the Machine . 我一直在读一本书写好代码 - 理解机器 In the section about rounding it says: 关于四舍五入的部分说:

Numbers should be rounded to the smallest bigger number if the decimal bit value is more than or equal half the total decimal value that can be represented. 如果十进制位值大于或等于可以表示的总十进制值的一半,则数字应舍入为最小的较大数字。

which means: 意思是:

round(1.5) // equals 2
round(1.49) // equals 1

but when I tried this with Python: 但是当我用Python尝试这个时:

x1 = 1.4999  # rounds to 1

x2 = 1.4999999999999999  # rounds to 2

print(round(x1))

print(round(x2))

the output was: 输出是:

1 1

2 2

I tried the same thing with C# and Swift and it gave the same output. 我用C#和Swift尝试了同样的东西,它给出了相同的输出。 So I assume it's a language-agnostic topic. 所以我认为这是一个与语言无关的话题。

But why does this happen? 但为什么会这样呢?

My assumption is that the floating-point unit rounds the extra bits which convert the "1.4999999999999999999" to "1.5" before applying the programmer's rounding. 我的假设是浮点单元对应用程序员舍入之前将“1.4999999999999999999”转换为“1.5”的额外位进行舍入。

In x2 = 1.4999999999999999 and print(round(x2)) , there are two operations that affect the value. x2 = 1.4999999999999999print(round(x2)) ,有两个操作会影响该值。 The round function cannot operate directly on the number 1.4999999999999999 or the numeral “1.4999999999999999”. round函数不能直接在数字1.4999999999999999或数字“1.4999999999999999”上操作。 Its operand must be in the floating-point format that the Python implementation uses. 其操作数必须采用Python实现使用的浮点格式。

So, first, 1.4999999999999999 is converted to the floating-point format. 因此,首先,将1.4999999999999999转换为浮点格式。 Python is not strict about which floating-point format a Python implementation uses, but the IEEE-754 basic 64-bit binary format is common. Python对Python实现使用哪种浮点格式并不严格,但IEEE-754基本64位二进制格式很常见。 In this format, the closest representable values to 1.4999999999999999 are 1.5 and 1.4999999999999997779553950749686919152736663818359375. 在此格式中,最接近的可表示值为1.4999999999999999为1.5和1.4999999999999997779553950749686919152736663818359375。 The former is closer to 1.4999999999999999 than the latter is, so the former is used. 前者比后者更接近1.4999999999999999,所以使用前者。

Thus, converting 1.4999999999999999 to the floating-point format produces 1.5. 因此,将1.4999999999999999转换为浮点格式会产生1.5。 Then round(1.5) produces 2. 然后round(1.5)产生2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于python的舍入误差和浮点数 - About python's rounding error and floating-point numbers 为什么在舍入浮点数时python返回0? - Why python returns 0 when rounding a floating point number? 很好地代表python中的浮点数 - Nicely representing a floating-point number in python 使用浮点数作为键的Firebase - Firebase Using Floating-Point Number as Key 为什么在浮点算术中0.9999999999999999!= 1但0.9999999999999999 + 1 == 2 - Why, in floating-point arithmetic, 0.9999999999999999 !=1 but 0.9999999999999999 + 1 == 2 将64位浮点格式字符串转换为浮点数 - Convert 64-bits floating-point format string to floating-point number 给定我们具有round()函数的原因,为什么还要麻烦量化()一个浮点数呢? - Why bother quantize() a floating-point number given that we have round() function? 为什么浮点值 4*0.1 在 Python 3 中看起来不错,而 3*0.1 则不然? - Why does the floating-point value of 4*0.1 look nice in Python 3 but 3*0.1 doesn't? Python 将浮点数四舍五入到最接近的 0.05 - Python rounding a floating point number to nearest 0.05 为什么子类的浮点变量和父类的浮点变量class的地址是同一个memory地址? - Why is the floating-point variable of the subclass the same memory address as the floating-point variable of the parent class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM