• Advertisement
Sign in to follow this  

A simple problem. DOUBLE TROUBLE

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement