0

The program is a Poisson Distribution, but when the number is very large, the factorial is imprecise and adds a few thousandths.

How could double make more precision? Or is there a value with larger size?

for (int x = 0; x <= 15; x++)
{
    Console.WriteLine("\n\nx:{0} -------------------------- ", x);
    double num1 = Math.Pow(e, -lambda);
    double num2 = Math.Pow(lambda, x);

    double num = num1 * num2;

    fact = 1;
    for (i = 1; i <= number; i++)
    {
        fact = fact * i;
    }
    //Fact

    double fxres = 0;
    fxres = num / fact;
    Console.WriteLine("F(x): " + fxres);
    fx+= fxres;
    Console.WriteLine("\nSum: " + fx);

    number++;
Jugger
  • 5
  • 1
  • I think decimal has more precision – micah May 27 '22 at 13:38
  • 2
    Does this answer your question? [Can C# store more precise data than doubles?](https://stackoverflow.com/questions/5473136/can-c-sharp-store-more-precise-data-than-doubles) – GSerg May 27 '22 at 13:38
  • 1
    What are your requirements? Would imprecision of a few millionths be OK? How about a few billionths ? – Neil May 27 '22 at 13:39
  • If you were to measure the circumference of the earth using `double` your precision would be off by nano-meters. If that is not enough, use 128-bit decimal values. – John Alexiou May 27 '22 at 13:48
  • `Math.Pow(e, -lambda)` never do this. Use `Math.Exp(-lambda)` instead. You use exp to get `e^x` in almost all programming languages – phuclv May 27 '22 at 14:39

0 Answers0