# Thread: A doubt about precision in Floats

1. ## A doubt about precision in Floats

Hi,

I ve a nagging question that I have been unable to figure out. From what I understand,
floats have a precision of 6 digits. And this is supposed to be 6 digits in total, not just 6 digits after the decimal point.

So the values that could be stored with precision in a floating point number are as follows,
0.123456
1.23456
12.3456
123.456
1234.56
12345.6 etc.. (i.e.) as the whole number part gets more and more digits, the number of digits that you can store precisely after the decimal point correspondingly decreases. Am I correct in my undestanding till here??

So my issue is, I have an array of floats with values ranging from One's to the thousands. However, when I print out the floats I see that all of the floats are printed out with 6 digits after the decimal point.

For instance a number such as 1000.82 would only be stored with 2 digits of precision right?. But while printing out I get something like 1000.824092. Is this some quirk of the printf statement that it automatically fills in junk numbers to fill up to 6 decimal points? Or is it that any number declared as a float must always have 6 digits after the decimal point?

I also tried rounding off all of the floats to 4 decimal digits. But that seems to have no effect. All floats are always printed out with 6 decimal digits.

If it is only at the printf that this filling out to 6 digits is taking place, I can go ahead and use the floats for further calculations, but if the numbers are stored in the memory itself like this, using them for calculations would mess up a lot of things. Thats why trying to get this cleared out.

thanks,

Avinash

P.S..
I know using 'double' instead would save me a lot of trouble. But I am actually supposed to take this float array and transfer onto Graphics cards for parallel processing. And double precision functions are either not available or incredibly slow on most Graphics cards. Thats why I am trying to see if I can work it out with floats itself..

2. A floating point number can have very many decimal digits. More correctly, its base10 decimal representation can have more than 6 decimal digits. The floating point is a representation different from the normal representation.

The printf just prints the 6 first decimal digits. If a number is 0.099931231 for example it prints 0.099931. The rest the printf assumes are not significant.

Note that you cannot represent 0.01 precisely with a float. It will be something like 0.009993... But that is close enought. It can be a problem if you are not careful and make the float a decimal (with an int), then keep only the first two digits. So you would get 0 instead of .01

If you want 99.999% precise calculations then you should use another format. Something like
Code:
```typedef struct temp
{
int num;
unsigned int dec;
} decimal;```
But a divition is never precise anyway. If you do 10/3 you cannot get a precise number even in decimal. So you always get an approximatation. The above code would just have a far greater one than a "float" or a "double". But it will be slower on its calculation as well.

3. Hi guys,

Thank you, both of you for your answers.
@iMalc: I actually did find this paper. But I guess I was just too intimidated by the size of it and hoped I would get a simpler answer here... Sorry bout that . I ll try to plough my way through it again though.

While I do that, I noticed something that I wanted to run past everyone. I was wrong when I said that rounding off the floats didnt work and everything still printed out 6 digits. Turns out some of the numbers do get rounded off.. But some of them don't.

Code:
```# include <stdio.h>
# include <math.h>

int main()
{
float num1;

num1 = 16.822599; /* or 17.320499, 45.519199, 75.179802 */

/*32.324577 on the other hand gets rounded off as expected */
printf("Num1=%f\n",num1);

num1 = (float)( (int)(num1*10000 + 0.5) )/ 10000;

printf("Num1=%f\n",num1);

return (0);
}```
I ve posted the snippet of test code that I used.. U ll see that with any of the numbers that I ve used, (alternative numbers in comments) the number doesn't get rounded off to 4 digits. But most numbers with 6 decimal places do get rounded off though..

I guess that once I ve read through the literature, I ll see that all of these numbers have something common in their binary representation that doesn't allow them to be rounded off any more than this. Am i right?

So is there any standard method for dealing with such numbers..? Or pointers to reading material that I could go through would be great too..

thanks,

Avinash

Popular pages Recent additions