473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

internals of typecasting

Hi,
I would like to know more about casts.What exactly happens
when casts are applied to a variable.I know that if an
object of type int is applied an cast of float the result
would be of type float.

1)
What i would like to know is the about the
internals when a cast is applied ? Say we have

int i = 3;
double j;
j = (double) i;

What happens in the above statement ? Can someone
explain me at bit level or a considerable explantion ?
2)
Also i would like to know what happens say

i = (int)j;

Again an explanation at the bit level would be very helpful.
If we have

3)

float f = 4.3;
int i;
i = (int) f;

What happens here ? How does the truncation take place ?
4)
Is it similar to how int got promoted to double in the question 2.
5)
How are side effects defined ? When do
i say typecasting is an side effect ?

6)
Is truncation a side effect of type casting or not ?

Nik
-
Nov 14 '05 #1
4 2789

On Mon, 3 May 2004, Niklaus wrote:

Hi,
I would like to know more about casts.What exactly happens
when casts are applied to a variable.I know that if an
object of type int is applied an cast of float the result
would be of type float.
A better phrasing: "If a value of type int is cast to float,
the resulting value is of type float." Type-casting has nothing
to do with /objects/, which in C are the actual locations where
values can be stored. 42 is not an object; yet (float)42 is still
a valid C expression.
1)
What i would like to know is the about the
internals when a cast is applied ? Say we have

int i = 3;
double j;
j = (double) i;

What happens in the above statement ? Can someone
explain me at bit level or a considerable explantion ?
On the average Wintel machine, this will retrieve the bits stored
in i (say, 0x0300), and pass them through a special function designed
specifically to convert "int bits" to "float bits." On the x86 line
of processors, the FPU has a special machine instruction that will do
this conversion. The result will be a bunch of bits that represent
a float value (say, 0x42DE; I don't know the real internals here).
These "float bits" get stored into the object denoted by j.
2)
Also i would like to know what happens say

i = (int)j;
Same thing in reverse. Again, the x86 processors have a special
floating-point-unit opcode devoted to converting between floating-
point values and integer values. On other machines, maybe the C
runtime library would handle this, essentially replacing cast-
expressions with function calls:

extern int __C_RUNTIME_double_to_int(double);
i = __C_RUNTIME_double_to_int(j);

On other machines, maybe this would simply be a bitwise truncation
of the value of j. It all depends on how the implementation wants
to represent floating-point and integer values.
3)
float f = 4.3;
int i;
i = (int) f;

What happens here ? How does the truncation take place ?
This is the same thing you wrote in #2, except with "double"
replaced by "float." Nothing's different in the explanation.
5)
How are side effects defined ? When do
i say typecasting is an side effect ?
Type-casting is no more a "side effect" than addition or
multiplication. A "side effect" of an operation is something that
*happens*, not something that *is produced*. Examples:
In the expression 2+2, the value 4 *is produced*. Nothing *happens*.
Thus, 4 is the value of the expression, and it has no side effects.
In the expression g=2.0, the value 2.0 is produced. What *happens*
is that 2.0 is assigned to g. Thus, 2.0 is the value of the expression,
and its side effect is to assign 2.0 to g.
In the expression (int)g, the value 2 is produced. Nothing happens.
Thus, 2 is the value of the expression (int)g, and it has no side
effects.
In the expression (a=1,++a), the value 2 is produced. What happens
is that first 1 is assigned to a, and then a is incremented; those are
the side effects of the expression.

You see now? Very simple.

6)
Is truncation a side effect of type casting or not ?


No. When you write (int)43.2, the value of that expression is 43.
It has no side effects. Yes, the value 43 is a "truncated version
of" 43.2, but that's not a side effect; it's just the way C defines
casting from floating-point numbers to integers.

If you have more specific questions about how your own computer
handles floating-point numbers, try asking in comp.programming or in
a forum or newsgroup dedicated to your platform. comp.lang.c is
for questions related to the standard C programming language, as
distinct from any particular implementation or compiler.

HTH,
-Arthur
Nov 14 '05 #2
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi***********************************@unix47 .andrew.cmu.edu>...
On Mon, 3 May 2004, Niklaus wrote:

Hi,
I would like to know more about casts.What exactly happens
when casts are applied to a variable.I know that if an
object of type int is applied an cast of float the result
would be of type float.


A better phrasing: "If a value of type int is cast to float,
the resulting value is of type float." Type-casting has nothing
to do with /objects/, which in C are the actual locations where
values can be stored. 42 is not an object; yet (float)42 is still
a valid C expression.
1)
What i would like to know is the about the
internals when a cast is applied ? Say we have

int i = 3;
double j;
j = (double) i;

What happens in the above statement ? Can someone
explain me at bit level or a considerable explantion ?


On the average Wintel machine, this will retrieve the bits stored
in i (say, 0x0300), and pass them through a special function designed
specifically to convert "int bits" to "float bits." On the x86 line
of processors, the FPU has a special machine instruction that will do
this conversion. The result will be a bunch of bits that represent
a float value (say, 0x42DE; I don't know the real internals here).
These "float bits" get stored into the object denoted by j.

Thanks for the explanation. I would like to know the real internals.
May be this isn't the appropriate group. In case someone knows please
do post a follow up.
2)
Also i would like to know what happens say

i = (int)j;


Same thing in reverse. Again, the x86 processors have a special
floating-point-unit opcode devoted to converting between floating-
point values and integer values. On other machines, maybe the C
runtime library would handle this, essentially replacing cast-
expressions with function calls:

extern int __C_RUNTIME_double_to_int(double);
i = __C_RUNTIME_double_to_int(j);

On other machines, maybe this would simply be a bitwise truncation
of the value of j. It all depends on how the implementation wants
to represent floating-point and integer values.
3)
float f = 4.3;
int i;
i = (int) f;

What happens here ? How does the truncation take place ?


This is the same thing you wrote in #2, except with "double"
replaced by "float." Nothing's different in the explanation.
5)
How are side effects defined ? When do
i say typecasting is an side effect ?


Type-casting is no more a "side effect" than addition or
multiplication. A "side effect" of an operation is something that
*happens*, not something that *is produced*. Examples:
In the expression 2+2, the value 4 *is produced*. Nothing *happens*.
Thus, 4 is the value of the expression, and it has no side effects.
In the expression g=2.0, the value 2.0 is produced. What *happens*
is that 2.0 is assigned to g. Thus, 2.0 is the value of the expression,
and its side effect is to assign 2.0 to g.
In the expression (int)g, the value 2 is produced. Nothing happens.
Thus, 2 is the value of the expression (int)g, and it has no side
effects.
In the expression (a=1,++a), the value 2 is produced. What happens
is that first 1 is assigned to a, and then a is incremented; those are
the side effects of the expression.

You see now? Very simple.

6)
Is truncation a side effect of type casting or not ?


No. When you write (int)43.2, the value of that expression is 43.
It has no side effects. Yes, the value 43 is a "truncated version
of" 43.2, but that's not a side effect; it's just the way C defines
casting from floating-point numbers to integers.

If you have more specific questions about how your own computer
handles floating-point numbers, try asking in comp.programming or in
a forum or newsgroup dedicated to your platform. comp.lang.c is
for questions related to the standard C programming language, as
distinct from any particular implementation or compiler.

HTH,
-Arthur

Nov 14 '05 #3
"Niklaus" <ni*****@gamebox.net> wrote in message
news:d0**************************@posting.google.c om...
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message

news:<Pi***********************************@unix47 .andrew.cmu.edu>...
On Mon, 3 May 2004, Niklaus wrote:
1)
What i would like to know is the about the
internals when a cast is applied ? Say we have

int i = 3;
double j;
j = (double) i;
Actually you don't need a cast in that case because C allows implicit
conversion between arithmetic types. Explicit casts are most commonly used
to convert between pointer types, which on most implementations don't do
anything at the bit level but allow bypassing the translator's type safety
rules.
What happens in the above statement ? Can someone
explain me at bit level or a considerable explantion ?


On the average Wintel machine, this will retrieve the bits stored
in i (say, 0x0300), and pass them through a special function designed
specifically to convert "int bits" to "float bits." On the x86 line
of processors, the FPU has a special machine instruction that will do
this conversion. The result will be a bunch of bits that represent
a float value (say, 0x42DE; I don't know the real internals here).
These "float bits" get stored into the object denoted by j.

Thanks for the explanation. I would like to know the real internals.
May be this isn't the appropriate group. In case someone knows please
do post a follow up.


The only "real internals" to this (at least for x86) are implemented in the
silicon of various processor chips, something the vendors are unlikely to
explain in detail. For other processors, get and read the code for your C
translator and/or runtime library.
3)
float f = 4.3;
int i;
i = (int) f;

What happens here ? How does the truncation take place ?


The overall process is the same as converting an int to a double, with the
exception that the implementation needs to be told whether to truncate, take
the floor, or take the ceiling of fractional parts (since implementations
are usually capable of all three). I believe the exact behavior is
specified in IEEE 754, but I don't have a copy to check.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #4
ni*****@gamebox.net (Niklaus) wrote:
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi***********************************@unix47 .andrew.cmu.edu>...
On Mon, 3 May 2004, Niklaus wrote:

int i = 3;
double j;
j = (double) i;

What happens in the above statement ? Can someone
explain me at bit level or a considerable explantion ?


On the average Wintel machine, this will retrieve the bits stored
in i (say, 0x0300), and pass them through a special function designed
specifically to convert "int bits" to "float bits." On the x86 line
of processors, the FPU has a special machine instruction that will do
this conversion. The result will be a bunch of bits that represent
a float value (say, 0x42DE; I don't know the real internals here).
These "float bits" get stored into the object denoted by j.

Thanks for the explanation. I would like to know the real internals.


The problem is that, from an ISO C point of view, there are no _the_
real internals. What happens at the bit level is different for each
architecture you do this on. All the C Standard defines is what the
result should be (j is now equal to 3.0), not how that result should be
achieved (such-and-such a bit is shuffled into so-and-so a position).
This is because the natural way to get the required result depends
highly on how both floats and ints are represented on the computer in
question.
So yes, you do need to ask this in another group; however, only you know
what systems you want to know this about, so only you can tell whether
you want comp.sys.amiga.something or microsoft.public.vc.something-else.

Richard

[ Oh, btw, snipping is not forbidden in comp.lang.c ;-) ]
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
63
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
3
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have...
7
by: Raghu | last post by:
Hello All, I need some help regarding overloading operation. Is there any way to overload typecasting? I mean if i have a piece of code as below. int a = 2: float b; b = (float)a;
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.