繁体   English   中英

为什么会有 SIGFPE?

[英]why is there a 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;
}

您的阶乘返回 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

我认为这是您的combinations function,您没有向我们展示,因为您提供的代码都没有使用任何浮点。


SIGFPE并不意味着浮点异常,即使这就是名称的来源。 @jcomeau 已正确确定您获得 SIGFPE 的原因。

暂无
暂无

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

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