Python浮动到int转换 [英] Python float to int conversion
问题描述
下面是我正在执行的代码:
/ p>
x = 2.51
print(------ --- --- 251.0)
$ p
y = 251.0
print(y)
print(int(y))
print(-------- - 2.51 * 100)
y = x * 100
print(y)
print(int(y))
print(------ --- 2.51 * 1000/10)
y = x * 1000/10
print(y)
print(int(y))
print --------- 2.51 * 100 * 10/10)
y = x * 100 * 10/10
print(y)
print(int(y))
x = 4.02
print(--------- 402.0)
y = 402.0
print(y)
print(int y))
print(--------- 4.02 * 100)
y = x * 100
print(y)
print int(y))
print(--------- 4.02 * 1000/10)
y = x * 1000/10
print(y)
print(int(y))
print(--------- 4.02 * 100 * 10/10)
y = x * 100 * 10 / 10
print(y)
print(int(y))
结果如下(第一个值是操作的结果,第二个值是int()):
--------- 251.0
251.0
251
--------- 2.51 * 100
251.0
250
--------- 2.51 * 1000/10
251.0
251
-------- - 2.51 * 100 * 10/10
251.0
250
--------- 402.0
402.0
402
---- ----- 4.02 * 100
402.0
401
--------- 4.02 * 1000/10
402.0
401
--------- 4.02 * 100 * 10/10
402.0
401
2.51和4.02是导致2.50 - > 5.00范围内出现奇怪行为的唯一值。在给定相同操作的情况下,该范围内的每个其他两位数值都可以毫无问题地转换为int。
所以,我错过了什么导致了这些结果呢?我使用Python 2.7.2。
解决方案
2.51 * 100 = 250.999999999997
int()函数简单地截断小数点处的数字, p>
int(round(2.51 * 100))
得到251为一个整数。一般来说,浮点数不能精确表示。因此,应该小心舍入误差。如上所述,这不是Python特有的问题。这是所有计算机语言中反复出现的问题。
Basically, I'm converting a float to an int, but I don't always have the expected value.
Here's the code I'm executing:
x = 2.51
print("--------- 251.0") y = 251.0 print(y) print(int(y)) print("--------- 2.51 * 100") y = x * 100 print(y) print(int(y)) print("--------- 2.51 * 1000 / 10") y = x * 1000 / 10 print(y) print(int(y)) print("--------- 2.51 * 100 * 10 / 10") y = x * 100 * 10 / 10 print(y) print(int(y)) x = 4.02 print("--------- 402.0") y = 402.0 print(y) print(int(y)) print("--------- 4.02 * 100") y = x * 100 print(y) print(int(y)) print("--------- 4.02 * 1000 / 10") y = x * 1000 / 10 print(y) print(int(y)) print("--------- 4.02 * 100 * 10 / 10") y = x * 100 * 10 / 10 print(y) print(int(y))
And here's the result (first value is the result of the operation, second value is int() of the same operation):
--------- 251.0 251.0 251 --------- 2.51 * 100 251.0 250 --------- 2.51 * 1000 / 10 251.0 251 --------- 2.51 * 100 * 10 / 10 251.0 250 --------- 402.0 402.0 402 --------- 4.02 * 100 402.0 401 --------- 4.02 * 1000 / 10 402.0 401 --------- 4.02 * 100 * 10 / 10 402.0 401
2.51 and 4.02 are the only values that lead to that strange behaviour on the 2.50 -> 5.00 range. Every other two digits value in that range converts to int without any problem when given the same operations.
So, what am I missing that leads to those results? I'm using Python 2.7.2 by the way.
解决方案2.51 * 100 = 250.999999999997
The int() function simply truncates the number at the decimal point, giving 250. Use
int(round(2.51*100))
to get 251 as an integer. In general, floating point numbers cannot be represented exactly. One should therefore be careful of round-off errors. As mentioned, this is not a Python-specific problem. It's a recurring problem in all computer languages.
这篇关于Python浮动到int转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!