Sign in to follow this  
Marz

A simple problem. DOUBLE TROUBLE

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this