?
Solved

type casting!!!

Posted on 2004-04-22
9
Medium Priority
?
344 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

764 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