c - c如何计算余数?

标签 c discrete-mathematics modular-arithmetic

我的一个函数有奇怪的行为。 程序代码非常庞大,难以阅读。所以我附上了部分功能。 如果j小于 4,它返回的不是实际余数,而是 j 和 4 之间的差。

我测试发现,3 % 4 == 1而不是 3。

while (j < 16)
{
    if ((crd + (j % 4)) + d * (j / 4) > d * d && str2[j] == 1)
        str[0] = 2;
    if (((crd + (j % 4) + d * (j / 4)) / d) - i != (j / 4) && str2[j] == 1)
        str[0] = 2;
    if (str2[j] == 1)
        str[(crd + (j % 4)) + d * (j / 4)] = str2[16];
    if (str[(crd + (j % 4)) + d * (j / 4)] == 2 || str[0] == 2)
        return (str);
    j++;
}

问题是如何%运算符(operator)工作。 screenshot of debugger

最佳答案

以下建议代码:

  1. 干净地编译
  2. 显示模运算符按预期工作

现在,建议的代码:

#include <stdio.h>

int main( void )
{
    int a = 3 % 4;
    printf( "%d\n", a );
}

编译/链接并运行时的结果是:

3

上面显示模运算符按预期工作。

剩下你的代码,它不是 [mcve],所以结果的可能性是你的代码有问题。但我们确实无法确定,因为我们没有您的代码,

关于c - c如何计算余数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54462922/

相关文章:

image-processing - Sobel 过滤器是否意味着要归一化?

algorithm - n 算法的基 b 扩展

c - 如何使用使用 128 位二进制和 4 位多项式模的 C 编写模块化运算?

algorithm - 如何快速计算这个系列的模 m 的总和?

algorithm - 数字等于其数字的幂的总和

performance - 具有数百万位模数和指数的模幂运算的极快方法

c - 不正确使用时的退出状态

c - 将文本文件中的值扫描到结构中

c++ - uint32 和 uint32_t 的区别

java - 是否可以将java结果发送到c程序