相关疑难解决方法(0)

C#与Excel的模数有何不同?

我正在试验负基数系统,我用Excel来玩并检查我的计算.

我注意到C#与Excel存在差异.为什么C#返回的结果与Excel不同?

例如:

C#:146%-3 = 2

Excel:mod(146,-3)= -1

c# excel

5
推荐指数
2
解决办法
706
查看次数

偶数的平均值给出了错误的值

我正在努力接近理解溪流,因此我正在做一些基本的练习.在这一个中,我想计算奇数的平均值.我写这个算法是为了这样做,但是它给出了不正确的结果(8.0).我试图调试它,但我找不到它实际上做了什么.

List<Integer> numbers = Arrays.asList(1, 3, -2, -4, -7, -3, -8, 12, 19, 6, 9, 10, 14);

OptionalDouble result = numbers.stream()
                               .filter(i -> i % 2 == 1)
                               .mapToDouble(i -> i).average();
if (result.isPresent()) {
   System.out.println(result);
} else {
   System.out.println("Error");
}
Run Code Online (Sandbox Code Playgroud)

我的代码现在在做什么?我应该如何解决它做它应该做的事情?

java lambda java-stream

4
推荐指数
1
解决办法
106
查看次数

模运算的结果是否定的

为什么以下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
查看次数

C中的无符号整数下溢

我在网站上看到了多个问题,这些问题解决了无符号整数上溢/下溢的问题。关于下溢的大多数问题都询问如何将负数分配给无符号整数。我不清楚的是,当unsigned int从另一个减去时会发生什么,unsigned int例如a - b结果为负。该标准的相关部分是:

涉及无符号操作数的计算永远不会溢出,因为无法用所得的无符号整数类型表示的结果的模数要比该所得类型可以表示的最大值大一模。

在这种情况下,您如何解释“减少”?这是否意味着UINT_MAX+1添加到负的结果,直到它>= 0

我看到这个问题解决了要点(基本上说标准选择谈论溢出,但是关于模数的要点也适用于下溢),但是我仍然不清楚:

说的结果a-b-1; 根据标准,操作-1%(UINT_MAX+1)将返回-1(如解释在这里); 所以我们回到了起点。

这可能过于花哨,但这与C的计算模相反,此模是否意味着数学模?

c standards integer underflow

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

对负数进行模运算

模运算a%b返回余数,a/b但对于负数,它不会这样做.

#include <stdio.h>

int main(void) {
  int n=-4;
  printf("%d\n",n%3);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它应返回2为3*( - 2)= - 6小于-4且倍数为3但输出为-1.为什么它(-a) mod b一样对待-(a mod b)

c modulo negative-number

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

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
查看次数

测试一个数字是否均匀分配到6?

我试图看看包含负数的两个输入数字(整数)是否均匀地进入6(余数为0).这是我正在尝试的代码.

if((in1)%6 == 0 && (in2)%6 == 0){
    printf("Divisible: both\n");
}
else if((in1)%6 == 0 && (in2)%6 > 0){
    printf("Divisible: only %i\n",in1);
}
else if((in1)%6 > 0 && (in2)%6 == 0){
    printf("Divisible: only %i\n",in2);
}
else{
    printf("Divisible: neither\n");}
Run Code Online (Sandbox Code Playgroud)

这适用于所有正整数,但对于任何负数,打印的代码总是"可分割:不"任何帮助我如何显示正整数和负数可被6整除,余数为0将是非常有帮助的

c modulo

-1
推荐指数
1
解决办法
57
查看次数

标签 统计

c ×5

modulo ×3

java ×2

math ×2

negative-number ×2

c# ×1

excel ×1

integer ×1

java-stream ×1

lambda ×1

mod ×1

standards ×1

underflow ×1