相关疑难解决方法(0)

如何在C/C++/Obj-C中编写处理负数的模数(%)运算符

我厌恶的C语言(作为一名数学家)就是这样的

(-1) % 8 // comes out as -1, and not 7

fmodf(-1,8) // fails similarly
Run Code Online (Sandbox Code Playgroud)

什么是最好的解决方案?

C++允许模板和运算符重载的可能性,但这些对我来说都是模糊的.感激地收到了例子.

c c++ operator-overloading modulo c++11

81
推荐指数
5
解决办法
23万
查看次数

C中的Python式整数除法和模数

在Python和Ruby中,带符号的整数除法向负无穷大截断,有符号整数模数与第二个操作数具有相同的符号:

>>> (-41) / 3
-14
>>> (-41) % 3
1
Run Code Online (Sandbox Code Playgroud)

但是,在C和Java中,带符号的整数除法截断为0,有符号整数模数与第一个操作数的符号相同:

printf("%d\n", (-41) / 3); /* prints "-13" */
printf("%d\n", (-41) % 3); /* prints "-2" */
Run Code Online (Sandbox Code Playgroud)

在C和Python中执行相同类型的除法和模数的最简单,最有效的方法是什么?

c c++ java division modulo

24
推荐指数
2
解决办法
7912
查看次数

C++中带负数的模数

我一直在编写以下重现关系的程序:

An = 5An-1 - 2An-2  - An-3 + An-4
Run Code Online (Sandbox Code Playgroud)

输出应该是答案模数10 ^ 9 + 7 ..我为此写了一个蛮力方法如下......

long long int t1=5, t2=9, t3=11, t4=13, sum;
while(i--)
{
    sum=((5*t4) - 2*t3 - t2 +t1)%MOD;
    t1=t2;
    t2=t3;
    t3=t4;
    t4=sum;
}
printf("%lld\n", sum);
Run Code Online (Sandbox Code Playgroud)

其中MOD= 10^9 +7 每件事似乎都是真的..但我得到了一些价值的否定答案..由于这个问题,我无法找到正确的解决方案...... Plz帮助保持正确的地方Modulus

c++ recurrence modulo

21
推荐指数
3
解决办法
3万
查看次数

使用带有浮点数的模数运算符

有人在编程竞赛中提出这个问题: -

-1%1000000009是-1或1000000008

我想知道,这甚至可能吗?我试过我的系统,每次都得-1.另外,我必须找出10 ^ -9%10 ^ 9,我用fmod并得到答案1e-009,不应该是1吗?

我的解释: - 10 ^ -9/10 ^ 9 = 1/10 ^ 18所以,答案= 1.

请告诉我我错在哪里.

c++ floating-point

5
推荐指数
1
解决办法
585
查看次数

模运算的结果是否定的

为什么以下C代码产生负数作为输出?我该如何防止这种情况发生?

    #include <stdio.h>

    int main()
    {
            int i;
            char buf[1024];
            for (i = 0; i < 1024; i++)
                    buf[i] = i%256;

            for (i=0; i<1024; i++) {
                    printf("%d ", buf[i]);
                    if (i%32==31)
                            printf("\n");
            }
    }
Run Code Online (Sandbox Code Playgroud)

c math modulo negative-number

3
推荐指数
1
解决办法
142
查看次数

使用底片时,Modulo返回错误的结果

我想计算:( - 15%3)应该是0但是我得到1:

当我明确地做:

int IntFcn (const void *key, size_t tableSize)
{
    printf("%d\n",(*(int*)key)); // prints -15
    printf("%d\n",tableSize); // prints 3
    printf("%d\n",(-15) % 3); // prints 0
}
Run Code Online (Sandbox Code Playgroud)

我得到了正确的结果(0)但是当我尝试使用下面的变量时我得到1:

int IntFcn (const void *key, size_t tableSize)
{
    printf("%d\n",(*(int*)key)); // prints -15
    printf("%d\n",tableSize); // prints 3
    printf("%d\n",((*(int*)key) % tableSize)); // prints 1
    return ((*(int*)key) % tableSize);
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

c modulo

2
推荐指数
1
解决办法
155
查看次数

Mod with negative numbers gives a negative result in Java and C

Let's say I have (-5) mod 8.

I tried it in both languages Java and C, and they gave me a -5 result when I was expecting 3.

Why is this happening? Can a modulus be negative? And what should I change to get the correct result?

Java code

public class Example {
    public static void main(String[] args) {
        int x;
        x = -5%8;
        System.out.println(x);
    }
}
Run Code Online (Sandbox Code Playgroud)

C code

int main(){
    int x;
    x = -5%8;
    printf("%d", x); …
Run Code Online (Sandbox Code Playgroud)

c java math mod

0
推荐指数
1
解决办法
4507
查看次数