为什么有SIGFPE?

时间:2011-06-04 04:29:53

标签: c signals pascals-triangle 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;
}

2 个答案:

答案 0 :(得分:6)

你的阶乘返回0,然后可能导致0分频错误。不应该返回1?


jcomeau@intrepid:/tmp$ cat test.c; make test;./test
#include <stdio.h>
int main() {
 return printf("%f\n", 1L / 0);
}
cc     test.c   -o test
test.c: In function ‘main’:
test.c:3: warning: division by zero
Floating point exception

答案 1 :(得分:2)

我认为这是你的combinations函数,你没有向我们展示过,因为你给出的代码都没有使用任何浮点数。


SIGFPE does not mean floating-point exception,即使这个名字来自于此。 @jcomeau已正确识别您获得SIGFPE的原因。