Re: Richard heathfields casting operation



On Nov 7, 7:05 am, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
sophia.ag...@xxxxxxxxx said:

Hi ,

I was going through peter van der linden's book Expert C programming,
in this book there is a section named "How and why to cast"

the author then says as follows

(float) 3 - it's a type conversion and the actual bits change.
if you say (float) 3.0 it is a type disambiguation,and the compiler
can plant the correct bits in the first place.some people say that
casts are so named because they help something broken to limp along.

In my edition, it says (double) 3 and (double) 3.0 - he's right about the
conversion but wrong about the disambiguation, since 3.0 is of type
double, so casting it to double doesn't disambiguate anything, as there is
no ambiguity to disambiguate.

what exactly is this type disambiguation...????

Absent.

i have n't seen any casts like (float) 3.0; in use (may be due to my
limited experience). how will it help the compiler to plant the
correct bits????

If you need a float with the value 3.0f, just say 3.0f - there is no need
to cast.



Then the author says that as an impratical example ,you can create a
pointer to ,for example printf(), with

extern int printf(const char*,...);
voif *f = (void *) printf;

Whilst this is a common extension, the Standard does not guarantee that you
can store a function pointer in a void * without losing information.
Better to do this (in which case no cast is required):

int (*f)(const char *, ...) = printf;



you can then call printf through a properly cast pointer, in this
manner:

(*(int(*)(const char*,...))f)("Bite my shorts. Also my chars and ints
\n");

Not guaranteed as written. But with my suggested change, you don't need to
cast. You just do either (*f)("Bite...\n"); or even simply f("Bite...\n");
at your discretion.



now void *f = (void *) printf; why this cast is required....????
^^^^^^

The code is broken. Don't use it.

why the author says that above example is impractical...???

Perhaps because the code is broken.

why is it because library functions are not meant to be called via
void pointer.....?????

Because the Standard doesn't guarantee that it's possible.

for explanation of above issues i went to Richard heathfields site:
http://www.cpax.org.uk/prg/writings/casting.php

there i found nothing mentioned about type disambiguation..

Right. There's no need for it. Casting is not about disambiguation.

but i found more complex example such as this

(int)((int (*)(const char *, ...))
printf((const char *)"%3.0f %6.1f\n",
(float)fahr, (float)celsius));

what exactly does this statement does...???

It's a kind of picture. Some people claim that casts "clarify" code. This
picture takes them at their word. Which do you think is clearer - the
above, or printf("%3.0f %6.1f\n", fahr, celsius))? I know which one I'd
choose.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

i compiled and executed your program:-

#include <stdio.h>
main()
{
float fahr, celsius;
int lower, upper, step;

lower = (int)0; /* lower limit of temperature table */
upper = (int)300; /* upper limit */
step = (int)20; /* step size */

fahr = (float)lower;
while((float)fahr <= (int)upper) {
celsius = (((float)
((float)5.0/(float)9.0)) *
(float)((float)
(((float)fahr-(float)32.0))));

(int)((int (*)(const char *, ...))
printf((const char *)"%3.0f %6.1f\n",
(float)fahr, (float)celsius));

fahr = (float)((float)fahr + (float)step);
}

gcc -o casting casting.c
../casting

i got the following result:-

0 -17.8
20 -6.7
40 4.4
60 15.6
80 26.7
100 37.8
120 48.9
140 60.0
160 71.1
180 82.2
200 93.3
220 104.4
240 115.6
260 126.7
280 137.8
300 148.9

I manually did the calculation for one value by using calculator
for your 220 104.4 values calculator o/p is
219.92 104.4
(calculation done for fahr value based on celsius value 104.4)
so your program is not accurate by a value of .08

.



Relevant Pages

  • Re: 12.34f vs (float) 12.34
    ... where cast followed by a literal parses the literal ... believe the compiler makes passes over the statement/expression trees it has ... generated, looking for certain patterns, and replacing them with ... float it's not guaranteed to be no more than a float. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Strange BUG in teh Framework?
    ... and because of the calculation I am using, ... some return "strange" values when cast from single to double. ... it returns a float. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Strange BUG in teh Framework?
    ... and because of the calculation I am using, ... some return "strange" values when cast from single to double. ... it returns a float. ...
    (microsoft.public.dotnet.framework)
  • Re: Strange BUG in teh Framework?
    ... and because of the calculation I am using, ... some return "strange" values when cast from single to double. ... it returns a float. ...
    (microsoft.public.dotnet.general)
  • Re: 12.34f vs (float) 12.34
    ... The compiler will optimize the cast away assuming it can. ... private static void Blah() ... > In principle, at least, the latter is a double constant being cast to> a float; while the two both generate actual constants, does the latter> ACTUALLY do a conversion at compile time? ...
    (microsoft.public.dotnet.languages.csharp)