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?
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.
When you multiply or divide two numbers and if one of the number is a floating point then you will get a floating point number. So, 1.0 * a will give you a floating number and that will be 10.0.
And the reason the comparison succeeds with 1.5 is that 1.5 can be represented exactly as a float and as a double ; it has a bunch of zeros in its low bits, so when the promotion adds zeros the result is the same as the double representation.
A float has 7 decimal digits of precision and occupies 32 bits . A double is a 64-bit IEEE 754 double-precision floating-point number. 1 bit for the sign, 11 bits for the exponent, and 52 bits for the value. A double has 15 decimal digits of precision and occupies a total of 64 bits .
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
.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With