While studying about video compression encoder I came across float rateRatio=1.0f. I want know where does it make difference I mean 1.0f and 1.0?
-
1possible duplicate of [Difference between 2.0 and 2.0f (explicit float vs double literals)](http://stackoverflow.com/questions/3696902/difference-between-2-0-and-2-0f-explicit-float-vs-double-literals) – Wooble Aug 20 '14 at 11:22
-
it makes a difference in expressions not involving double types: the 1.0f is of type float so it calculates the expression using floats instead of doubles, which is potentially faster – programmerjake Aug 20 '14 at 11:23
-
1In `float rateRatio=1.0f` the suffix is useless, because the value is assigned to `float`. – Sergey Kalinichenko Aug 20 '14 at 11:26
-
3It's not a duplicate, the question does not ask the difference between `1.0f` and `1.0` but where does it make a difference. See my answer. – ouah Aug 20 '14 at 11:31
-
1Related to [Is 'float a = 3.0;' a correct statement?](http://stackoverflow.com/q/25229832/1708801) in my [answer](http://stackoverflow.com/a/25231230/1708801) I cover some interesting cases. – Shafik Yaghmour Aug 20 '14 at 11:41
3 Answers
As other said, one literal is of type float
and the other is of type double
.
Here is an example where it makes a difference:
#include <stdio.h>
int main(void)
{
int a = 16777217 * 1.0f;
int b = 16777217 * 1.0;
printf("%d %d\n", a, b);
}
prints on my machine:
16777216 16777217
The expression 16777217 * 1.0f
is of type float
and 16777217
cannot be represented exactly in a float
(in IEEE-754) while it can be represented exactly in a double
.

- 142,963
- 15
- 272
- 331
One is a double
the other is a float
:
double x = 0.0; // denotes a double
float y = 0.0f; // denotes a float
It depends on the system but e.g. on Windows you'll find that float
has 32bit of precision whereas double
has 64bit. This can make a tremendous difference when it comes to precise or numericable unstable calculations.

- 23,898
- 50
- 191
- 378
-
@PRAVINPAWAR ther's an implicit conversion between `double` and `float`, but if the compiler can't compute it at compile-time it will be a runtime cast. – Quentin Aug 20 '14 at 11:26
-
can we not write float y=0.0
From your comment, I see where the confusion stems from. It's not the data type of the variable assigned to but the data type of literal constant (0.0, 1.0f, 1.0, etc.) itself that matters here. When you write
float f = 1.0;
1.0
a literal of type double
while f
is a float
, hence the compiler does an implicit narrowing conversion to float
, the same holds true for double d = 1.0f
where it's widening implicit conversion from float
to double
.
Implicit conversion rules are the reason 16777217 * 1.0f
expression (in ouah's answer) becomes a float
, since 1.0f
is a float
and in an expression with both float
and int
the resulting type is dictated by the standard as a float
, thus both are converted to float
s, but the resulting value isn't representable as a float
and thus you see a different value.
Instead when 1.0f
is changed into 1.0
it becomes a double
and thus 16777217 * 1.0
expression becomes a double
(again because the standard dictates that in an expression with double and any other integral type, the result is a double
) which is large enough to hold the value 16777217
.