Re: 12.34f vs (float) 12.34

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Klaus H. Probst (usenet001_at_vbbox.com)
Date: 02/11/04


Date: Wed, 11 Feb 2004 00:48:33 -0700

No. The compiler will optimize the cast away assuming it can. It's probably
generating something like this:

private static void Blah()
.locals (float V_0, float V_1)
L_0000: ldc.r4 1.23
L_0001: stloc.0
L_0002: ldc.r4 4.56
L_0003: stloc.1

from this:

static void Blah()
{
    float f1 = 1.23F;
    float f2 = (float) 4.56;
}

-- 
____________________
Klaus H. Probst, MVP
   http://www.vbbox.com/
"Jon Shemitz" <jon@midnightbeach.com> wrote in message
news:4029D05A.2725B798@midnightbeach.com...
> Is there a difference between a constant like "12.34f" and "(float)
> 12.34"?
>
> 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? That is, are there constants
> where
>
>   <constant>f != (float) <constant>
>
> ?
>
> -- 
>
> programmer, author  http://www.midnightbeach.com
> and father          http://www.midnightbeach.com/hs


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: 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: 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)