- Home /
Why do I need the "f" when using a float?
The variable "speed", near the bottom when I subtract .5 from it it requires that "f", why is that? It was already declared a float in the beginning of the code.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
float speed = 0;
float max_speed = 5;
float accel = 1;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey (KeyCode.W))
{
if (speed < max_speed) { speed += accel * Time.deltaTime; } else { speed = max_speed; }
transform.Translate (0, 0, speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.S))
{
transform.Translate (0, 0, -3 * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A))
{
transform.Translate (-3 * Time.deltaTime,0 , 0);
}
if (Input.GetKey (KeyCode.D))
{
transform.Translate (3 * Time.deltaTime,0 , 0);
}
if (speed > 0) { speed -= .5f * Time.deltaTime; }
}
}
Answer by whydoidoit · Sep 05, 2013 at 05:04 AM
You need it because in C# .5 is a double not a float and the compiler will complain that you are losing precision. .5f indicates a float and so it is the same type.
Just in addition - Here is a table for all value types and its suffix: http://msdn.microsoft.com/en-us/library/bfft1t3c.aspx
So is there a point in declaring it a float then? It seems redundant to declare it one type and declare it again later on. I apologize, I only have experience in C++. And do I need to use that for any decimal number then?
The compiler accepts speed as a float, because you declared it as one. The problem is that .5 and .5f represent different types, which take up different amounts of memory (thus offering different precision). If you mix and match these types the compiler wants to make sure you know what you're doing by giving you a warning.
The float literal is also used in C++ to explicitly state a number to be a float.
You have to do it for every hard-coded decimal number you want to be considered a float. An alternative (which I've never tried) is to use the more cumbersome (Float).5;.
This issue doesn't occur because the compiler doesn't know the type of speed - it knows that speed is a float. The point here is that you try to assign a double to a float and an implicit cast is not possible in this case. Some examples where speed is declared as float:
float myFloat = speed / 5; //float devided by an int will return a float
float myFloat = speed / 0.5; //float devided by a double will return a double - that will be an issue
float myFloat = speed / 0.5f; //float devided by a float will return a float
I see what your saying. I have a square container (float) and I am trying to fit a round peg into it (double). Thanks for the help everyone!
Answer by Nexum1 · Sep 05, 2013 at 02:06 PM
Your declaration that the variable speed is of type float. It assigns the value 0.5 to this variable.
According to the C# specification, a number containing a decimal point that doesn't have a suffix is interpreted as a double.
So we now have a double value that we want to assign to a variable of type float. In order to do this, there must be an implicit conversion from double to float. There is no such conversion, because you may (and in this case do) lose information in the conversion.
The reason is that the value used by the compiler isn't really 0.5, but the floating-point value closest to 0.5, which is 0.4999999999999978655962351581366... for double and exactly 0.499999946057796478271484375 for float.
Strictly speaking, the f is not required. You can avoid having to use the f suffix by casting the value to a float:
float speed= (float)0.5;
Your answer
Follow this Question
Related Questions
Scaled Variables 2 Answers
HOW TO ADD AND REMOVE VARIABLES ALREADY DECLARED IN A LIST 1 Answer
How do I add something on to a float. 1 Answer
random respawn and respawn delay 1 Answer