Can someone provide the theory explanation for this error (floats)

I used a formula like this
float x = 3f;
float y = .2f;
x = 1 - y * .75

I got an error saying basically ´ cannot explicitly convert ´doubles´ into ´floats´…are you sure you didn´t miss a cast.

So it was easy enough to fix, I just put an f behind the 1 and .75

I am just not sure why that error happens.

According to the language specifications a number with a decimal but without an identifier is automatically a double. Automatic conversions exist for converting lower precision types to higher precision types because doing so won’t lose any data but they don’t exist for converting from higher to lower because you can potentially lose data.

Thanks, that makes sense. So it sees that .75 and saves memory by casting it as a double rather than a float.

It isn’t about saving memory. A C# float is 4 bytes and a double is 8 bytes. It’s about precision. Floats have 32-bit precision and doubles have 64-bit precision. However, all video GPUs support floats, whereas support for doubles is not common (and precision can vary) – so Unity uses floats everywhere.

Put an “f” after your constant:

float f = 0.75f;

Refusing to automatically cast a variable from a higher precision double to a lower precision float is not about saving memory but about eliminating a potential runtime error that may be difficult to track down.