简体   繁体   English

为什么我得到浮点异常

[英]Why I am getting floating point exception

I don't think there is any point in my code I am reaching zero or dividing it with zero so can anyone help me why I am getting floating point exception for input 20 75我认为我的代码中没有任何一点我达到零或将其除以零所以任何人都可以帮助我为什么我得到输入 20 75 的浮点异常

I am just calculating factorial for 2*n -1 and diving it with factorial of n and n-1 but I don't know where my code is getting zero or something else is reason我只是在计算 2*n -1 的阶乘,并用 n 和 n-1 的阶乘来计算它,但我不知道我的代码在哪里变为零或其他原因

int fact(int num) {
    if(num == 1 || num == 0) return 1;
    else return (num*fact(num-1));
}
int Solution::solve(int A) {
    int val1 = fact(A-1);
    int val2 = fact(A-1+A-1);
    int ans = (val2/((val1*val1)%1000000007))%1000000007;
    return (ans/A)%1000000007;
}

for A = 20 or A = 75 I am getting floating point exception对于 A = 20 或 A = 75,我得到浮点异常

With input A = 20, you invoke fact(20 - 1) and fact(20 - 1 + 20 - 1) which are fact(19) fact(38) .输入 A = 20,您调用fact(20 - 1)fact(20 - 1 + 20 - 1) ,它们是fact(19) fact(38) Factorial of 19 is 121645100408832000 and factorial of 38 is 523022617466601111760007224100074291200000000. 19 的阶乘是 121645100408832000,38 的阶乘是 523022617466601111760007224100074291200000000。

On typical PC, the largest representable value for int is 2147483647 which is less than either of the above factorials that you attempt to calculate.在典型的 PC 上, int的最大可表示值是 2147483647,它小于您尝试计算的上述任何一个阶乘。 Your program overflows a signed integer, and behaviour of the program is undefined.您的程序溢出了签名的 integer,并且程序的行为未定义。

I don't think there is any point in my code I am... dividing it with zero我认为我的代码没有任何意义...将其除以零

(val1*val1)%1000000007 could be zero for some values of val1 . (val1*val1)%1000000007对于val1的某些值可能为零。 Therefore val2/((val1*val1)%1000000007) may be dividing by zero.因此val2/((val1*val1)%1000000007)可能被零除。 A simple case where that is the case is when val1 is zero and another case is where it is 1000000007. You may be thinking that val1 can never be either of those values since they are not factorials, but it can totally be either value when you have signed overflow in your program.一个简单的情况是val1为零,而另一种情况是 1000000007。您可能会认为val1永远不可能是这些值中的任何一个,因为它们不是阶乘,但是当您在您的程序中签名溢出。

The largest factorial representable by as 32 bit integer is 12. and thus the largest input that your function can solve is A = 7.可表示为 32 位 integer 的最大阶乘是 12。因此您的 function 可以解决的最大输入是 A = 7。

I can't add much but if you would change all int var =... variables to long long int var =... (while I think that the long long int is not recommended to use according to Google's Style Guide) it would work also with input of 20 becuase of the reasons mensioned in previous comments:我不能添加太多,但是如果您将所有int var =...变量更改为long long int var =... (虽然我认为根据 Google 的样式指南不建议使用long long int )它会由于先前评论中提到的原因,还需要输入20

long long int fact(long long int num) {
    if(num == 1 || num == 0) return 1;
    else return (num*fact(num-1));
}
int Solution::solve(int A) {
    long long int val1 = fact(A-1);
    long long int val2 = fact(A-1+A-1);
    long long int ans = (val2/((val1*val1)%1000000007))%1000000007;
    return (ans/A)%1000000007;
}

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

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