c++中long-long的模运算
modulus operations on long long in c++
我正在研究大数字的素数分解(主要是项目3@project Euler。我需要对声明为long-long的数字使用模。每次我试图对那个巨大的数字进行模运算时,我都会得到一个浮点异常。如有任何帮助,我们将不胜感激。谢谢。
我已经通过gdb运行了这个,看看发生了什么。下面是我的代码。这是一个非常粗略的逻辑请不要给我这个问题的答案。我很乐意接受帮助,让这件事变得更好,只是请不要直截了当地回答我。谢谢:)
long factor(long number) {
string br = "nr";
long x = 0;
/*this modulus variable is an attempt
to move the answer into a long long container
to see if that solves my floating point exception,
it didn't*/
long long modulus;
while(x <= number) {
modulus = number % x;
if(modulus == 0) {
cout << number/x << br;
return factor(number/x);
}//if number % x
else {
return x;
}//else
x++;
}//while
}//factor
不要试图通过0进行mod,它是未定义的!这样做将导致除以零的错误。
long x = 0;
modulus = number % x; // x is 0 here and thus not valid
根据维基百科关于模块操作的文章,我的答案可以进一步扩展
a模0在大多数系统中都是未定义的,尽管有些系统确实将其定义为a。
从开始
long x = 1 ;
以避免被零除。
相关文章:
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 这句话是什么意思 - " vector<long long> distance(n, std::numeric_limits<long long>::max()); "?
- C ++分割错误,为什么使用"long long"我没有得到答案?
- 以0开头的值初始化long-long会导致奇怪的值
- "1L << count"是什么意思?如何使用超出"unsigned long long int? "范围的语句编号打印?
- 为什么我的函数返回 long long 返回 705032704?
- 如何在 Turbo C++ 3.2 编译器中使用 long long 关键字
- "How to use long long data-type rather than pointers data-type to modify other variables ?"
- 只读位置'__result.std::_Rb_tree_const_iterator<_Tp>::operator*<long long int>()'分配错误
- 没有匹配函数来调用"std::basic_ofstream<char>::write(std::string*, long long unsigned int)"
- 如何处理计算中的"unsigned long long int"?
- 将long-long的数组转换为字符串
- unsigned int/signed int/long-long:无法解释的输出
- "long"和"long int"以及"long long"与"long long int"有什么区别
- map(long long int,long long int)不适用于key=1000 000 000(在long l
- 如果在我的机器上 sizeof(long) == sizeof(long long),为什么它们不是同一类型?
- long long value in Visual Studio
- C++定义了long-long,但输出是有限的
- c++中long-long的模运算