# precision - making sure I understand this correctly.

• 03-25-2007
hamsteroid
precision - making sure I understand this correctly.
Hi all,

Just thought I'd discuss some basics on a nice sunny morning. :)

When we are dealing with precision - in this case float - when it is said that float has 6 decimal digits precision - does that include the digits before the decimal point? I had assume d wrongly that it referred only to the digits after the decimal point. I am not using double here - examining float just right now.

eg. this circle example highlights this. In the printf control strings I display to 6 places.

Code:

```  float radius = 0.0f;                  /* radius of table */   float diameter = 0.0f;   float circumference = 0.0f;   float area = 0.0f;   float pi = 3.14159265f;     printf("\nInput the diameter of the table: ");   scanf("%f", &diameter);     radius = diameter/2.0f;   circumference = 2.0f*pi*radius;        /* circumference = 2πr */   area=pi*radius*radius;                /* area = π^r2        */     printf("\nThe circumference is %.6f", circumference);   printf("\nThe radius is %.6f", radius);   printf("\nThe area is %.6f\n", area);   return 0;```
run1
Input the diameter of the table: 1.1

The circumference is 3.455752
The area is 0.950332 /* on my calculator it is 0.9503317 */
hamster@ganymede:~/it/c/lessons\$ ./2.8_circle

run2
Input the diameter of the table: 11

The circumference is 34.557522
The area is 95.033180 /* on my calculator it is 0.95033176 */
hamster@ganymede:~/it/c/lessons\$

In run1, the area is only accurate to 5 places after the decimal - so 0 before the decimal is part of 6 digits of precision.
In run2, the area is only accurate to 4 places after the decimal - so 95 before the decimal is part of 6 digits of precision.

I am basing these precisions on my compiler/machine version. So, having a large number before the decimal (eg 1024.4567) would drastically reduce the accuracy. Using format %.6f only would be good for floats like 0.xxxxx for example and 1024.xxyyyy would be inaccurate after 2 places beyond the decimal.
• 03-25-2007
Salem
No, it refers to the number of digits.

Like so
123456.0
123.456
0.123456

Floats have a fixed number of bits (23 usually) to represent the mantissa (the number itself), and a further (8 usually) bits to represent the exponent (roughly where the decimal point goes).
Since you need just over 3 binary bits to represent each decimal digit, this gives you the 6 digit precision.
• 03-25-2007
hamsteroid
Quote:

Originally Posted by Salem
No, it refers to the number of digits.

Like so
123456.0
123.456
0.123456

Floats have a fixed number of bits (23 usually) to represent the mantissa (the number itself), and a further (8 usually) bits to represent the exponent (roughly where the decimal point goes).
Since you need just over 3 binary bits to represent each decimal digit, this gives you the 6 digit precision.

Thanks Salem, just to add to that, you are saying that 0.123456 is counted as 6 digits - ie, the zero is not counted as long as it's zero before the decimal? (similarly 123456.0). Sorry for the confusion I was counting the digits. :)
• 03-25-2007
Salem
Imagine all floats as
000000000000123456xxxxxxxxxxxxxxxxx
Somewhere in there is the decimal point, whether it's 1.23 or 1.23E10 or 1.23E-10

Anything to the left of the most significant digit is always 0 (and not stored anywhere).
Anything to the right of the 6th digit is "decreasingly inaccurate" (more inaccurate, but less significant).

So the 7th digit is probably right (but might not be), and the rest have a 1:10 chance of being right (just chaotic noise in other words).
To be exact, you need log(10)/log(2) bits for each decimal digit. Given 23 bits (in a float mantissa), you get 6.9236899 decimal digits of precision.
• 03-25-2007
hamsteroid
Yes I got it! That's a pretty good answer. Thank you! :)