C++ 获取错误浮点异常:8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14638632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the error floating point exception: 8
提问by Josh Horowitz
I have no idea why g++ doesn't like my code. It ran fine in java. Any insights would be greatly appreciated.
我不知道为什么 g++ 不喜欢我的代码。它在java中运行良好。任何见解将不胜感激。
#include<iostream>
using namespace std;
bool isPrime(long number);
int main(){
const long number = 600851475143;
long max = 0;
for(long i= 0; i*i <= number; i++)
if(number % i == 0 && isPrime(i))
max = i;
cout<< max << endl;
return 0;
}
bool isPrime(long number){
if(number <= 1) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(long i= 3; i*i <= number; i+=2)
if(number % i == 0)
return false;
return true;
}
回答by billz
const long number = 600851475143;
There is overflow, long can't hold that big number.
有溢出,长不能容纳那么大的数字。
see this link
看到这个链接
LONG_MAX is 2147483647
try:
尝试:
const unsigned long long number = 600851475143;
unsigned long longmax = 0;
Edit:
编辑:
You can't % against 0, i
starts from 0
你不能 % 反对 0,i
从0
for(long i= 0; i*i <= number; i++)
^^
{
if(number % i == 0 && isPrime(i))
^^^
{
max = i;
cout<< max << endl;
}
}
}
Minor change to a working version:
对工作版本的小改动:
bool isPrime(unsigned long long number);
int main(){
const unsigned long long number = 600851475143;
unsigned long long max = 0;
for(long i = 1; i*i <= number; i++)
{
if(number % i == 0 && isPrime(i))
{
max = i;
cout<< max << endl;
}
}
return 0;
}
bool isPrime(unsigned long long number)
{
if(number <= 1) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(unsigned long long i= 3; i*i <= number; i+=2)
{
if(number % i == 0)
{
return false;
}
}
return true;
}
回答by Rapptz
I don't see a floating point anywhere, but if I had to guess it's because it's due to overflow. Use unsigned long long
or long long
instead of regular long
.
我在任何地方都没有看到浮点数,但如果我不得不猜测是因为它是由于溢出。使用unsigned long long
orlong long
代替常规long
.
sizeof(long)
on some compilers has evaluated to 4, similar to sizeof(int)
, which means that the limit of long
is 2147483647. long long
is required by the C++ standard to be at least 64-bits, double that of long
and int
, which has a signed maximum of 9223372036854775807.
sizeof(long)
在某些编译器上评估为 4,类似于sizeof(int)
,这意味着 的限制long
为 2147483647。long long
C++ 标准要求至少为 64 位,是long
和 的两倍int
,其有符号最大值为 9223372036854775807。
The error stems from your code: You're doing modulus by zero, which is wrong.
错误源于您的代码:您将模数为零,这是错误的。
Consider doing this instead:
考虑这样做:
#include <iostream>
using namespace std;
bool isPrime(unsigned long long number);
int main(){
const unsigned long long number = 600851475143;
unsigned long long max = 0;
for(unsigned long long i= 1; i*i <= number; i++)
if(number % i == 0 && isPrime(i))
max = i;
cout<< max << endl;
return 0;
}
bool isPrime(unsigned long long number) {
if(number <= 1) return false;
if(number == 2) return true;
if(number % 2 == 0) return false;
for(unsigned long long i= 3; i*i <= number; i+=2)
if(number % i == 0)
return false;
return true;
}
Notice how i = 0
was changed to i = 1
请注意如何i = 0
更改为i = 1
回答by MichaelChirico
For me, this bug came up when checking for integer overflow when doing a product:
对我来说,在做产品时检查整数溢出时出现了这个错误:
#define INT_MIN -2147483648 // -2^31
#define INT_MAX 2147483647 // 2^31-1
int out=-1, x=-5;
if ((out > 0 && (x > INT_MAX/out || x < INT_MIN/out)) ||
(out < 0 && (x < INT_MAX/out || x > INT_MIN/out))) {
// what to do for overflow
} else {
out *= x;
}
The issue is that because abs(INT_MIN) > abs(INT_MAX)
, exactly when out=-1
, the condition INT_MIN/out
was causing an overflow of int
(because of 0
, there's not enough room to fit INT_MAX+1
in int
). I added a new condition to fix the floating point issue: out == -1 && (x > INT_MAX || x <= INT_MIN)
这个问题是因为abs(INT_MIN) > abs(INT_MAX)
,什么时候out=-1
,条件INT_MIN/out
是造成的溢出int
(因为0
,有没有足够的空间,以适应INT_MAX+1
中int
)。我添加了一个新条件来解决浮点问题:out == -1 && (x > INT_MAX || x <= INT_MIN)