# Unexpected variable behaviour

• 02-23-2009
Dondrei
Unexpected variable behaviour
I'm still very much a programming newbie, so maybe this all makes perfect sense but it wasn't the behaviour I was expecting. I wrote a Celsius to Fahrenheit converter to show someone the basics of coding, here it is:

Code:

```#include <iostream> using namespace std; int main() {     float var1;     cout << "Please enter degrees Celcius: ";     cin >> var1;     float var2 = var1 * (9/5) + 32;     cout << var1 << " degrees Celsius = " << var2 << " degrees Fahrenheit";     return 0; }```
Now, the parentheses around 9/5 are completely unnecessary, but I put them in thinking it wouldn't make any difference. The code gave me the wrong answers and I think it's because it's actually treating the result of (9/5) as an integer and thus truncating it.

Of course, I just removed the parentheses but I'm interested to know why it behaves this way and if there is a way to get around it.

I've been hobby programming in C++ on and off for a year or two, I wondered why I'd never encountered this issue before but then I realized to my chagrin that everything I'd been playing with was discrete and I'd actually never used a float in C++ before... :P
• 02-23-2009
neandrake
You are correct in your assumptions. It truncates to integer because 9 and 5 are integers. I believe you have to change at least the numerator to 9.0f to convert the result to float.
• 02-23-2009
R.Stiltskin
The * and / operators have the same precedence, so without the parentheses the expresssion var1 * 9 / 5 is evaluated from left to right. var1 is a float so the result of var1 * 9 is a float, and that result / 5 is a float, so without parentheses you get the correct answer.

The parentheses have a higher precedence, so the expression inside the parentheses is evaluated first. There you have an int divided by an int, thus result is truncated. Truncation is just the standard behavior for integer division. If either of the operands is a float (or a double), floating point division will be performed, so you can write (9.0/5) or (9/5.0) or (9.0/5.0) and you will get the correct result.
• 02-23-2009
neandrake
I should've been more specific. I was never referring to assumptions about the parenthesis. Forgot about that question while typing.
• 02-23-2009
Dondrei
Oh right, that makes sense. I was assuming that because the logic was being used in the definition of a float, it would assume all the numbers in it were floats too.

So if I put (9.0/5) for example, it treats them both as a float? How would I get it to treat them as doubles? Would that be 9.0d? Do all the numerical types have a shorthand like that?
• 02-24-2009
iMalc
Quote:

Originally Posted by Dondrei
Oh right, that makes sense. I was assuming that because the logic was being used in the definition of a float, it would assume all the numbers in it were floats too.

So if I put (9.0/5) for example, it treats them both as a float? How would I get it to treat them as doubles? Would that be 9.0d? Do all the numerical types have a shorthand like that?

No, that one is a double. The float version would be (9.0f / 5), or if you prefer (9.f / 5)