Re: internals of typecasting
From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 05/04/04
- Next message: Arthur J. O'Dwyer: "Re: Q: Newbee: Why is this little program not working"
- Previous message: Alan Balmer: "Re: Expert C programming by Peter Vander Tiden/Liden"
- In reply to: Niklaus: "internals of typecasting"
- Next in thread: Niklaus: "Re: internals of typecasting"
- Reply: Niklaus: "Re: internals of typecasting"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 3 May 2004 18:43:02 -0400 (EDT)
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
- Next message: Arthur J. O'Dwyer: "Re: Q: Newbee: Why is this little program not working"
- Previous message: Alan Balmer: "Re: Expert C programming by Peter Vander Tiden/Liden"
- In reply to: Niklaus: "internals of typecasting"
- Next in thread: Niklaus: "Re: internals of typecasting"
- Reply: Niklaus: "Re: internals of typecasting"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|