precision - making sure I understand this correctly.

This is a discussion on precision - making sure I understand this correctly. within the C Programming forums, part of the General Programming Boards category; Hi all,
Just thought I'd discuss some basics on a nice sunny morning.
When we are dealing with precision - ...

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.

The circumference is 3.455752
The radius is 0.550000
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 radius is 5.500000
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.

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.

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.

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.