Problem while Type Casting
Hi all,
I'm a beginner and I tried to write a program to convert celsius into Fahrenheit but i have a problem due to type casting operation.
That is even though i explicitly converted an integer to float number I'm getting only integer value .
Here's the program :
# include<iostream>
using namespace std;
int main()
{
float celsius;
cout<<"Enter the temperature in celsius: ";
cin>>celsius;
float fahrenheit =(celsius*static_cast<float>(9/5))+32;
cout<<"Temperature in Fahrenheit = "<<fahrenheit<<endl;
return 0;
}
Please tell me what is wrong with that code with explanation .
Thanks in advance.
parthiban
Junior Poster in Training
80 posts since Sep 2006
Reputation Points: 10
Solved Threads: 6
The cast is in the wrong place. The sub-expression 9/5 has already happened, and produced an integer result, before the cast happens.
Why not just remove it and type
celsius * 9.0f / 5.0f + 32.0f;
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
9/5 is an integer expression (==1). use
float fahrenheit = celsius * float(9)/5 + 32 ;
it would be better to use doubles instead of floats unless there is an externally imposed constraint.
salem, didn't see your post!
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Thanks Salem and Vijay . I now understood .
parthiban
Junior Poster in Training
80 posts since Sep 2006
Reputation Points: 10
Solved Threads: 6
9/5 is an integer expression (==1).
Hi vijay, one more question as you said that expression yields integer " 1" but I'm casting that " 1" to float (I'm not sure just I'm thinking) as
celsius*static_cast(9/5).
so it now becomes float (I think) .
now float * float+32 = float .
What i expected is the result should be a floating point number but why it gives integer number ?
Sample interaction :
Enter the temperature in celsius: 32
Temperature in Fahrenheit = 64
what i expect is 64.000000 .
Please tell me actually what is happening .
parthiban
Junior Poster in Training
80 posts since Sep 2006
Reputation Points: 10
Solved Threads: 6
what about this
float fahrenheit =(celsius*(9.0/5.0))+32;
no need to type cast
jaepi
Practically a Master Poster
647 posts since Jul 2006
Reputation Points: 32
Solved Threads: 4
The result does not have decimals because 9 and 5 are both integers and 9/5 returns an integer. you are typcasting the result of the division which is 1 to a float. Read Salem's example for the correct way to do what you are attempting, unless you want to get really brillent and appear to be super-smart and do something like this: :cool:
celsius*static_cast<float>(static_cast<float>(9)/static_cast<float>(5))
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
What i expected is the result should be a floating point number but why it gives integer number ?
Please tell me actually what is happening .
stream output in c++ by default omits the decimal point if there are no significant digits after it. so,cout << 64.0 << '\n' ; // prints just 64
cout << showpoint << 64.0 << '\n' ; // prints 64.000000
(the showpoint manipualator makes the stream insert a decimal point even when there are no significant digits after it.)
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Thanks a lot vijayan for spending time to answer my question. now i really understood what happened beyond the scenes .
Once again thanks for spending your valuable time for me.
parthiban
Junior Poster in Training
80 posts since Sep 2006
Reputation Points: 10
Solved Threads: 6