Re: 12.34f vs (float) 12.34

From: Stu Smith (stuarts_at_remove.digita.com)
Date: 02/12/04


Date: Thu, 12 Feb 2004 11:54:14 -0000


"Jon Shemitz" <jon@midnightbeach.com> wrote in message
news:402A8C48.26F41DF7@midnightbeach.com...
> Stu Smith wrote:
>
> > HOW: it's called constant folding,
>
> Yes, thanks. Point of my question, though, is that I can imagine a few
> ways that this might be done:
>
> * Very straightforwardly, whereby "(float) 1.23" constructs a double
> value 1.23, then casts it to a single.
>
> * More convolutedly, whereby the constant folding operation sees the
> (float) cast being applied to an implicitly typed double, and goes
> back to the literal and reparses it as 1.23f.
>
> * Complex parsing, where cast followed by a literal parses the literal
> exactly as if it were 1.23f.

As far as I'm aware (and we're at the limits of my knowledge here), constant
folding is done at compile time, so option one is out of the question. I
believe the compiler makes passes over the statement/expression trees it has
generated, looking for certain patterns, and replacing them with
equivalents. I guess one pattern could be something like "(builtin value
type) constant -> new constant".

>
> The point being that in some cases the first approach might yield
> slightly different values than the first.
>
> > WHETHER: in theory I think they could in your example (but that's just
my
> > interpretation); section 11.1.3 of the CLI docs states:
>
> I'm not sure that ECMA-335 has much to say about the workings of the
> compiler ....

Don't know about that, but I do know that if you have a variable of type
float it's not guaranteed to be no more than a float.

>
> --
>
> programmer, author http://www.midnightbeach.com
> and father http://www.midnightbeach.com/hs



Relevant Pages

  • Re: Richard heathfields casting operation
    ... in this book there is a section named "How and why to cast" ... if you say 3.0 it is a type disambiguation,and the compiler ... If you need a float with the value 3.0f, just say 3.0f - there is no need ... I manually did the calculation for one value by using calculator ...
    (comp.lang.c)
  • 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)
  • Re: Casts
    ... a cast *specifies* a conversion. ... result is errors that aren't diagnosed by the compiler. ... Very bad example as the float will immediately be promoted back up to a double before being passed to the variadic function. ...
    (comp.lang.c)
  • Re: Possible contradiction between JLS and Suns Java compiler
    ... which seems to contradict what the JLS claims. ... The compiler won't accept it without a cast of c to float. ...
    (comp.lang.java.programmer)
  • Re: problem with memcpy and pointers/arrays confusion - again
    ... this second method is known as an explicit conversion, or cast. ... The cast, in effect, tells the compiler: ... the malloc function. ... function taking a size_t as a parameter and returning a void pointer (i.e. ...
    (comp.lang.c)