Unexpected variable behaviour

This is a discussion on Unexpected variable behaviour within the C++ Programming forums, part of the General Programming Boards category; I'm still very much a programming newbie, so maybe this all makes perfect sense but it wasn't the behaviour I ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    26

    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

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    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.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I should've been more specific. I was never referring to assumptions about the parenthesis. Forgot about that question while typing.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    26
    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?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by Dondrei View Post
    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)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 12:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  3. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21