采用负数模数有意义吗? [英] When taking modulo of negative numbers makes sense?

查看:147
本文介绍了采用负数模数有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,因为我们在2年前的代码中发现了一个错误。我们用0x8000初始化了一个16位有符号整数(该平台当然使用2的补码表示为负数)。在一个难以重现的特殊情况下,对该变量执行模数10以提取十进制表示中的各个数字,同时该值仍为0x8000(-32768)。 -32768%10 == 248这对我们的应用没有意义。

I'm curious because we found a bug in our code written 2 years ago. We initialized a 16 bit signed integer with 0x8000 (the platform is of course uses 2's complement representation for negative numbers). In a hardly reproducible special case, modulo by 10 was being performed on this variable to extract individual digits in decimal representation while the value is still 0x8000 (-32768). -32768 % 10 == 248 which makes no sense for our application.

我们的平台是OKI 411微控制器。

Our platform is OKI 411 micro-controller.

我很好奇,虽然在这种情况下取​​负数的模数是没有意义的,是否有任何现实生活的例子或实际理由或以某种方式通过取负数的模数获得利益?

I'm curious, though taking modulo of negative number in this case makes no sense, is there any real life example or practical reason or to get benefit somehow by taking modulo of negative number?

推荐答案

我需要提供更完整的答案。

I needed to give a more complete answer.

如你所说,数学上有意义(虽然,你不需要成为一名数学家来理解这一点,但让我澄清一些事情。当我说你对残留物感兴趣时,这可能是错误的认为残留物为正( a mod n )或为负( -a mod n )数字是相同的,因为人们会天真地丢弃符号并在负面情况下进行除法。当然,你现在知道这是不正确的。您可以这样想:当计算 a mod n 时,首先要找出哪个数字 n * x (其中x是整数)最接近 a 而不超过 a 。之后,计算 n * x a 之间的数量。一个例子可能在这里有所帮助:

As you say, mathematically makes sense (although, you don't need to be a mathematician to understand that), but let me clarify something. When I said you're interested in the residue, which it's true, one may be mistaken to think that residues for positive (a mod n) or negative (-a mod n) numbers are the same because one would naively drop the sign and carry on the division in the negative case. Of course, you now know this is not correct. You can think of it in this way: When calculating a mod n, first you find out which number n*x (where x is an integer) is closest to a without going over a. After that, you count how many number are there between n*x and a. An example is likely to help here:

假设您想要 -282 mod 10 ,然后10 * -29 = -290最接近-282而不越过它。然后你只计算在 n * x a 之间的数量(即290到282之间) )。有8个号码,这是你的答案,这是正确 。一方面,对于正数(282 mod 10),最接近282的数字将是10 * 28 = 280(记住,我们不想超过282)。因此,中间有两个数字(也可以正确)。

Let's suppose you want -282 mod 10, then 10*-29 = -290 is closest to -282 without going over it. And then you simply count how many numbers are in between n*x and a (that is, between 290 and 282). There are 8 numbers, and that's your answer, which is correct. On the hand, for positive numbers (282 mod 10), the closest number to 282 will be 10*28 = 280 (remember, we don't want to go over 282). Therefore, there are 2 numbers in between (also correct).

至于应用程序,我不确定一个应用程序特别依赖于计算负数的模数,但是模块化算术提供了很多应用程序。请参阅上面的Wikipedia链接,了解它们。如果不是因为具有负数的模运算的数学一致性,可能我们最终会得到较少的一般定理,这反过来意味着不那么强大的应用。

As for the applications, I'm not sure about one application relying particularly in calculating modulo of negative numbers, but the whole field of Modular arithmetic provides a lot of applications. See the Wikipedia link above to read a bit about them. If it weren't for the mathematical consistency of having modulo operations of negative numbers, probably we would end up with less general theorems, which in turn means, less powerful applications.

关于您的编程问题:

当a或n为负数时,这个天真的定义会破坏,
编程语言在定义这些值方面会有所不同。虽然
通常使用a和n都是整数执行,但许多计算
系统允许其他类型的数字操作数。

When either a or n is negative, this naive definition breaks down and programming languages differ in how these values are defined. Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands.

另请参见

这篇关于采用负数模数有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆