user2879969
user2879969

Reputation: 51

probabilities with small numbers

I am working with large amounts of probabilities that I multiply so i quickly obtain very small numbers. But it seems that python finally store the final result as zero.

to overpass this difficutly, I decided to sum the logs of these probabilities (instead of directly multiplying the probabilities). This strategy returns a negative number (call it c) as expected.

But then, if I want to apply the exponential on c (to come back on the real value of my product of probabilities), I obtain the value zero because c is too largely negative (something like -123445,4).

How could I overpass this problem?

Upvotes: 1

Views: 285

Answers (1)

helb
helb

Reputation: 7773

If you are going to use numbers of that magnitude you should use a specialized library which can handle arbitrary floating point precision. Check out mpmath or bigfloat package for example.

Computers natively only support number down to approximately exp(-300). Alternatively, you could restrict your code to store only the exponent and never convert it in a decimal representation.

Upvotes: 1

Related Questions