使用负运算数的模运算符

问题描述

当我在负运算符上使用模运算符给出不同的输出时,我正在学习 我正在打印print(-28%10)给出输出2,而print(28%-10)给出输出为-2,因为print(28%10)给出8的结果是实际余数。有人可以帮我了解一下吗?

解决方法

您可以将A%B运算符视为以下工具: “只要我们找到一个介于0和B之间的数字,就从A中减去或减去B” 即

-28%10 = 2
since:
-28 +10 +10 +10 = 2 # between 0 and 10

28%-10 = -2
since:
28 -10 -10 -10 = -2 #between 0 and -10
28 -10 -10 = 8 #not between 0 and -10

28%10 = 8
since:
28 -10 -10 = 8# between 0 and 10

,

%运算符实际上不是python的模运算符,而是余数运算符。文档对此并不十分清楚,但是它没有遵循负值的常规模逻辑,而是简单地获取余数。

,

Guido van Rossum explains认为Class<Activity?>会以这种方式工作,因为这对某些应用程序很有用,例如,如果您正在使用第二个时间戳来表示历元开始之前的时间点(1970年1月1日),您可以通过执行Class<String>来轻松计算天数。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...