locked
Convention of declaring float as a whole number RRS feed

  • Question

  • Which is better according to programming conventions

    float var1= 1f

    float var2 =1;

    Monday, April 23, 2012 10:19 AM

Answers

  • In both cases the C# compiler generate the same IL code but strictly speaking the variant with f suffix is "more correct".

    This is because 1, without f suffix, denotes an integer constant. Assigning an integer to a float variable requires a cast and a dumb compiler may very well insert such a cast which leads to larger and slower code.

    @Viral Jain: You can't possible use "float var = 1.0;". That doesn't compile, 1.0 has type double, not float.

    Monday, April 23, 2012 11:01 AM

All replies

  • I use float var = 1.0;

    Viral.


    MCTS - WPF, WinForms, Sql Server 2008

    Monday, April 23, 2012 10:29 AM
  • In both cases the C# compiler generate the same IL code but strictly speaking the variant with f suffix is "more correct".

    This is because 1, without f suffix, denotes an integer constant. Assigning an integer to a float variable requires a cast and a dumb compiler may very well insert such a cast which leads to larger and slower code.

    @Viral Jain: You can't possible use "float var = 1.0;". That doesn't compile, 1.0 has type double, not float.

    Monday, April 23, 2012 11:01 AM
  • I use float var = 1.0;

    Why would you assign a double to a float like that? You want a compile error?

    Monday, April 23, 2012 11:29 AM
  • Watch out assigning ints to floats, especially if you convert them back to ints.

    For example, look at the following code. What do you think it will print out?

    float f = int.MaxValue;
    Console.WriteLine(f);
    
    int i1 = (int) f;
    Console.WriteLine(i1);
    
    int i2 = Convert.ToInt32(f);
    Console.WriteLine(i2);
    

    Monday, April 23, 2012 11:35 AM
  • I think

    float var1 = 1f

    because of : http://msdn.microsoft.com/en-us/library/b1e65aza%28v=vs.80%29.aspx


    Please mark as reply if helped.
    Also visit my blog http://msguy.net/

    Monday, April 23, 2012 12:02 PM
  • In both cases the C# compiler generate the same IL code but strictly speaking the variant with f suffix is "more correct".

    This is because 1, without f suffix, denotes an integer constant. Assigning an integer to a float variable requires a cast and a dumb compiler may very well insert such a cast which leads to larger and slower code.

    @Viral Jain: You can't possible use "float var = 1.0;". That doesn't compile, 1.0 has type double, not float.

    Mike is absolutely right. Here is the IL (VS2010) just in case you may want to see...

    .method private hidebysig static void  Main(string[] args) cil managed
    {
      .entrypoint
      // Code size       8 (0x8)
      .maxstack  1
      .locals init ([0] float32 a)
      IL_0000:  nop
      IL_0001:  ldc.r4     1.
      IL_0006:  stloc.0
      IL_0007:  ret
    } // end of method Program::Main


    Monday, April 23, 2012 12:18 PM
  • Assigning an integer to a float variable requires a cast and a dumb compiler may very well insert such a cast which leads to larger and slower code.


    Such a compiler would disobey the C# specification. Constant expressions must be evaluated at compile-time.
    Monday, April 23, 2012 3:17 PM