标签: modulo

股息为零时 % 5 的意外行为

我有以下代码

#include <iostream>
#include<exception>
#include <cstdlib>
int main(){
  for (int i = 0; i < 100; i++) {
    std::cout << i << " ";
      if (i % 5 == 0) {
        abort();
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

但它只写 0 并说为什么调用了 abort?我认为它应该输出 0 1 2 3 4 然后退出程序是吗?

c++ operators modulo

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

模运算是否在不同的基础上起作用?

我对模数(mod)运算符在不同的基础上的属性感到困惑.说我正在计算230 10%11 10 = 10 10.如果我换到16号基地,那么E6 16%B 16 = A 16?数字系统的基础是否适用于mod操作?

(注意:我正在使用这个用于我正在编写的JavaScript中的小计算器应用程序,但我不明白为什么语言应该重要.)

javascript math modulo

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

循环整数范围时,将表示月数的整数转换为年数和月数

我正在使用 for 循环来显示数字 1-60:

for ($i = 0; $i <= 60; $i++)
Run Code Online (Sandbox Code Playgroud)

在循环中,我希望能够显示年数和月数。例如:

1 month
2 months
3 month
...
1 year
1 year 1 month
1 year 2 month
Run Code Online (Sandbox Code Playgroud)

等等...

我试过这个:

if (!is_float($i / 12)) {
    $years = $i/12;
} else {
    $years = 'no';
}
Run Code Online (Sandbox Code Playgroud)

这显示了 12 个月的 1、24 个月的 2,但不显示中间的时间。

php time data-conversion division modulo

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

二进制 % 的无效操作数(有浮点型和浮点型)

下面是我的代码:

#include <stdio.h>

int main ( )   
{
    float a = 5,b = 2;    
    int c,d;    
    c = a % b;
    d = a/2;    
    printf("%d\n",d);    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译这个时,我得到

“二进制 % 的操作数无效”

在6号线上。我该如何解决这个问题?

c modulo

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

模数行为 -1 % 3 (C++)

if (currIndex < 0) {

            cout << currIndex << " % " << array.size() << endl;

            currIndex = currIndex % array.size();

            cout << currIndex << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:

-1 % 3
0
Run Code Online (Sandbox Code Playgroud)

-1 % 3 = -1 在 C++ 中,为什么返回 0?

完整片段:https : //ideone.com/leWqhi

c++ modulo

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

Why does the modulo operator return true?

This problem basically says to iterate over an integer, and evaluate if the number is divisible by its left-side number and if it is divisible return a Boolean array.

73312

  • 第一个数字没有左边的数字,所以它是假的。
  • 下一个是 3/7,计算结果为 false,依此类推。

我运行了测试,一切都很好,但是使用这个数字 (73312),它在应该返回 false 时返回 true。

预期输出 [false,false,true,false,true]

实际产量 [false,false,true,true,true]

function divisibleByLeft(n) {

  let flag = false;
  const ansArr = [];
  const s = JSON.stringify(n);
  const arr = [];

  for (let i = 0; i < s.length; i++) {
    arr.push(parseInt(s[i]));
  };

  for (let i = 0; i …
Run Code Online (Sandbox Code Playgroud)

javascript modulo

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

简单的 for 循环突然使 C 中的程序崩溃

我通过使用 MSVC

cl file.c
Run Code Online (Sandbox Code Playgroud)

用这个非常简单的代码:

#include <stdio.h>
#include <stdlib.h>
    
int
main(void) {
    
    int num = 50, pointNum = 60, max = 0;
    
    puts("Hello");

    for (int i = 0; i <= num; i++) {
        printf("%d\n", pointNum % i);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我从命令行运行它时,它只是暂停然后崩溃,只打印“Hello”。我不知道出了什么问题,因为它似乎没有错误。

c loops modulo

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

Java 中的无符号模

我正在移植一些在 uint32_t 上进行取模的 C 代码。uint32_t 按位适合 Java int,但我无法弄清楚如何在不转换为 long 的情况下对其执行模运算。这是我现在的代码:

int i = 0xffffffff;
long asUnsigned = Integer.toUnsignedLong(i);
int mod = (int) (asUnsigned % 42L);
Run Code Online (Sandbox Code Playgroud)

我可以在不转换为长整型的情况下执行此模计算吗?

java bit-manipulation modulo unsigned-integer signed-integer

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

用除后的值填充列表

我正在寻找一种方法,将代表经过的秒数的数字(例如 11000)除以 3600,使其成为一小时并创建一个如下列表:

['3600','3600','3600','200']

我知道这个任务对于基本的数学运算来说非常简单,但我很好奇是否有更多的“Pythonic”方法来实现这一点。

python list modulo

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

阶乘的模除以阶乘

如何计算a!/(b1! b2! ... bm!) modulo pp素数在哪里?a和的阶乘b可能非常大(long long int不够),所以我需要传递到模。

c++ algorithm factorial modulo number-theory

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