[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

type casting!!!

Posted on 2004-04-22
9
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 80 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 clear a vector as well as how to detect empty vectors in C++.

656 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