Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the value of this float change from what it was set to?

Why is this C program giving the "wrong" output?

#include<stdio.h>

void main()
{
    float f = 12345.054321;

    printf("%f", f);

    getch();
}

Output:

12345.054688

But the output should be, 12345.054321.

I am using VC++ in VS2008.

like image 588
user366312 Avatar asked May 11 '10 06:05

user366312


1 Answers

It's all to do with precision. Your number cannot be stored accurately in a float.

like image 178
zaf Avatar answered Sep 21 '22 19:09

zaf