What's that "f" near a number?

sorry for the topic, i don’t now how to write it more clearly…
question is simple:

var Speed :int = 10;

OR

var Speed:int=10f;

what is the difference?

I have my player with max speed 10 (using standard motor script)
and my enemy ( a rabbit ) with max speed of 9.8
and it is moving by
Vector3.MoveTowards(transform.position, new_checkpoint, Time.deltaTime * 9.8);

but the rabbit is really more fast than me … what’s wrong, in your opinion?

do you know, where i must work to make the rabbit move A LITTLE less faster than me?

var Speed : int=10f;

So far I know this ^^^ won’t work. It should be

var Speed : float = 10f;

or

var Speed : double = 10f;

So as “F” stands for float, and float are numbers behind the point or comma, depends where you live.
If you want your rabbit to move faster, you change “10f;” to “11f;” and so on.

Thats ok,
thankyou

But even if alla speeds are integer why my rabbit is still much more faster than me
if my speed is 11 and his speed is 10?

i tryed even with rabbit speed at 10.98, but it his faster than me!
To make the rabbit runs at my speed i must set his speed to 5, or 6 …

Maybe the motorscript calculate speed in a way that i don’t know?

do you have a clue?

f means float and it’s only used if you’re assigning a value to a float.

1 Like

var gravity : float = 9.8f actually consists of two operations. First the literal value 9.8 is created, and then it is assigned to the variable. Problem is any floating point literal value is created as a double by default, and then guess what it won’t fit into a float (it doesn’t matter how big the value is, it’s all about how many bytes of storage). So to persuade the compiler to actually create the literal as a float you put the f after it, and then it can be assigned to the variable.

the reason for the “f” is to donate its “type”. the poblem is that there are multiple types of floating point numbers.
Single or often called float is a floating point number with 32bit
Double is 64 bit and
Decimal is 128 bit
to distinguis between those types there have a sufix
float = f
double = d
decimal = m
if you write a floating point numberr in code with out the suffix it is assumed to be of type double at least in c#

2 Likes

Just to clarify the last comment by @ - decimal is not a floating-point type - it’s a 128bit precise decimal.

1 Like

No, it depends on the language. This topic seems to talking about Unityscript, in which case floating point literals are created as floats by default. In order to use doubles, you need to specify “d”, as in “var foo = 123.123d”. C# is the opposite; double is the default and you need to add “f” to get floats. Using “f” in Unityscript doesn’t actually do anything; 1.23 and 1.23f are the same thing.

–Eric

1 Like

Thanks for that clarification Eric - my background is Java and I use C# in Unity. Guess my example wasn’t that apropos then, being UnityScript syntax.