1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Calculating Probabilities in C++

  1. Sep 8, 2012 #1
    1. The problem statement, all variables and given/known data

    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).

    2. Relevant 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?
  2. jcsd
  3. Sep 8, 2012 #2
    like this

    Code (Text):

    double d = 1.0/3;

    Code (Text):

    double d = 1/3.0;

    Code (Text):

    double d = 1/3.;

    Code (Text):

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

    but not

    Code (Text):

    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.
  4. Sep 8, 2012 #3
    Great. Thank you!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook