为什么会有 SIGFPE?
由于某种原因,它曾经有效。但现在我收到了 SIGFPE...出了什么问题?
#include "usefunc.h"
long factorial(long num) {
if (num > 1) {
long counter;
long fact = 1;
for (counter = num; counter > 0; counter--) fact *= counter;
return fact;
}
else return 0;
}
long combinations(long n, long k) {
return (factorial(n)) / (factorial(k)*factorial(n-k));
}
int main()
{
printf("How many rows of Pascal\'s triangle should I print?\t");
int rows = GetInteger();
long pArray[rows][rows];
int counter;
int counter2;
for (counter = 1; counter <= rows; counter++)
{
int y = rows-counter;
for (; y > 0; y--) printf(" ");
for (counter2 = 0; counter2 <= counter; counter2++)
{
/*
THIS IS AN OUTPUT
*/
printf("%9.0lu", (long) combinations(counter, counter2));
pArray[counter][counter2] = (long) combinations(counter, counter2);
}
/*
THIS IS AN OUTPUT
*/
printf("\n");
}
return 0;
}
for some reason, it used to work. but now i get a SIGFPE.....what's wrong?
#include "usefunc.h"
long factorial(long num) {
if (num > 1) {
long counter;
long fact = 1;
for (counter = num; counter > 0; counter--) fact *= counter;
return fact;
}
else return 0;
}
long combinations(long n, long k) {
return (factorial(n)) / (factorial(k)*factorial(n-k));
}
int main()
{
printf("How many rows of Pascal\'s triangle should I print?\t");
int rows = GetInteger();
long pArray[rows][rows];
int counter;
int counter2;
for (counter = 1; counter <= rows; counter++)
{
int y = rows-counter;
for (; y > 0; y--) printf(" ");
for (counter2 = 0; counter2 <= counter; counter2++)
{
/*
THIS IS AN OUTPUT
*/
printf("%9.0lu", (long) combinations(counter, counter2));
pArray[counter][counter2] = (long) combinations(counter, counter2);
}
/*
THIS IS AN OUTPUT
*/
printf("\n");
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的阶乘返回 0,这可能会导致除以 0 错误。不应该返回1吗?
your factorial returns 0, which then can cause a divide-by-0 error. shouldn't it be returning 1?
我认为这是您的
combinations
函数,您没有向我们展示该函数,因为您给出的代码都没有使用任何浮点。SIGFPE
并不意味着浮点异常,即使这就是名字的由来。 @jcomeau 已正确确定您收到 SIGFPE 的原因。I think it's your
combinations
function, which you haven't shown us, because none of the code you've given uses any floating-point whatsoever.SIGFPE
does not mean floating-point exception, even if that's where the name came from. @jcomeau has correctly identified why you're getting SIGFPE.