Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

5456.367 -> 5456.367188. Is this because float precision in c?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading