python中整数除法和float转int的区别是什么原因?
What is the reason for difference between integer division and float to int conversion in python?
我最近注意到 int()
rounds a float towards 0, while integer division 将浮子朝向其底部旋转。
例如:
-7 // 2 == -4
int(-7/2) == -3
我已阅读指定的文档:
class int(x, base=10)
Return an integer object constructed from a number or string x, or return 0 if no arguments are >given. If x is a number, return x.__int__()
. For floating point numbers, this truncates towards zero.
和:
floor division
Mathematical division that rounds down to nearest integer. The floor division operator is //
. For example, the expression 11 // 4
evaluates to 2 in contrast to the 2.75
returned by float true division. Note that (-11) // 4
is -3
because that is -2.75
rounded downward. See PEP 238.
但我觉得 2 个类似的操作(浮点除法为整数)应该 return 不同的结果似乎不合逻辑。
函数之间的差异有什么动机吗?
一致性。
您需要遵循一些非常基本且看似无关紧要的解释才能理解它。
你在学校学过除法和余数。你已经做了这样的计算:
8 ÷ 4 = 2 R 0
7 ÷ 4 = 1 R 3
6 ÷ 4 = 1 R 2
5 ÷ 4 = 1 R 1
4 ÷ 4 = 1 R 0
3 ÷ 4 = 0 R 3
2 ÷ 4 = 0 R 2
1 ÷ 4 = 0 R 1
0 ÷ 4 = 0 R 0
^------ This is the result of x // 4
^-- This is the result of x % 4 (modulo)
后来,你学会了实数除法:
8 ÷ 4 = 2.0
7 ÷ 4 = 1.75
6 ÷ 4 = 1.5
5 ÷ 4 = 1.25
4 ÷ 4 = 1.0
3 ÷ 4 = 0.75
2 ÷ 4 = 0.5
1 ÷ 4 = 0.25
0 ÷ 4 = 0.0
^--- Note that the number in front of the . is int(x/4)
到目前为止,您可能认为 x // 4
和 int(x/4)
总是给出相同的结果。这是你目前对情况的理解。
但是,看看整数除法会发生什么:R后面的数字从3,2,1循环到0然后重新开始:3,2,1,0。R前面的数字递减每 4 步。
那么,接下来会怎样?
8 ÷ 4 = 2 R 0
7 ÷ 4 = 1 R 3
6 ÷ 4 = 1 R 2
5 ÷ 4 = 1 R 1
4 ÷ 4 = 1 R 0
3 ÷ 4 = 0 R 3
2 ÷ 4 = 0 R 2
1 ÷ 4 = 0 R 1
0 ÷ 4 = 0 R 0
-1 ÷ 4 = -1 R 3
^------ We have to decrease now, because we already have 0 four times
^-- We have to restart the cycle at 3
同时实数除法得到:
-1 ÷ 4 = -0.25
^----- There is still a 0 in front of the .
这就是为什么 -1 // 4
给出 -1 而 int(-1/4)
给出 0.
Is there any motivation for the differences between the functions?
嗯,它们有不同的用途://
是带余数的整数计算的一部分,int()
是实数运算 .
前面的部分。
您决定要计算什么,然后您决定在 Python 中使用哪个运算符以获得正确的结果。
好问题。继续学习。
我想说的是,您观察到这两个操作在直觉上应该相似,这是意料之中的,因为它们在正数上的行为相同。但是,如果您查看它们的起源(一个来自数学,另一个来自计算机科学),那么它们的不同行为就更有意义了。
你可以看看背后的概念:
- 地板除法也就是应用于数学除法的地板函数
- 类型conversion/Type铸造
============================================= =====================
I)floor division aka floor function applied to the math division
下限函数是数学中一个非常成熟的概念。
The floor function |_ x_ |, also called the greatest integer function or integer value (Spanier and Oldham 1987), gives the largest integer less than or equal to x. The name and symbol for the floor function were coined by K. E. Iverson (Graham et al. 1994)
所以floor除法只不过是应用到数学除法的floor函数。行为非常清楚,"mathematically precise"。
II)类型conversion/Type铸造
来自wikipedia:
In computer science, type conversion, type casting, type
coercion and type juggling are different ways of changing an
expression from one data type to another.
在大多数编程语言中,浮点数到整数的转换形式是通过舍入规则应用的(因此有一个约定):
- 向 0 舍入 – 向零定向舍入(也称为
截断)
根据 IEEE 754.
的舍入规则
所以,换句话说,python中整数除法和浮点数到整数转换之间的差异是数学原因,这里是 Guido van Rossum 的一些想法(我想我没有介绍一下他:D)(来自博客Python的历史,文章"Why Python's Integer Division Floors")
This disturbs some people, but there is a good mathematical reason.
The integer division operation (//) and its sibling, the modulo
operation (%), go together and satisfy a nice mathematical
relationship (all variables are integers):
a/b = q with remainder r
such that
b*q + r = a and 0 <= r < b
(assuming a and b are >= 0).
根据@thomas-weller的回答,我也发现自己可能是一个新手,因为他的除法示例有余数:
-
7 / 4 = 1 R 3 = 1 + 0,75 = 1.75
-
-> R3 = 3/4 = 0.75
-
-1 /4 = -1 R 3 = -1 + 0,75 (R3) = -0.25
我最近注意到 int()
rounds a float towards 0, while integer division 将浮子朝向其底部旋转。
例如:
-7 // 2 == -4
int(-7/2) == -3
我已阅读指定的文档:
class int(x, base=10)
Return an integer object constructed from a number or string x, or return 0 if no arguments are >given. If x is a number, return
x.__int__()
. For floating point numbers, this truncates towards zero.
和:
floor division
Mathematical division that rounds down to nearest integer. The floor division operator is
//
. For example, the expression11 // 4
evaluates to 2 in contrast to the2.75
returned by float true division. Note that(-11) // 4
is-3
because that is-2.75
rounded downward. See PEP 238.
但我觉得 2 个类似的操作(浮点除法为整数)应该 return 不同的结果似乎不合逻辑。
函数之间的差异有什么动机吗?
一致性。
您需要遵循一些非常基本且看似无关紧要的解释才能理解它。
你在学校学过除法和余数。你已经做了这样的计算:
8 ÷ 4 = 2 R 0
7 ÷ 4 = 1 R 3
6 ÷ 4 = 1 R 2
5 ÷ 4 = 1 R 1
4 ÷ 4 = 1 R 0
3 ÷ 4 = 0 R 3
2 ÷ 4 = 0 R 2
1 ÷ 4 = 0 R 1
0 ÷ 4 = 0 R 0
^------ This is the result of x // 4
^-- This is the result of x % 4 (modulo)
后来,你学会了实数除法:
8 ÷ 4 = 2.0
7 ÷ 4 = 1.75
6 ÷ 4 = 1.5
5 ÷ 4 = 1.25
4 ÷ 4 = 1.0
3 ÷ 4 = 0.75
2 ÷ 4 = 0.5
1 ÷ 4 = 0.25
0 ÷ 4 = 0.0
^--- Note that the number in front of the . is int(x/4)
到目前为止,您可能认为 x // 4
和 int(x/4)
总是给出相同的结果。这是你目前对情况的理解。
但是,看看整数除法会发生什么:R后面的数字从3,2,1循环到0然后重新开始:3,2,1,0。R前面的数字递减每 4 步。
那么,接下来会怎样?
8 ÷ 4 = 2 R 0
7 ÷ 4 = 1 R 3
6 ÷ 4 = 1 R 2
5 ÷ 4 = 1 R 1
4 ÷ 4 = 1 R 0
3 ÷ 4 = 0 R 3
2 ÷ 4 = 0 R 2
1 ÷ 4 = 0 R 1
0 ÷ 4 = 0 R 0
-1 ÷ 4 = -1 R 3
^------ We have to decrease now, because we already have 0 four times
^-- We have to restart the cycle at 3
同时实数除法得到:
-1 ÷ 4 = -0.25
^----- There is still a 0 in front of the .
这就是为什么 -1 // 4
给出 -1 而 int(-1/4)
给出 0.
Is there any motivation for the differences between the functions?
嗯,它们有不同的用途://
是带余数的整数计算的一部分,int()
是实数运算 .
前面的部分。
您决定要计算什么,然后您决定在 Python 中使用哪个运算符以获得正确的结果。
好问题。继续学习。
我想说的是,您观察到这两个操作在直觉上应该相似,这是意料之中的,因为它们在正数上的行为相同。但是,如果您查看它们的起源(一个来自数学,另一个来自计算机科学),那么它们的不同行为就更有意义了。
你可以看看背后的概念:
- 地板除法也就是应用于数学除法的地板函数
- 类型conversion/Type铸造
============================================= =====================
I)floor division aka floor function applied to the math division
下限函数是数学中一个非常成熟的概念。
The floor function |_ x_ |, also called the greatest integer function or integer value (Spanier and Oldham 1987), gives the largest integer less than or equal to x. The name and symbol for the floor function were coined by K. E. Iverson (Graham et al. 1994)
所以floor除法只不过是应用到数学除法的floor函数。行为非常清楚,"mathematically precise"。
II)类型conversion/Type铸造
来自wikipedia:
In computer science, type conversion, type casting, type coercion and type juggling are different ways of changing an expression from one data type to another.
在大多数编程语言中,浮点数到整数的转换形式是通过舍入规则应用的(因此有一个约定):
- 向 0 舍入 – 向零定向舍入(也称为 截断)
根据 IEEE 754.
的舍入规则所以,换句话说,python中整数除法和浮点数到整数转换之间的差异是数学原因,这里是 Guido van Rossum 的一些想法(我想我没有介绍一下他:D)(来自博客Python的历史,文章"Why Python's Integer Division Floors")
This disturbs some people, but there is a good mathematical reason. The integer division operation (//) and its sibling, the modulo operation (%), go together and satisfy a nice mathematical relationship (all variables are integers):
a/b = q with remainder r
such that
b*q + r = a and 0 <= r < b
(assuming a and b are >= 0).
根据@thomas-weller的回答,我也发现自己可能是一个新手,因为他的除法示例有余数:
-
7 / 4 = 1 R 3 = 1 + 0,75 = 1.75
-
-> R3 = 3/4 = 0.75
-
-1 /4 = -1 R 3 = -1 + 0,75 (R3) = -0.25