#include <iostream>
using namespace std;
int main() {
double fin;
fin = (87/100)* 50;
cout << "\n\t" << fin <<"\n\n\n";
system("PAUSE");
return 0;
}
A simple problem. DOUBLE TROUBLE
I can't figure out why this returns 0.
I would greatly appreciate help.
Thank you.
The type of the variable to which an expression is assigned doesn't affect the way in which the expression is evaluated; it just gets converted at the end.
Therefore, the math is being done with integers. Thus the first operation is an integer division, which results in an integer and discards the remainder. Thus 87/100 yields 0, with a (discarded) remainder of 87. 0 times 50 is still 0. Assigning integer 0 to a double variable requires conversion, but the value is still 0.
Therefore, the math is being done with integers. Thus the first operation is an integer division, which results in an integer and discards the remainder. Thus 87/100 yields 0, with a (discarded) remainder of 87. 0 times 50 is still 0. Assigning integer 0 to a double variable requires conversion, but the value is still 0.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement