Yes, you should use the f
suffix. Reasons include:
-
Performance. When you write
float foo(float x) { return x*3.14; }
, you force the compiler to emit code that converts x to double, then does the multiplication, then converts the result back to single. If you add thef
suffix, then both conversions are eliminated. On many platforms, each those conversions are about as expensive as the multiplication itself. -
Performance (continued). There are platforms (most cellphones, for example), on which double-precision arithmetic is dramatically slower than single-precision. Even ignoring the conversion overhead (covered in 1.), every time you force a computation to be evaluated in double, you slow your program down. This is not just a “theoretical” issue.
-
Reduce your exposure to bugs. Consider the example
float x = 1.2; if (x == 1.2) // something;
Issomething
executed? No, it is not, because x holds1.2
rounded to afloat
, but is being compared to the double-precision value1.2
. The two are not equal.