Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What data type should be used for a probability? [closed]

What data type should be used to store a probability (that goes therefore from 0 to 1)? Is there a more efficient way than using a double or float with value control (0 ≤ x ≤ 1)?

like image 584
Robb1 Avatar asked Oct 18 '22 00:10

Robb1


1 Answers

A common alternative choice is fixed-point arithmetic on unsigned short or unsigned int with the decimal point set to the far left: so, for the usual unsigned short, the value range is either from 0.00000 = 0/65535 to 1.0000 = 65535/65535, or 0.0000 = 0/65536 to 0.99998 = 65535/65536, depending on whether you would rather be able to represent 1.0 or 0.5 exactly.

The major advantages of this design are that the representable probabilities are spaced uniformly over the unit interval, and it's impossible for any calculation to produce a value outside the mathematically meaningful range. The major disadvantages are that P(AB) cannot be computed by simple multiplication, you have to choose which of 1.0 and 0.5 can be represented exactly, and underflow is much more likely to bite you. Performance is probably a wash on modern CPUs.

I don't know what you mean by "more efficient" so I can't be more specific.

like image 135
zwol Avatar answered Oct 27 '22 00:10

zwol