Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Printf formatting question

  1. Dec 11, 2004 #1
    Ok perhaps i'm not grasping how C does math. I have these few questions:

    E = delta*beta/gamma+c/b

    F = (d/b)%d;

    The print formats for those are %2.3lf and %3d respectively. Here are the values:

    delta = 4 .000
    beta = 4.0
    gamma = 3.0
    c = 2
    b = 3
    d = 7

    c,b,d are type int and the rest are type double. On my answer sheet they says the answers for those two are E = 5.333 and F = 2 i have the spaces down, but i'm having a problem getting to those answers.

    Any help would be great.
  2. jcsd
  3. Dec 12, 2004 #2
    E = [(delta * beta) / gamma] + (c / b) = 5.333 + 0. The result of c / b is truncated. Similarly, F = (d / b) % d = 2 % 7 = 2.
  4. Dec 12, 2004 #3
    hmmm i think i wa sjust screwing something minor then up. THanks
  5. Dec 12, 2004 #4
    you can cast the ints into doubles if you wish

    ... + (double) c / (double) b

    actually you only need to cast one of them as when an operation involves a mixture of ints and doubles, the remaining int(s) will be promoted automatically.

    However, with your formula, the c/b part is calculated first, and as they are both ints, an integer calculation is done. then the answer to that calculation is promoted to a double, so that it can be added to the other doubles.
    Last edited: Dec 12, 2004
  6. Dec 12, 2004 #5
    well see these are like test questions, and I wanted to know how to do them in the proper way. If it was up to me everything would be double.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook