为什么会有 SIGFPE?

发布于 2024-11-13 08:00:44 字数 1124 浏览 4 评论 0原文

由于某种原因,它曾经有效。但现在我收到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿光 2024-11-20 08:00:44

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

your factorial returns 0, which then can cause a divide-by-0 error. shouldn't it be returning 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
小苏打饼 2024-11-20 08:00:44

我认为这是您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文