-3

Since I am noob in C++, and Udacity Quiz narrates:

The following program produces wrong out put. To fix the program, you need to change at least two of the variable types: the answer, and one of the divisors.

 #include <iostream>
 int main(void)
 {
     int numerator = 4; // no need to change type
     float denominator = 5.0; // changed to float
     float answer = 0.0; // changed to float

     answer = numerator / denominator;
     std::cout<<"answer = "<<answer; // answer = 0.8
     return 0;
 }

Question:How type casting implies here and Why change variable types on both sides of equality.

why not change only answer. as directed I tried setting float to answer and denominator only. and now it works, but I want to learn implicit typecasting in c++.

PS: The code is a quiz by Udacity classroom

Kaleem Ullah
  • 6,799
  • 3
  • 42
  • 47
  • 2
    What is the correct answer here? This will output `answer = 0.8`. Without knowing what you want, sounds good to me.. – Persixty Apr 02 '18 at 11:28
  • 2
    So what’s the question here? Why integer division doesn’t do what they want to happen? Think about it for a moment. – Sami Kuhmonen Apr 02 '18 at 11:29
  • What are you quoting from? Sounds like a recommendation by somebody who knows more about what you try to achieve than you tell readers here. – Yunnosch Apr 02 '18 at 11:34
  • Is the shown code the result of applying the recommendation or is it what needs to be changed? If the code needs to be fixed, it probably is broken now; please explain in which way it is broken. – Yunnosch Apr 02 '18 at 11:35
  • the question is clear, that why I need to change variable type of both sides i.e the answer variable and numerator, denominator variable. why not only changing answer variable type works. – Kaleem Ullah Apr 03 '18 at 07:08
  • @Yunnosch I've already explained what you are asking. please read carefully. that changing type of only `answer` produces wrong out put. but the code sample is fixed. – Kaleem Ullah Apr 03 '18 at 07:30
  • Well I think I got lost in your original formatting style. I see one bold line saying it needs fixing and one bold line saying it is fixed. And a sentence/question with odd capitalisation and punctuation. Then there is the quote-formatted which seems completely out of context. Then you describe that if it is not done like in the first bold it fails. The code has comments which say again that it needs fixing... Probably only me finding it unclear in total. Wait, not only me, at least four others have actually voted to close for lack of clarity, too. And nobody sees a reason to reopen... Fine. – Yunnosch Apr 03 '18 at 07:41
  • @Yunnosch you are right, cause the code with comments has bug in it. and I posted it after fixing that bug. but I got solution to the problem in the answer below by others. – Kaleem Ullah Apr 03 '18 at 13:33
  • @Yunnosch I've improved the question. please vote it. – Kaleem Ullah Apr 12 '18 at 10:49
  • No I won't; and believe me it is in your interest that I don't. The question still has most of the aspects which confused me. How about writing some explaining of your question? Something like I solved this homework assignment: , which was accompanied by this original code . I solved the assignment ifself by changing the code to . But now I wonder why it is necessary to . I think it might have to do with "promotion". But what is the logic? – Yunnosch Apr 12 '18 at 18:26

2 Answers2

2

If the both operands of the expression

numerator / denominator

have integer types then the result of the expression is also has the common integer.

To make the result of a floating type one of the operands has to be of a floating type.

On the other hand if the variable result has an integer type then there can be a truncation of the floating type expression

numerator / denominator
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

The division occurs before the assignment. The result of the division between two integers is an integer. That's where truncation occurs, so we know at least one of the operands needs to be float to maintain precision.

Then, as you know, the assignment occurs. An integer variable can't maintain the precision of the floating-point result, so that needs to be float as well.

Which means if operands of division are not the same then one will be promoted to match the other. like

 int numerator = 4;
 float denominator = 5;

on division:

numerator / denominator;

numerator will be promoted to float. as pointed in this answer

float / int =>  float / float = float
int / int = int
Kaleem Ullah
  • 6,799
  • 3
  • 42
  • 47
  • 1
    When one of the operands has less precision as in your example, type promotion occurs. [Here's a question](https://stackoverflow.com/questions/5563000/implicit-type-conversion-rules-in-c-operators) about implicit type promotion. –  Apr 02 '18 at 11:46