Re: internals of typecasting

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 05/04/04


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



Relevant Pages

  • Re: integer division
    ... (float)(dy/dx), the results of ... are calculated BEFORE the cast to ... float, and since dx and dy are integers, that division is done as an integer ... division because at least one of its parts is floating-point. ...
    (comp.lang.cpp)
  • Re: RosAsm is a broken pile of crap
    ... If there's no type information stored (ie, MyPoint is just a label), I guess the assembler would interpret "10" as a DWORD and "10.0" as a floating-point variable? ... feed it a DWORD and it could treat it as a FLOAT, ... When programming assembler, the programmer must know what her data are ment to represent. ...
    (alt.lang.asm)
  • Re: problem incrementing my variable
    ... Also note that when floating-point values of type float are passed as trailing ... The digits with the bar over them repeat over and over, indefinitely, ... decimal representation, it simply truncates the above sequence of repeating, ...
    (comp.lang.c)
  • Re: Decimal vs float
    ... >>> the odds are that it cannot be represented precisely as a float. ... I concur and I wonder why CAS like e.g. Maple that represent floating ... Maple hardware floating-point numbers can only exist ...
    (comp.lang.python)
  • Re: Problems With strtof()
    ... >I then tried to convert it into a float and store it in another variable. ... The cast is unnecessary but also not harmful. ... really have a C99 compiler or is this a typo? ...
    (comp.lang.c)