Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 1 month ago.
What does that "f" signify?
const double amplitude = 0.5f;
Question relating to:
3 Answers
10 years, 1 month 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.
Whereas
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 23 Jan 201510 years, 1 month 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.