python浮点除法和整数除法_在python中整数除法和浮点到int转换之间的区别是什么原因?...

我想说,你的观察,这两个操作应该是直觉上相似的,因为在正数上它们的行为是相同的。但是如果你看看它们的起源(一个来自数学,另一个来自计算机科学),那么它们的不同行为就更有意义了。在

你可以从以下几个概念中寻找: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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值