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
)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With