Search code examples
algorithmprobabilitylong-integer

Calculating iterative probability using only longs


I am trying to calculate an iterative probability using only longs. In other words, I know that I can calculate this probability with the closed form of P(x) = 1-(1-p)^x when p is the probability per occurrence and x is the number of occurrences. But I can't use floats so I can't just iteratively multiply.

The function will take x (the number of occurrences). It will have access to global variables for the numerator and denominator of p( for example d = 100,000,000 and n1 = 500,000 for a p of 1/200). It will then return a long , n2, for which P(iterative) = n2 / d.

I know that eventually n2 will approach d but for my purposes, it shouldn't ever really get that close. I just want to be able to do this without risking overflow and only having access to 64 bit registers.


Solution

  • If there are no limits on the argument values, this is impossible.

    For instance, with p=99/100 and x=10, no ratio of 64 bits integers can represent the result.

    And even when a ratio is feasible, you may see a serious loss of accuracy.