When calculating and manipulating probabilities in Java, and then expressing them as percentages, what is the best data structure to use to represent them?
The native double
and float
don't seem like particularly ideal candidates, since they have odd rounding issues which can introduce errors when the rounding happens multiple times and gets compounded.
BigInteger
works well for calculating permutations and combinations, and BigDecimal
seems like it might be a good candidate for the non-integer values, but is there something more suited to dealing with percentages already?
Note: In this case, the probabilities being calculated are similar in nature to those involving decks of cards, but with hundreds of cards. For the more math-inclined, I'm specifically working with Multivariate Hypergeometric_distributions.
You would use the division operator to divide the percentage by one hundred. Example: System. out. println(50/100); 50% equals 50/100, which is 0.5.
When it comes to probabilities, I would suspect you would benefit from working with fractions.
Have a look at this question:
Otherwise I think BigDecimal
is your best choice. Beware however of the java.lang.ArithmeticException: Non-terminating decimal expansion;
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