Public Group

# A simple problem. DOUBLE TROUBLE

This topic is 4851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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
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
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.

• 10
• 17
• 9
• 14
• 41