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 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.