Calculating Probabilities in C++

  • Context: Comp Sci 
  • Thread starter Thread starter jhudson1
  • Start date Start date
  • Tags Tags
    C++ Probabilities
Click For Summary
SUMMARY

This discussion focuses on calculating probabilities in C++ for a shootout scenario involving three participants with varying accuracy levels: 1/3, 1/2, and 1/1. The key issue addressed is how to represent fractions accurately as floating-point numbers in C++. The recommended methods include using double precision with expressions like double d = 1.0/3; or double d = static_cast(1)/3;. It is emphasized that at least one operand must be a floating-point type to avoid integer division errors.

PREREQUISITES
  • Understanding of C++ data types, specifically float and double
  • Knowledge of basic arithmetic operations in C++
  • Familiarity with type casting in C++
  • Basic probability concepts related to accuracy and hit rates
NEXT STEPS
  • Research C++ floating-point precision and its implications on calculations
  • Explore C++ type casting techniques in detail
  • Learn about probability theory as it applies to game mechanics
  • Examine sample C++ code for probability calculations in gaming scenarios
USEFUL FOR

C++ developers, game programmers, and anyone interested in implementing probability calculations in software applications.

jhudson1
Messages
16
Reaction score
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
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.
 
Great. Thank you!
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K