C : My 'Poisson calculator' gives me #1. INF00。为什么会出现这种情况?

标签 c floating-point poisson

首先,这是我的代码:

#include <stdio.h>
#include <conio.h>
#include <math.h>

main()
{
    float Exp, Act, Px, Facto_Act;
    printf("\n This is Poisson Distribution Calculator!");
    printf("\n Enter the expected value of success in a time period:");
    scanf("%f",& Exp);
    printf("\n Enter the actual or calculated value of success in a time period:");
    scanf("%f",& Act);
    Px=pow(M_E,-Exp)*pow(Exp,Act)/Facto_Act;
    printf("\n Poisson probability is:%f", Px);
    getch();
    return 0;
}

Facto_Act(float Act)
{
    float c;
    float result=1;
    for(c=1;c<=Act;c++)
        result=result*c;
    return result;
}

进一步说明:

泊松方程如下所示:

P(x)= (e^-Lambda)(Lambda^x)/(x!)

Exp:给定时间内的预期事件数(Lambda) Act:给定时间内的实际事件数 (x) Px:给定时间内事件发生的概率( P(x) ) Facto_Act:给定时间内实际事件数的阶乘(x!)

当我弄清楚如何在 C 中对整数进行阶乘时,我也会尝试对正小数进行阶乘。但 #1.INF00 不是我期望的值。

当我编译代码时,不再显示编码错误。但是,当我输入一段时间内成功的预期值,然后输入一段时间内成功的实际值时,我总是以#1.INF00 结束。我对 C 非常菜鸟,虽然这个网站帮助我改进了我的程序,但我无法理解“#1.INF00”的含义。

我决定不将 Facto_Act 设为函数

我决定通过不将 Facto_Act 设为函数然后尝试调用它来规避整个 Facto_Act 函数问题。看起来阶乘可以在不为其创建新函数的情况下执行。因此 Facto_Act 现在是一个变量。这是我的新代码:

#include <stdio.h>
#include <conio.h>
#include <math.h>


main()
{
    double Exp, Px;
    int c, Act, Act2, Facto_Act=1;
    printf("\n This is Poisson Distribution Calculator!");
    printf("\n Enter the expected value of success in a time period:");
    scanf("%lf",& Exp);
    printf("\n Enter the actual or calculated value of success 
    \n in a time period(must be an integer!):");
    scanf("%d",& Act);
    /*My factorial starts here*/
    for (c=1;c<=Act;c++)
        Facto_Act=Facto_Act*c;
    /*My factorial ends here*/
    Px=(pow(M_E,-Exp))*(pow(Exp,Act))/Facto_Act;
    printf("\n Poisson probability is:%lf", Px);
    getch();
    return 0;
}

感谢大家对我的帮助。

最佳答案

您声明了一个名为 FactoAct 且类型为 float 的变量。由于它是一个没有初始化的外部变量,因此它的值为0。

稍后定义一个隐式返回类型为“int”的函数 Facto_Act(float Act)。

除法 xxx/FactoAct 将 xxx 除以变量 FactoAct,该变量为零。这就是您的 INF 结果的来源。

当函数位于顶部时,当编译器看到 xxx/FactoAct 时,FactoAct 不是函数调用的结果,而是函数本身。您不能将数字除以函数。这没有道理。您对函数唯一能做的就是获取它的地址,或者调用它。

您可能想要 FactoAct (x) 或类似的东西。

PS。不要使用 float 代替 double,除非您有一个理由可以清楚地说明为什么在您的特定情况下 float 比 double 更好。

关于C : My 'Poisson calculator' gives me #1. INF00。为什么会出现这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34332300/

相关文章:

r - xgboost泊松回归: label must be nonnegative

c - Emacs 缩进

c - 入队函数仅在第二次迭代时覆盖第一个条目

c - 从文件中读取整数

c - C 中的 IEEE-754 浮点异常

c# - System.Decimal 是否一致?

c - 将多个 3D 数组发送到 MPI 中的进程

java - Java 中的 x86 80 位浮点类型

r - R 中泊松回归的预测不准确

Python:根据属于特定范围内的项目数量从列表中创建分布