gpt4 book ai didi

python - "a//b"和 "int(a/b)"之间的区别

转载 作者:行者123 更新时间:2023-12-01 07:25:52 25 4
gpt4 key购买 nike

我知道a/b是浮点除法并且 a//b是Python中的楼层划分。
可以看出int(a/b)如果分子和分母均为正数,则结果也与向下除法相同。但尝试-a//bint(-a/b)产生不同的结果。内部运作有哪些?

>>> int(-5/3)
-1
>>> -5//3
-2

有何不同int(a/b)来自等效楼层划分,即 a//b

最佳答案

来自 int docs :

For floating point numbers, this truncates towards zero.

来自 // docs :

Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result.

关于python - "a//b"和 "int(a/b)"之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57455992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com