4 years, 4 months ago.

What does that "f" signify?

const double amplitude = 0.5f;

Question relating to:

Comment on this question

3 Answers

3 years, 2 months ago.

f means float.It's like a C programming language.

4 years, 4 months ago.

f = float

In c a value of 1 is an integer and 1.0 is a double, you use f after a decimal number to indicate that the compiler should treat it as a single precision floating point number.

e.g.: If you have a line

float myValue = 0.3*someFloat;

then you are multiplying a double by a float which means the float is converted to a double, the two are multiplied and the result is converted back to a float.


float myValue = 0.3f*someFloat;

would mean all the calculations are done as floats. This can have significant performance implications if the compiler follows the rules strictly.

The example line you give is somewhat silly however since:

const double amplitude = 0.5f;

is creating a float of value 0.5 and then expanding it to be a double. In that situation the f is completely pointless.

Looking at that example code it also has one of the other common errors:

const double offset = 65535/2;

offset will have a value of 32767 not the 32767.5 that is intended. int/int will result in an int which is then converted to a double. To get the correct result it should be 65535/2.0;

posted by Andy A 23 Jan 2015
4 years, 4 months ago.

It means 0.5 is defined as float, and not as double. Generally it won't really matter for your average program if there arent high demands on calculations, but this way you also have it clearly as float.

Why instantiate a double with a float? Makes no sense, clearly a (small) error. There is also no realistic reason why in such an example a double would be required over a float.

To post an answer, please log in.