python 负数模运算_在Python中使用负数进行模运算

python 负数模运算

First and foremost, sorry for the long absence recently. Work and study have been taking most of me in the last months. In fact, what I’m writing down Today is something I learnt recently and thought it was worth sharing given that it quite surprised me and it’s a super useful fact to learn; how does Python handle modulo operation with negative numbers. So, let’s keep it short and sweet and get straight to it.

首先,对于长期以来的缺席深表歉意。 在过去的几个月中,工作和学习一直困扰着我。 实际上,今天我写下的是我最近学到的东西,并认为值得分享,因为这让我很惊讶,这是一个非常有用的知识。 Python如何处理带负数的模运算 。 因此,让我们使其简短而有趣,直接使用它。

The base logic behind the modulo operator indicates the following:

模运算符后面的基本逻辑指示以下内容:

For any two numbers, the modulo operator (%) returns the remainder of dividing the left-hand operand by right-hand operand. It’s used to get the remainder of a division problem. Beau Carnes at www.freecodecamp.org

对于任何两个数字,取模运算符(%)返回左操作数除以右操作数的余数。 它用于获得除法问题的其余部分。 博·卡恩斯(Beau Carnes), 网址:www.freecodecamp.org

The basic syntax is:

基本语法为:

  • a % b = r

    a%b = r

In the previous example a is divided by b, and the r, i.e. the remainder is returned. Let’s see an example with numbers now:

在前面的示例中,a除以b,然后返回r,即余数。 现在来看一个带有数字的示例:

  • 7 % 2 = 1

    7%2 = 1

The result of the previous example is one. Two goes into seven three times and there is one leftover. In a similar way if we were to choose two numbers where b > a we would get the following:

上一个示例的结果为1。 有2次进入7次3次,剩下1次。 以类似的方式,如果我们选择b> a的两个数字,则会得到以下结果:

  • 3 % 4 = 3

    3%4 = 3

This will result in three since four does not go into three any times so the original three remains.

这将导致三个,因为四个不会在任何时候进入三个,因此原来的三个仍然存在。

The sibling of the modulo operator is known as the integer division operation (//), where the fractional part, i.e. the remainder is discarded. Take the following example:

模运算符的同级称为整数除法运算(//),其中小数部分(即余数)被丢弃。 请看以下示例:

  • 5 // 2 = 2

    5 // 2 = 2

For positive numbers, there’s no surprise. However if one of the operands is negative, the result will be floored as well, i.e. rounded away from zero towards negative infinity, returning the largest integer less than or equal to x. For example:

对于正数,不足为奇。 但是,如果其中一个操作数为负,则结果也将被累加,即从零向负无穷大舍入,返回小于或等于x的最大整数。 例如:

  • -5 // 2 = -3

    -5 // 2 = -3

Now, there are several ways of performing this operation. Unlike C or C++, Python’s modulo operator always returns a number having the same sign as the denominator (divisor) and therefore the equation running on the back will be the following:

现在,有几种执行此操作的方法。 与C或C ++不同,Python的模运算符始终返回与分母(除数)具有相同符号的数字,因此在背面运行的方程式如下:

  • mod = a — math.floor(a/b) * base

    mod = a — math.floor(a / b)*基数

For example, working with one of our previous examples we’d get:

例如,使用我们之前的示例之一,我们将获得:

  • mod = 7 — math.floor(7/2) * 2

    mod = 7 — math.floor(7/2)* 2

  • mod = 7–3*2 = 1

    mod = 7–3 * 2 = 1

And the overall logic works according to the following premises:

总体逻辑根据以下前提进行工作:

  • a/b = q with remainder r

    a / b = q,其余为r

  • Such that: b*q + r = a and 0 <= r < b

    这样:b * q + r = a且0 <= r <b

Now, if we want this relationship to extend for negative numbers, there are a couple of ways of handling this corner case, but according to Wikipedia in mathematical number theory mathematicians prefer to stick to flooring towards negative infinity as in the following example:

现在,如果我们希望将此关系扩展为负数,则有几种方法可以处理这种极端情况,但是根据Wikipedia的数学数论,数学家宁愿坚持朝负无穷大展开,如以下示例所示:

  • -500/27 = -18.51

    -500/27 = -18.51

  • math.floor(-500/27) = -19

    math.floor(-500/27)= -19

Python follows the same logic. Why? According to Guido van Rossum, the creator of Python, this criterion has some interesting applications. For example, consider taking a POSIX timestamp (seconds since the start of 1970) and turning it into the time of day. Since there are 24*3600 = 86400 seconds in a day, this calculation is simply t % 86400. But if we were to express times before 1970 using negative numbers, other criterions used would give a meaningless result, while using the floor rule works out fine.

Python遵循相同的逻辑。 为什么? 根据 Python的创建者Guido van Rossum的说法 ,该标准具有一些有趣的应用程序。 例如,考虑采用POSIX时间戳(自1970年初以来的秒数),并将其转换为一天中的时间。 由于一天中有24 * 3600 = 86400秒,因此此计算仅为t%86400。但是,如果我们要使用负数表示1970年之前的时间,则使用底限法则可以得出其他准则将无意义。精细。

Finally, take into account the following when working with negative numbers:

最后,在使用负数时请考虑以下因素:

  • If a < 0 then 0 <= r < b remains the same

    如果a <0,则0 <= r <b保持不变
  • But if b < 0 then 0 >= r > b

    但是如果b <0则0> = r> b

Finally, let’s see a quick example for a = -500 and b = 12:

最后,让我们看一下a = -500和b = 12的简单示例:

  • -500/12 = -41.67

    -500/12 = -41.67

  • -500 % 12 = 4

    -500%12 = 4

  • -500 // 12 = -42

    -500 // 12 = -42

Then we can check for b*q + r = a:

然后我们可以检查b * q + r = a:

  • 12 * -42 + 4 = -500

    12 * -42 + 4 = -500

For closing up this story, let’s see an interesting application of using the modulo operator with negative numbers: converting an hour written in the 24-hour clock into the 12-hour clock. The challenge seems easy, right? For example, 23%2 will give us 11 and 15%12 will give us 3…exactly what we want! Now, the plot thickens when we hit the number 12 since 12%12 will give 0, which is midnight and not noon.

为了结束这个故事,让我们看一下使用带负数的模运算符的有趣应用:将24小时制中写入的小时转换为12小时制。 挑战似乎很容易,对吧? 例如,23%2将给我们11,15%12将给我们3…正是我们想要的! 现在,当我们将数字设为12时,情节就会变厚,因为12%12会给出0,即午夜而非正午。

The solution here is using the modulo operator with negative numbers. For example, -22%12 will give us 2 and -19/12 will give us 5. As a rule of thumb, this kind of operation will always give us how many units we have left until the next multiple of our denominator. I.e. in our first example we’re missing 2 hours until 12x2 and in a similar way -34%12 would give us 2 as well since we would have left 2 hours until 12x3.

解决方案是使用带负数的模运算符。 例如,-22%12将给我们2,而-19/12将给我们5。根据经验,这种操作将始终给我们剩下多少单位,直到分母的下一个倍数为止。 也就是说,在我们的第一个示例中,我们错过了12x2之前的2小时,并且以类似的方式,-34%12也将为我们提供2,因为我们将剩下2个小时直到12x3。

So, coming back to our original challenge, for converting an hour written in the 24-hour clock into the 12-hour clock, we could write the following:

因此,回到我们最初的挑战,要将24小时制中写入的小时转换为12小时制,我们可以编写以下内容:

def convert24to12(hour):
return 12 — ((- hour) % 12)

That’s all for today! If you enjoyed this story, don’t forget to visit my profile where you’ll find content such as:

今天就这些! 如果您喜欢这个故事,请不要忘记访问我的个人资料,在其中找到诸如以下内容:

See you around! And thanks for reading!

再见! 并感谢您的阅读!

翻译自: https://medium.com/@gonzaloferreirovolpi/modulo-operation-with-negative-numbers-in-python-18e3cedffe82

python 负数模运算

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值