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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:32:28  来源:igfitidea点击:

Getting the error floating point exception: 8

c++

提问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, istarts from 0

你不能 % 反对 0,i0

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 longor long longinstead of regular long.

我在任何地方都没有看到浮点数,但如果我不得不猜测是因为它是由于溢出。使用unsigned long longorlong long代替常规long.

sizeof(long)on some compilers has evaluated to 4, similar to sizeof(int), which means that the limit of longis 2147483647. long longis required by the C++ standard to be at least 64-bits, double that of longand int, which has a signed maximum of 9223372036854775807.

sizeof(long)在某些编译器上评估为 4,类似于sizeof(int),这意味着 的限制long为 2147483647。long longC++ 标准要求至少为 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 = 0was 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/outwas causing an overflow of int(because of 0, there's not enough room to fit INT_MAX+1in 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+1int)。我添加了一个新条件来解决浮点问题:out == -1 && (x > INT_MAX || x <= INT_MIN)