C:负数和余数背后的数学 [英] C: The Math Behind Negatives and Remainder

查看:84
本文介绍了C:负数和余数背后的数学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是在处理Remainder/Mod时被要求的第一件事,而我有点撞墙了.我正在教自己用一本教科书和一小段C代码编写程序.

This seems to be the #1 thing that is asked when dealing with Remainder/Mod, and I'm kind of hitting a wall with it. I'm teaching myself to program with a textbook and a chuck of C code.

看到我真的没有教员说:不,不.它实际上是这样工作的",我以为我会在这里试一试.不过,我还没有找到关于数学部分的最终答案.

Seeing as I don't really have an instructor to say, "No, no. It actually works like this", I thought I'd try my hand here. I haven't found a conclusive answer to the mathematical part of this, though.

所以...我觉得这是一种非常罕见的情况,但是我仍然想知道在闪亮的编译下面会发生什么.另外,根据C89标准,这本教科书希望我提供使用负余数时所有可能的值.问一个人是否可以检查一下这个数学运算是否正确?

So... I'm under the impression that this is a pretty rare occurrence, but I'd still like to know what it is that happens underneath the shiny compiling. Plus, this textbook would like for me to supply all values that are possible when using negative remainders, per the C89 Standard. Would it be much to ask if someone could check to see if this math is sound?

1) 9%4
9 - (2) * 4 = 1     //this is a value based on x - (x/y) * y
(2) * 4 + (1) = 9     //this is a check based on (x/y) * y + (x%y) = x

2) -9%4
9 - (2) * 4 = 1; 9 - (3) * 4 = -3   //these are the possible values
(2) * 4 + (1) = 9; (3) * 4 + (-3) = 9    //these are the checks

3) 9%-4
Same values as #2?? 

我尝试在表达式中使用负数进行计算,并提出了诸如17和-33之类的可笑的东西.它们也是#3的1和-3吗?

I tried computing with negatives in the expressions, and came up with ridiculous things such as 17 and -33. Are they 1 and -3 for #3 as well??

4) -9%-4
Same as #1??

在代数除法中,负号取消".他们在这里做同样的事情,还是有其他事情发生?

In algebraic division, negative signs "cancel". Do they do the same here, or is there something else going on?

我认为最让我感到困惑的是底片.我在学校(5至6年前)学习代数的方式,它们与数字相关".在编程中,由于它们是一元运算符,不是吗?示例:在#2上填写x的值时,x = 9而不是x = -9.

I think the thing that gets me confused the most is the negatives. The way I learned algebra in school (5-6 years ago), they are "attached" to their numbers. In programming, since they are unary operators, is that not so? Example: When filling in the value for x on #2, x = 9 instead of x = -9.

衷心感谢您的帮助.

推荐答案

在这里,您需要对余数进行数学定义.

Here you need the mathematical definition on remainder.

给出两个整数 m,d ,我们说 r m 余数 >和 d 如果 r 满足两个条件:

Given two integer numbers m, d, we say that r is the remainder of the division of m and d if r satisfies two conditions:

  • 还有另一个整数 k ,例如m == k * d + r
  • 0 <= r < d.
  • There exists another integer k such that m == k * d + r , and
  • 0 <= r < d.

对于正数,在C中,只需遵循上面的定义,我们就有m % d == rm / d == k.

For positive numbers, in C, we have m % d == r and m / d == k, just by following the definition above.

从定义中可以得出3%2 == 1和3/2 ==1.
其他示例:

From the definition, it can be obtainded that 3 % 2 == 1 and 3 / 2 == 1.
Other examples:

4/3 == 1和5/3 == 1,尽管有5.0/3.0 == 1.6666(其中 会四舍五入到2.0).

4 / 3 == 1 and 5 / 3 == 1, in despite of 5.0/3.0 == 1.6666 (which would round to 2.0).

4%3 == 1和5%3 == 2.

4 % 3 == 1 and 5 % 3 == 2.

您也可以相信公式r = m - k * d,该公式在C中的编写方式为:

You can trust also in the formula r = m - k * d, which in C is written as:

m % d == m - (m / d) * d

但是,在标准C中,整数除法遵循以下规则:round to 0.
因此,对于负数操作数C,其结果与数学结果不同.
我们将有:

However, in the standard C, the integer division follows the rule: round to 0.
Thus, with negative operands C offer different results that the mathematical ones.
We would have:

(-4)/3 == -1,(-4)%3 == -1(在C中),但在普通数学中:(-4)/3 = -2,(-4)%3 = 2.

(-4) / 3 == -1, (-4) % 3 == -1 (in C), but in plain maths: (-4) / 3 = -2, (-4) % 3 = 2.

在普通数学中,余数始终为非负数,并且小于abs(d).
在标准C中,其余部分始终具有第一个操作数的符号.

In plain maths, the remainder is always nonnegative, and less than the abs(d).
In standard C, the remainder always has the sign of the first operand.


 +-----------------------+
 |  m  |  d  |  /  |  %  |
 +-----+-----+-----+-----+
 |  4  |  3  |  1  |  1  |
 +-----+-----+-----+-----+
 | -4  |  3  | -1  | -1  |
 +-----+-----+-----+-----+
 |  4  | -3  | -1  |  1  |
 +-----+-----+-----+-----+
 | -4  | -3  |  1  | -1  |
 +-----------------------+

备注:此说明(否定情况)仅适用于标准C99/C11.您必须谨慎使用编译器版本,并进行一些测试.

Remark: This description (in the negative case) is for standard C99/C11 only. You must be carefull with your compiler version, and do some tests.

这篇关于C:负数和余数背后的数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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