0

I am working on a homework question and I have been able to do everything but this one question. I think I have an idea of how to go about it, I just want to make sure.

Assume the following declarations:

int a =1, b = 2, c = 3;

Fully parenthesize and evaluate each of the following expressions assuming they are evaluated in the order in which they are written.

So I have 2 expressions:

(double)a / b * c

a / (double)(b + c)

which I parenthesized to

((double)a) / (b * c)

(a) / ((double)(b + c))

I am just not sure on how to go about solving the double part of the question, does this just mean that it the evaluation of this will be printed in such a fashion of like 3.000 or 5.000 not just like a regular int such as 3 or 5?

Community
  • 1
  • 1
Tom
  • 49
  • 5
  • 1
    I'd say your first expression is incorrectly parenthesized. – EOF Oct 04 '15 at 19:54
  • Evaluation and printing are separate issues - but your idea is close to being correct. Post code showing how you want to print the results. – chux - Reinstate Monica Oct 04 '15 at 19:55
  • In C the division operator returns different results depending on the type of the operands. So read up on integer vs floating point division. For example: http://stackoverflow.com/questions/16221776/why-dividing-two-integers-doesnt-get-a-float – kaylum Oct 04 '15 at 19:58
  • The specific question is not clear. – clearlight Oct 04 '15 at 20:12

2 Answers2

2

I guess you just need to understand that any time there is an operation between an integral type and a floating point type, the integral type is promoted to the floating type so ...

Example 1:

(double)a/b * c
>>> 1.0/2*3 = (0.5) * 3 = 1.5

which is different than a non casted version that integer division would drop the remainder and you would end up with 0... 1/2*3 -> 0 * 3 -> 3

Example 2:

a/(double)(b+c)
>>> 1/ (double) (2+3) = 1/(5.0) = .20

so when you then assign these values to a float or a double, they will be those values, if they are assigned to an integral type they will be 1, and 0 respectively, because it drops the remainder...

as to what part the cast applies to (which I think is part of your question)... there is a very set order of operations in C, see (operator precedence in c)... you will see that a cast is higher precedence than division... otherwise...

(double)1/2 * 3 -> (double)(1/2) * 3
>>> 0.0 * 3 = 0.0
Grady Player
  • 14,399
  • 2
  • 48
  • 76
  • What? @Tom, suggest to read this answer again ... (edit: and maybe read mine, too ... still something unclear?) –  Oct 04 '15 at 20:16
  • yeah pretty much, that is how the float to int conversion works... if you want rounded values you need to round before the conversion, or simple add 0.5f (which pretty much gets you where you want) i = f + 0.5; (but if it really really matters you should use a library rounding function.) – Grady Player Oct 04 '15 at 20:18
1

I am just not sure on how to go about solving the double part of the question, does this just mean that it the evaluation of this will be printed in such a fashion of like 3.000 or 5.000 not just like a regular int such as 3 or 5?

It means the value is converted to a double. Printing is an entirely different (and, here, unrelated) issue. You will have a temporary with an internal representation allowing for floating point values. This will btw force all other ints in the same expression to be converted to double, too.

(double)a/b * c
[...]
((double)a)/(b*c)

This is wrong. / and * share the same order of precedence ... so, being evaluated like written, it should be (((double)a)/b)*c.