Solved

type casting!!!

Posted on 2004-04-22
9
300 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 11

Expert Comment

by:avizit
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 11

Expert Comment

by:avizit
Comment Utility
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
Comment Utility
@abhijit
Is the promotion order fixed for C++ or is it compiler dependent?
0
 

Author Comment

by:player8
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now