Could you explain a little further, I don't understand the terms.
Does it transform the calculation to a float?
What does single precision mean?
What is the reason for the .0f, why not just .f?
And most importantly, why would I want to do this?
1. It means the calculation will perform a floating-point division and that you'll get a float out of it. So yes.
2. Single-precision means the floating point integer is 4 bytes (32 bits) long. This is opposed to double-precision (8 bytes or 64 bits).
3. Verbosity to make the distinction clearer, I guess? As Duoas said, just 1000f would have been enough.
4. Because without the f, C++ compilers will assume the number there is an integer, and you'll wind up doing integer divisions. This is not something that you'd always want to do.
There are two separate reasons why it is done this way. The first is to avoid doing integer division.
Example: 3/4 , both 3 and 4 are integers. An integer divide keeps the whole number part and discards any fractional part, giving 3/4 = 0
Example: 3/4.0 , 3 is of type int, 4.0 is of type double. The compiler will in this case do a floating point divide giving the result of 0.75 with a type of double.
The reason for adding the f suffix is to override the default type (double) and force the value to be of type float. Usually this would be done for consistency with the context, for example a function might expect a parameter of type float, so that is the type chosen.
32.0f is exactly the same as 32.f. It's just a little easier to read that it's not an integer with the 0 there.
Not sure what your competency is so I'll start with the basics: intfloatdouble are all primitive types. int is a 32-bit integer, but doesn't support fractions. float is a 32-bit floating point number. That means it handles decimals, it contains a number (mantissa) and an exponent. It's effectively binary scientific notation. double is a 64-bit floating point number. It's the same as float but takes more space.
When we type a number in C++ without a container, it's called a literal. Typing 123 specifies an integer. 0x123 is an integer in hex. 0123 is an integer in octal. 123.0f or 123.f or 123f is a float. 123.0or 123. is a double.
In the line you've asked about, we've had to explicitly state that this number is a float because otherwise we'll run into integer division. When you have an integer divided by another integer, the fraction is lost. Therefore 2/5 is 2, not 2.5. We use the modulus operator (%) to retrieve the remainder in that case. When we state that the demonimator is a float, the we are ensuring that the output is not an integer and the decimal is not discarded.
So if I understand this right, we divide with 1000.0f to not have an integer division? Then my question is, why are we multiplying with 32.0f? Can we not just multiply with 32, does it have to be a float?
Thanks for the help!
The multiplier could be an integer, but since the left operand of multiplication is already a float and would induce a conversion to float anyway, it is more consistent to have a float on the right side from start.
Why does it have to be an integer when it could be a float? You could do either one really. keskiverto gave one reason. Another reason is that if you should ever decide to change this line a little and remove the 1000.0f, then you might forget to make the 32 into a 32.0f and induce a bug. Getting into the habit of using floats for division will make these types of tough-to-find bugs less frequent.
You'll spend less time debugging and we all like that.