Calculating Probabilities in C++

In summary, the conversation revolved around creating a program to calculate a shootout between 3 people with different levels of accuracy. The discussion also included ways to represent 1/3 as a float/double for more accurate calculations, sources for relevant reading material and source code, and the possibility of responding without bumping the thread. The key point was that at least one of the operands should be a floating point type for accurate results.
  • #1
jhudson1
16
0

Homework Statement



Working on a program that will calculate the results of a shootout between 3 people, each of whom have a different level of accuracy (one hits his mark 1/3 of the time, one hits his mark 1/2 of the time, one hits his mark 1/1 of the time).


Homework Equations



What's a way to represent 1/3 as a float/double so that I can make my calculations as accurate possible? I'm sure there are multiple ways, but I'm curious what kind of options I have available to me.

Does anyone have some links to relevant reading material? Or some source code that addresses a similar problem that I might examine?

Anything welcome.

P.S. Is there any way to sage a post on the forum? i.e. respond but not bump the thread?
 
Physics news on Phys.org
  • #2
like this

Code:
double d = 1.0/3;

or

Code:
double d = 1/3.0;

or

Code:
double d = 1/3.;

or

Code:
double d = static_cast<double>(1)/3;

etc.

but not

Code:
double d = 1/3;

because the "1/3" part would evaluate to an integer with value zero

The important thing to know is that at least one of the operands (numerator or denominator) should be a floating point type.
 
  • #3
Great. Thank you!
 

FAQ: Calculating Probabilities in C++

1. How do I calculate probabilities in C++?

To calculate probabilities in C++, you can use the rand() function to generate random numbers, and then use conditional statements to determine the probability of a certain event occurring. You can also use mathematical equations and statistical methods to calculate probabilities.

2. What is the difference between probability and odds?

Probability is a measure of the likelihood of an event occurring, expressed as a number between 0 and 1. Odds, on the other hand, are a way of expressing the ratio of the probability of an event occurring to the probability of it not occurring. In other words, odds are the ratio of the number of ways an event can occur to the number of ways it cannot occur.

3. How do I handle errors or exceptions when calculating probabilities in C++?

In C++, you can use try-catch blocks to handle errors or exceptions that may occur when calculating probabilities. You can also use conditional statements and input validation to prevent errors or handle unexpected input.

4. Can I use libraries or functions for calculating probabilities in C++?

Yes, there are many libraries and functions available for calculating probabilities in C++. Some popular libraries include Boost, GSL, and GNU Scientific Library. You can also create your own functions or classes to calculate probabilities according to your specific needs.

5. Is there a limit to the number of events I can calculate probabilities for in C++?

No, there is no specific limitation on the number of events you can calculate probabilities for in C++. However, as the number of events increases, the complexity of the calculations may also increase. It is important to consider the efficiency and scalability of your code when working with a large number of events.

Similar threads

Replies
11
Views
1K
Replies
15
Views
2K
Replies
7
Views
1K
Replies
12
Views
2K
Replies
2
Views
4K
Replies
6
Views
4K
Replies
15
Views
6K
Replies
7
Views
2K
Replies
3
Views
1K
Back
Top