• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Quick entrylvl C++ question

Fiveohhh

Diamond Member
I just started my first programming class and was given a program and need to fix part of it to work. All it does is convert celsius to farenheit. I've narrowed it down to one line.

This is how the line is written on his program:
tempCentigrade = 5/9 * (tempFahrenheit - 32);

If I change it to:
tempCentigrade = (tempFahrenheit - 32) * 5/9;

The program works as it should. When I put data into the original program I keep getting -0.0 as the result.

I'm just wondering why it needs to be this way.
Thanks
 
Sorry for my ignorance as this is only my second week😀 the variables tempFarenheit and tempCentigrade are declared as double. I tried posting the whole code, but the forum keeps coming up with an error.
 
cast the 5/9 division as well e.g. (double)(5/9) as that is the integer math...
Bill
 
Originally posted by: Fiveohhh
Ahh, I see. Why does it work when I put it (the 5/9) at the end of the line than?

I'd need to dig thru the operator precidence, but the short version is the compiler is doing the right thing with the types in that case. What compiler, gcc or msvc?
 
double tempCentigrade, tempFahrenheit;
...

tempCentigrade = ( tempFahrenheit - 32.00 ) * ( 5.00 / 9.00 );

I prefer to tack on a ".00" or even just a "." after any floating point constants to make it clear whats going on to both you and the compiler without ugly casting kludges, etc. Also parenthesis are free, make use of them to ensure order of operations when you aren't sure yourself, and it also helps yours eyes keep parts of equations in separate steps when reading and makes things much more clear.
 
Originally posted by: bsobel
cast the 5/9 division as well e.g. (double)(5/9) as that is the integer math...
Bill

That would do the casting after doing the integer division resulting in 0. So your cast would give you 0 as a double.

It worked correctly with the 5/9 last because it is doing the order of operations. It is the same as doing

((tempFahrenheit - 32) * 5)/9

Edit is isn't really correct
 
The main thing to learn is that in C/C++, math operations of mixed types may or may not automatically cast / change the types of numbers and variables when you want them to.

The problem is any time you have an operator like * or / between 2 ints, the compiler won't assume you want a float or double result at that point, even if you later assign the result to a float or double.

int i , j ;
double x, y ;

i = 10 ;
j = 3 ;
y = 1.0 ;

x = 10/3 ; // will usually set to 3.0 not 3.33333333

x = y + ( i /3) ; // could go either way 4 or 4.33333
x = ( i /3) + y ; // could go either way 4 or 4.33333

this will always work, because one of the 2 operands is already changed to a double before the "/" takes place:

x = y + ((double) i / j) ; // 4.33333

this is even safer in case you change the formula later:

x = y + ((double) i / (double) j ) ; // 4.33333 for sure

 
smack Down is correct about the casting and order if operation. 5/9 results in an integer , so when you divide 5/9 it would result in a zero, it just keeps everything before the decimal. However the second line, tempFahrenheit is declared as a double, when you subtract 32 from a double it would result in a double. Then when you multiply that double by 5 it would still result in a double, and then when you divide that double by 9, it would result in a double which would give you the right answer. If you want to see what's happening to do this.

double temp = tempFahrenheit - 32;
cout.precision(2);
cout << temp << endl;
temp = temp * 5;
cout << temp << endl;
temp = temp / 9;
cout << temp << endl;
 
Back
Top