I have a program in C like this:
#include <stdio.h>
int main() {
float number = 0;
scanf("%f", &number);
printf("%f\n", number);
}
And the output is:
ururay@note:~/workspace/c/float-precision$ gcc float-precision.c -o float-precision
ururay@note:~/workspace/c/float-precision$ ./float-precision
5456.367
5456.367188
I have executed this program several times and the output is the same. Is the '188' "appended" to the end of the number because float precision? If so, how could I identify this in binary representation?
CodePudding user response:
The number 5456.367
cannot be represented exactly in the IEEE-754 single-precision floating point format.
The closest number is 5456.3671875
, which has the binary representation 01000101101010101000001011110000
. The next smaller representable number is 5456.3666992
, which has the binary representation 01000101101010101000001011101111
.
CodePudding user response:
Yes if you want a better precision try using double
or long double
(but long double
is way slower than other type or refer to the same type as double
).
Also printf only takes double
and long double
go look at the spec of printf