相关疑难解决方法(0)

浮点异常

我成功编写了这段代码:

#include <stdio.h>
#include <math.h>
int q;

int main()
{
    srand( time(NULL) );
    int n=3;
    q=ceil(sqrt(n));
    printf("%d\n %d\n", n,q);

    if(n == 2)
        printf("%d\n is prime", n);
    else if(n % 2 == 0.0 || n < 2)
        printf("%d\n is not prime", n);
    else
    {
        int x;
        for(x = 0; x < q; x++){
            if(n % x == 0)
            {
                printf("%d\n is not prime", n);
                return;
            }
            else
                printf("%d\n is prime", n);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行我的代码时,我收到以下错误:

浮点异常

这个错误意味着什么,我该如何解决?

c floating-point

12
推荐指数
1
解决办法
13万
查看次数

当没有浮点数据类型时,为什么此代码会出现浮点异常?

我没有除以零,我的代码中没有float数据类型,我仍然得到浮点异常.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    unsigned long long int t,n;

    cin>>t;
    while(t--)
    {
        cin>>n;
        unsigned long long int deno = pow(10,n-1),count=2,sum = 0,f1=1,f2=1;

         while(1){
            sum = f1+f2;
            f1 = f2;
            f2 = sum;
            count++;
            if((int)(sum/deno)>0){
                cout<<count<<endl;
                 break;
             } 
        }

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

所有以前的问题都有类似的问题,除以零,但变量deno永远不会为零n>=2.

以前的研究来自我方:

  1. 代码中的"浮点异常",不包含浮点数
  2. 浮点异常C++为什么以及它是什么?

问题陈述:https://www.hackerrank.com/contests/projecteuler/challenges/euler025/problem

它通过2个测试用例并失败2.所有都是隐藏的测试用例.结果图片

在传递输入150时,我们可以重现错误.细节:

 GDB trace: Reading symbols from solution...done. [New LWP 15127] Core
 was generated …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point-exceptions c++14

6
推荐指数
1
解决办法
1124
查看次数