Solved

type casting!!!

Posted on 2004-04-22
9
326 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

809 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