Floats, doubles and the "f" after numbers

Hi all.
So I’m a C++ programmer trying to adapt to Unity’s javascript.
In C++, the number 1.5 would be a double while 1.5f would be a float.
Unity’s javascript does seem to allow the “f” after the number, does it still work the same?

eg.
if I do:

var myVar = 1.5;
var myOtherVar = 1.5f;

will the dynamic type casting make the first a double and the second a float?

The scripts I’ve seen in the demos don’t seem to put the “f” after the variables and I’m wondering if it does nothing and that’s why people are leaving it off or if people are just being inefficient.

Unityscript implements numbers like “1.5” as floats. “1.5” and “1.5f” are the same. “1.5d” is a double. This is probably because you don’t really use doubles in Unity, everything in the engine is floats. (You might of course need doubles in certain specialized cases in your own code.)

By the way, if you ever have questions about the type of a variable, use “typeof”.

var myVar = 1.5;
print (typeof(myVar));

Also, that’s not “dynamic type casting”. It’s type inference, which is a major difference. Type inference assigns the type of a variable at compile time based on the value. Dynamic typing means that the variable doesn’t inherently have a type at all, and that it’s figured at run-time, which is much slower. This is dynamic typing:

var myVar;
myVar = 5;
myVar = "aaa";

That works, but is slow and potentially error-prone. So don’t do it. Using type inference, however, is no different (from a performance perspective) than assigning the type. If you try the above with type inferencing, it won’t work:

var myVar = 5;
myVar = "aaa"; // compile error!

These two lines of code are 100% functionally identical:

var myVar = 5;
var myVar : int = 5;

This is, by the way, a C# feature. “var myVar = 5;” is valid C# code and works exactly the same as it does in Unityscript. Like any tool, type inferencing can be useful, or it can be abused. If there’s any question at all about what the type is, explicitly specify it, otherwise it’s fine not to. e.g.:

var myVar : Vector3 = Vector3.zero;
// Obviously myVar is a Vector3, writing it out twice just adds noise
var myVar = Vector3.zero; 
// Same info, more readable

var myVar = SomeRandomFunction(4, 77.5, "q");
// What the heck does SomeRandomFunction return and what does that make myVar?
var myVar : int = SomeRandomFunction(4, 77.5, "q");
// OK, that's clear now