Thread: float/int issue if converting C to F

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    72

    float/int issue if converting C to F

    Hello,

    I created a program to convert Celsius to Fahrenheit.

    Code:
    #include <iostream>
    using namespace std;
    float Fahrenheit, Celsius;
    int main()
    {
    float Fahrenheit;
    float Celsius;
    
    cout<<"Enter the temperature in Fahrenheit: ";
    cin>>Fahrenheit;
    
    Celsius=(Fahrenheit-32)*5/9;
    
    cout<<"The temperature in Celsius is: "<<Celsius;
    cout << (int)Celsius << endl;
    system ("pause");
    
    
    return 0;
    }
    But it needs to round up or down. Meaning 55 F need to be 13 Celsius and not 12.7... I read now that I need to change it to int..which I did right here

    Code:
    cout<<"The temperature in Celsius is: "<<Celsius;
    cout << (int)Celsius << endl;
    system ("pause");
    Also I need to do it by adding 0.5 to the calculation expression. Well that's it

    Code:
    Celsius=(Fahrenheit-32)*5/9;
    but of course it wont work, if I do add + 0.5 to it. Can someone help me with this?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Casting always rounds down.

    Code:
    cout << (int) (Celsius + 0.5) << endl;

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You could find the the value of the first decimal place by multiplying by ten, and finding the remainder when you divide by ten:

    Code:
    int d = (Celsius * 10) % 10;
    If that number is greater than 5, cast it to an int and add one. If not, just cast it to an int.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by cyberfish View Post
    Casting always rounds down.

    Code:
    cout << (int) (Celsius + 0.5) << endl;

    Dosen't work... 55F is still 12.7 C if I do this.
    Last edited by XodoX; 02-08-2009 at 06:13 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Isn't this C++?

    Dosen't work... 55F is still 12.7 C if I do this.
    I do not beleive that you can output integer as 12.7
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by vart
    Isn't this C++?
    Certainly looks like it, so this thread has been moved to the C++ programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    The reason it is not working is simple....
    casting does not "round" either way.
    casting a decimal to an int simply truncates everything after the '.' leaving only the int value in the final int variable

    you would have to add 0.5 post F-C conversion and pre cast to int...
    Code:
    Celsius=((Fahrenheit-32)*5/9)+0.5;
    cout << (int)Celsius << endl;
    ...wow, edited for multiple typos lol
    Last edited by QuestionKing; 02-09-2009 at 01:47 AM.

  8. #8
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    Why do you declare your variable globe and then local???



    Code:
    float Fahrenheit, Celsius;
    
    int main()
    {
    
    	float Fahrenheit;
    	float Celsius;

    jw....
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Hey!

    Thanks for the input. It helped a lot! Basically the floating-point datay type is used for those things?

    Like this following code


    Code:
    #include <iostream>  
    using namespace std;
    
    // Function Prototypes
    
    void pause(void);
    
    // Variables
    
    float      pennies;
    float      nickels;
    float      dimes;
    float      quarters;
    double       total_value;
    
    //******************************************************
    // main
    //******************************************************
    
    int main(void)
      {
      // Input	
      cout << "\nHow many pennies do you have? --->: ";
      cin >> pennies;
      cout << "\nHow many nickles do you have? -->: ";
      cin >> nickels;
      cout << "\nHow many dimes do you have -->: ";
      cin >> dimes;
      cout << "\nHow many quarters do you have -->: ";
      cin >> quarters;
      
      // Process
      total_value = (pennies * 0.01) + (nickels * 0.05) + (dimes * 0.1) + (quarters * 0.25) ;
    
      // Output
      cout << "\nThe total value of your coins is: ";
      cout << total_value;
    
      pause();
      return 0;
      }
    
    //******************************************************
    // pause
    //******************************************************
    
    void pause(void)
      {
      cout << "\n\n";
      system("PAUSE");
      cout << "\n\n";
      return;
      }
    Each coins value is a floating-point data type, a fraction of a dollar. So I use the floating-point like this, right? But int would have done it too in this case. So why use floating-point? Cause of the larger values I might get?

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    A good way to do this is:

    Code:
    #include <iostream>
    #include<cmath>
    using namespace std;
    float Fahrenheit, Celsius;
    int main()
    {
    float Fahrenheit;
    float Celsius;
    
    cout<<"Enter the temperature in Fahrenheit: ";
    cin>>Fahrenheit;
    
    Celsius=(Fahrenheit-32)*5/9;
    
    float decimal = Celsius - floorf(Celsius);
    if(decimal < 0.5)
    {
    Celsius = floorf(Celsius);
    }
    else
    {
    Celsius = ceilf(Celsius);
    }
    
    cout << "The temperature in Celsius is: " << Celsius << endl;
    system ("pause");
    
    
    return 0;
    }
    the new code is in bold


    and for the last question, an int would have done it since you are using floating points with coin values...
    Last edited by Cherry65; 02-15-2009 at 04:55 PM.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    When writing code that deals with fractional values, get in the habit of appending ".0" to all integer values to force the compiler to generate floating point code.

    Instead of x*5/9, write x*5.0/9.0, and the math will automatically happen in the floating point domain.

    As far as rounding, the trick:

    Code:
    int rounded = (int)(val + 0.5);
    Only works if val is positive. If val is negative this will round the wrong way. The correct strategy is:

    Code:
    int rounded = (int)(val + (val >= 0) ? 0.5 : -0.5)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Code:
    int rounded = (int)(val + (val >= 0) ? 0.5 : -0.5)
    Code:
    int rounded = (int)(val + ((val >= 0) ? 0.5 : -0.5));
    Soma

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Forgive me but the only difference I see is the parens. Addition is commutative, though.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Forgive me but the only difference I see is the parens.
    Well, that's pretty much the only difference. ^_^

    Addition is commutative, though.
    This is an issue of precedence; not an issue of associativity or mathematics.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  3. Compiling issue with the OpenGL tutorial by RoD
    By Twitchmokey in forum Game Programming
    Replies: 12
    Last Post: 06-26-2006, 06:05 PM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM