【发布时间】:2014-03-26 08:59:53
【问题描述】:
在 C 中以下代码 ...
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<stdlib.h>
int main() {
int result = (-12) % (10);
printf("-12 mod 10 = %d\n",result);
return 0;
}
给出这个输出
> gcc modTest.c
> ./a.out
-12 mod 10 = -2
但根据this mod calculator -12 mod 10 = 8
在 Python 中 ...
> python
Python 3.3.0 (default, Mar 26 2013, 09:56:30)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> (-12) % (10)
8
在 C 中发生了什么会产生 -2 而不是 8?
【问题讨论】:
-
嗯,它们是相同的模 10,不是吗。
-
与此重复的问题没有可接受的答案!
标签: c modulus negative-number