简体   繁体   English

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

[英]Why does this code get floating point exception when there is no float data-type?

I'm not dividing by zero and there is no float datatype in my code, I still get floating point exception. 我没有除以零,我的代码中没有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;
}

All the previous questions on the same had the similar problem of dividing by Zero but variable deno can never be zero as n>=2 . 之前的所有问题都有类似的除以零的问题,但变量deno永远不能为零,因为n>=2

Previous research from my side: 以前的研究来自我方:

  1. “Floating point exception” in code that contains no floats 代码中的“浮点异常”,不包含浮点数
  2. Floating Point Exception C++ Why and what is it? 浮点异常C ++为什么以及它是什么?

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

It passes 2 test cases and fails 2. All are hidden test cases. 它通过2个测试用例并失败2.所有都是隐藏的测试用例。 Result image 结果图片

On passing the input 1 50 we can reproduce the error. 在传递输入150时,我们可以重现错误。 Details: 细节:

 GDB trace: Reading symbols from solution...done. [New LWP 15127] Core
 was generated by `solution'. Program terminated with signal SIGFPE,
 Arithmetic exception.
 #0  main () at solution.cc:23 
 23 if((int)(sum/deno)>0){
 #0  main () at solution.cc:23

It is perfectly normal for integer division to produce an exception that is reported as "floating point exception" on some platforms (Linux, for one example). 整数除法产生在某些平台上报告为“浮点异常”的异常(Linux,例如)是完全正常的。 You can easily get it from integer division by zero, or, for another example, by triggering overflow as in 你可以很容易地将它从整数除以零,或者,换句话说,通过触发溢出,如下所示

int i = INT_MIN;
int b = -1;
i = i / b;

http://coliru.stacked-crooked.com/a/07c5fdf47278b696 http://coliru.stacked-crooked.com/a/07c5fdf47278b696

In certain contexts this exception might appear or disappear depending on optimization levels. 在某些情况下,此异常可能会出现或消失,具体取决于优化级别。 The exception is normally only triggered when the compiler decided to generate the actual division instruction (as opposed to optimizing out the division). 通常仅在编译器决定生成实际除法指令时触发异常(与优化除法相反)。


In your case unsigned integer division is used, so division by zero seems to be the only possible culprit. 在你的情况下,使用无符号整数除法,因此除以零似乎是唯一可能的罪魁祸首。 I would guess that this 我猜这个

unsigned long long int deno = pow(10,n-1);

happens to result in zero in deno . 恰好导致deno为零。 pow is a floating-point function that produces a floating-point result. pow是一个浮点函数,可以产生浮点结果。 Conversion from floating-point type to integer type leads to undefined behavior if the original value is too large (which is the case for n equal to 50 ). 如果原始值太大( n等于50的情况),则从浮点类型到整数类型的转换会导致未定义的行为。 Note that this is the case even if the target integer type is unsigned. 请注意,即使目标整数类型是无符号的,也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM