我想说,你的观察,这两个操作应该是直觉上相似的,因为在正数上它们的行为是相同的。但是如果你看看它们的起源(一个来自数学,另一个来自计算机科学),那么它们的不同行为就更有意义了。在
你可以从以下几个概念中寻找:Floor division又名Floor division应用于数学除法的Floor函数
类型转换/类型铸造
========================================================================
I)Floor division又名应用于数学除法的Floor函数
楼层函数是数学中一个非常成熟的概念。在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)
所以楼层划分只不过是应用于数学除法的楼层函数。行为非常清楚,“数学上精确”。在
II)类型转换/类型转换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).