# A simple problem. DOUBLE TROUBLE

## Recommended Posts

Marz    150
I can't figure out why this returns 0. I would greatly appreciate help. Thank you.


#include <iostream>

using namespace std;

int main() {
double fin;

fin = (87/100)* 50;

cout << "\n\t" << fin <<"\n\n\n";

system("PAUSE");
return 0;
}



##### Share on other sites
(87/100) = 0

thats integer division

(87.0f/100.0f) would work however

hope that helps
-Dan

##### Share on other sites
Zahlman    1682
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.

##### Share on other sites
Marz    150
Yup I understand now. Thank you. Man I wish it would just do the conversions. This is going to cause the function that I pulled this from problems as it takes int as arguments from another function. Oh well I'll get it now somehow.

Thanks again folks.