4

When creating float variables, integers don't need a type suffix, i.e. all of these are valid:

public float distance = 3;
public float distance = 3f;
public float distance = 0.3f;

Is there a reason to use 3f instead of 3? Is the compiler smart enough to recognize the type as float and automatically cast it?

Related Questions: Why Should we use literals in C# and Why is the "f" required when declaring floats

Both explain why we need to use the type suffix 'f', but is there a 'correct' way to declare floats that have integer values? Is one way more efficient?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Xander Luciano
  • 3,753
  • 7
  • 32
  • 53

4 Answers4

3

Here is the list of allowed Implicit Numeric Conversions. Implicit conversions are allowed when the target type can hold the original type range. In the case, float distance = 0.3; is an error because a float range cannot accommodate the double range.

As to efficiency, between 3 and 3f, the compiler should optimize for you.

IL_0001:  ldc.r4     3.   // float distance1 = 3;
IL_0006:  stloc.0
IL_0007:  ldc.r4     3.   // float distance2 = 3f;
IL_000c:  stloc.1
Decima
  • 116
  • 1
  • 2
  • Oh now that's interesting and kind of what I was interested in knowing, was after it's compiled, does it make a real difference or are they the same, and it looks here that the compiler is smart enough to make them the same, so ultimately it's preference and readability. Thanks! – Xander Luciano Feb 12 '16 at 21:09
  • 1
    FYI, you can use the [Ildasm.exe](https://msdn.microsoft.com/en-us/library/f7dy01k1(v=vs.110).aspx) tool to view the IL. – Decima Feb 12 '16 at 21:18
1

The need for the f suffix becomes obvious in your 3rd example:

public float distance = 0.3f;

If you remove the f, the 0.3 will be interpreted as a double and will generate a compilation error, because a double can't be downcast implicitly and assigned to a float variable. You need to specify that 0.3 is a float literal in that case.

sstan
  • 35,425
  • 6
  • 48
  • 66
  • Correct, but if it is an integer, I don't need to explicitly declare it as a float with `f`, but is there a reason *to* declare it as a float regardless of the value? i.e. should I always add `f` or is it all just preference? – Xander Luciano Feb 12 '16 at 20:50
  • So say I have a function that accepts three floats `void Position(float x, float y, float z)` and I pass it integers `Position(2,5,18)`, would it be better practice to use the type suffix? `Position(2f,5f,18f)` or is there very little impact? – Xander Luciano Feb 12 '16 at 20:58
  • I would get in the habit of using the suffix if I really need a `float`, because normally, I would expect that most values will have a decimal portion, in which case, the suffix is required. If you always find yourself passing integer values, one has to wonder why you are using the `float` data type in the first place. – sstan Feb 12 '16 at 21:00
  • Makes sense, the only other case is: `distance *= 2f;`, where I still need the type to be float, but I am multiplying by a constant integer. Either way makes little difference, but was just curious if there was an 'unwritten rule' about using type suffixes. – Xander Luciano Feb 12 '16 at 21:04
1

Pragmatics. The f is not required. However, note that a decimal point number without the "f" suffix is interpreted as a double, a more precise but memory-expensive primitive. Consider the following:

public float distance = 3.123456789123456;

The right-hand of this assignment is interpreted first, and without the "f" connotation, it is perceived as a double. Compiler error would occur here. Because floats are less precise (holding 7 decimal places vs a double's 15), you would lose accuracy, and the variable "distance" would now be something more somewhere around 3.123457.

Omnus Ruthius
  • 120
  • 2
  • 12
  • Ok so it really it's just preference as to whether you should add the `f` with integers or not? And to expand on your point about precision, if you were add the f `public float distance = 3.123456789123456f;` then print that out distance, the result would be `3.123456` due to the accuracy of the float type. – Xander Luciano Feb 12 '16 at 20:54
  • 1
    Xander- Correct. What it really comes down to in regards to integers is convenience for yourself and other developers working with you later on. When you have a complex project with multiple variables, sometimes it's just nice to see: public float distance = 3.0f; instead of public float distance = 3; And yes, public float distance = 3.123456789123456f; would only keep track of 7 decimal places. – Omnus Ruthius Feb 12 '16 at 21:04
0

If you write float dist = 3; it can be without f. But in you assign object dist = 3f; then you need f because otherwise it will be defined as integer.

i486
  • 6,491
  • 4
  • 24
  • 41