Understanding Printf Formatting in C for Mathematical Operations

  • Thread starter Thread starter Spectre32
  • Start date Start date
AI Thread Summary
The discussion revolves around calculating two expressions in C using given values for delta, beta, gamma, c, b, and d. The first expression, E, is calculated as E = (delta * beta) / gamma + c / b. The issue arises because c and b are integers, leading to integer division, which truncates the result. To achieve the correct result of E = 5.333, it is necessary to cast either c or b to a double to ensure floating-point division. The second expression, F, is calculated as F = (d / b) % d, which yields F = 2. This calculation is correct as it uses integer values, and the modulo operation is straightforward. The key takeaway is the importance of type promotion in C, particularly when mixing integer and floating-point types, to avoid truncation and achieve accurate results.
Spectre32
Messages
136
Reaction score
0
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.
 
Computer science news on Phys.org
E = [(delta * beta) / gamma] + (c / b) = 5.333 + 0. The result of c / b is truncated. Similarly, F = (d / b) % d = 2 % 7 = 2.
 
hmmm i think i wa sjust screwing something minor then up. THanks
 
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:
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.
 
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...
Back
Top