delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/05/06/09:30:45

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
Message-ID: <4099F4DD.65F0D691@acm.org>
From: Eric Sosman <esosman AT acm DOT org>
X-Mailer: Mozilla 4.72 [en] (Win95; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: typecast internals
References: <d06ac2eb DOT 0405032358 DOT 6c5035f1 AT posting DOT google DOT com>
Lines: 104
Date: Thu, 06 May 2004 13:15:15 GMT
NNTP-Posting-Host: 12.76.164.21
X-Complaints-To: abuse AT worldnet DOT att DOT net
X-Trace: bgtnsc05-news.ops.worldnet.att.net 1083849315 12.76.164.21 (Thu, 06 May 2004 13:15:15 GMT)
NNTP-Posting-Date: Thu, 06 May 2004 13:15:15 GMT
Organization: AT&T Worldnet
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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.
> 
> 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 ?

    There are many ways to represent the number three.  The
`int' type represents it using a base-2 notation with digits
zero and one: "00000000000000000000000000000011".  A `double'
represents the same value with 64 bits instead of 32 and in
a more complicated pattern (which I'm not going to try to
sort out at the moment).  An ancient Roman would represent
three as "III".  A three-fingered alien from Planet Primrose
might use a base-three notation and represent three as "01"
(Primrosians, as is well known, write the least significant
digit at the left.)

    Conversion from one representation to another is solving
the following problem: Given a value in Roman representation,
what is the Primrosian representation of that same value?
We can construct rules and algorithms to solve the problem,
and when we apply them we find that "IX" corresponds to "02".
Converting an `int' to a `double' is the same kind of task:
Given a value represented as an `int', find the `double'
representation that expresses the same value.  It turns out
that the algorithm to solve this problem is encoded in the
computer hardware: there is an instruction (on some machines,
a sequence of instructions) to perform the conversion.  When
the compiler translates the code you have shown, it emits the
appropriate conversion instruction as part of the executable.

    Incidentally, the cast is not necessary; you would get
exactly the same result with just `j = i;'.

> 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

    It's the same case as the first example, but this time you
begin with a value in `double' representation and look for the
`int' representation of that same value.  Again, the computer
has instructions to perform the conversion -- and again, you
would get exactly the same result with a simple `i = j;'.

> 3)
> 
>                   float f = 4.3;
>                   int i;
>                   i = (int) f;
> 
>     What happens here ? How does the truncation take place ?

    Here we encounter a problem I ignored above.  Some values can be
expressed in one representation but not in another: you cannot
express "1.5" in Roman numerals.  In this case, conversion cannot
deliver the exact same value in the new representation; instead, it
delivers a substitute value, where the C language defines the kind
of substitution that takes place.  When you convert "4.3" from
`float' to `int', the C language defines that the result shall be
four, so you get 000...0100.  It's "an approximation," if you like
to think of it that way.  The CPU instructions that perform the
conversion take care of discarding the non-integer parts.

> 4)
>      Is it similar to how int got promoted to double in the question 2.

    Yes.  The language defines a correspondence between the values of
the different types, and when the conversion cannot be exact it
specifies how the resulting approximation is chosen.  Incidentally,
there are some `int' values that have no exact representation as
`float': conversion in either direction can involve approximation.

> 5)
>      How are side effects defined ? When do
>      i say typecasting is an side  effect ?

    Never.  A conversion (which is what a cast operator
performs) is not a side effect.

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

    Truncation is not a side effect.

-- 
Eric Sosman
esosman AT acm DOT org

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019