Solved

type casting!!!

Posted on 2004-04-22
9
320 Views
Last Modified: 2010-04-01
Hi everybody,
      int a=20+40.40
The answer i am getting is 60.Actually the sum of 20 and 40.40 is 60.40 but it is stored in a as 60 bcoz a is int.My question is why 60.40 is not retained while storing as float is bigger than int and type promotion should happen
Thank you
0
Comment
Question by:player8
  • 4
  • 4
9 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 10887765
You tell the variable a to be an int. So the compiler does the assignment and stores the result 60.40 into the variable a.

Type promotion does not depend on value of variables but on the order of appearance. In that case the first constant is and int (20) and the result is an int so the compiler can cast 40.40 to int and store the sum in a.
0
 
LVL 11

Expert Comment

by:avizit
ID: 10887773
here the result expected is a int hence the answer is an int

tyype promotion would have occurred if you had

int b = 20;
float c = 40.40 ;
float a = b + c ;

in this case the integer "b" gets promoted to a float
0
 
LVL 13

Expert Comment

by:SteH
ID: 10887776
The promotion needs to be done on order and not on value because the compiler does the translation and the values are not necessarily known before runtime. And you don't really want to recompile your code just because the values of some variables are once in order a,b and later in order b,a would you?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 11

Expert Comment

by:avizit
ID: 10887818
In fact in your example the calculation 20 + 40.40 is done as a floating add  and a float result is generated

but since the answer expected is an int the result is truncated to an int

here is a simple example i wrote to demostrate

#include <iostream>
using namespace std;
int main(){
int b = 20;
float e = 20.90;
float c = 40.40 ;
int a;
a  = b + c + e ;
cout<<"a="<<a<<endl;
return 0;
}

===========

in the above the answer printed is :: 81

which means that while doing the addition the variable b was promoted to a float and the result was truncated while assigning to 'a'  else the answer would have been 80.

/abhijit/
0
 
LVL 11

Expert Comment

by:avizit
ID: 10887843
quote >>>
The promotion needs to be done on order and not on value because the compiler does the translation and the values are not necessarily known before runtime. And you don't really want to recompile your code just because the values of some variables are once in order a,b and later in order b,a would you?
<<<<

you dont know the values during compilation but you know the "type"  of the variables during compilation  which is sufficient to decide on type promotion ( as in integer/float etc )

/abhijit/
0
 
LVL 13

Expert Comment

by:SteH
ID: 10887847
@abhijit
Is the promotion order fixed for C++ or is it compiler dependent?
0
 

Author Comment

by:player8
ID: 10887862
Thanks  steh and avizit.I learned a crucial point from avizit.I thank u for
participating.
Steh i have got to give u my points.Thanks for helping me.
 
0
 
LVL 13

Accepted Solution

by:
SteH earned 20 total points
ID: 10887881
But in the original question:
>>My question is why 60.40 is not retained while storing as float is bigger than int.

The bigger should not affect the compilation. And size of float and int is on 32bit CPUs 4 bytes each. On 64 bit CPUs an int could be 8 bytes whereas the float should stay at 4 bytes so what is bigger now?
0
 
LVL 11

Expert Comment

by:avizit
ID: 10887932
It should "not" be compiler dependent ,

in the case mentioned i.e  int a=20+40.40  the addition was done using a float add but it got truncated as the expected result  is int


==
from K&R

when an operator has operands of different types, they are converted to a common type according to a small number of rules. In general , the only automatic conversions are those that convert a "narrower" operand into a 'wider" one without losing information

/abhijit/
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question