Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

(Floating Point Airthmatic error )Why the Result is not zero??

Status
Not open for further replies.

Naveed Ahmed

Member level 4
Joined
Aug 31, 2008
Messages
77
Helped
9
Reputation
18
Reaction score
9
Trophy points
1,288
Location
Singapore
Activity points
1,862
float a=1.0;
char i;
for(i=0;i<100;i++)
{
a=a-0.01;
}
printf("%e\n",a);

In the above code, I expect the result of "a" to be 0, but why its not zero?? Can anyone tell me what is the reason for its error in precision?

Thnks,

Regards,
Naveed
 

a float decimal value such as 0.1 is converted by the compiler into a binary equivalent using some float point representation.
Typically a float represented by 32 bit binary has a precision of about 7 significant figures.
Copnsider the following version of your program
Code:
float a=1.0;
char i;
printf("0.1 is %2.15f\n\n", 0.1f);
for(i=0;i<101;i++)
{
printf("%2.15f\n",a);
a=a-0.01;
}
}
when run it gives
Code:
0.1 is 0.100000001490116

1.000000000000000
0.990000009536743
0.980000019073486
0.970000028610229
0.960000038146973
0.950000047683716
0.940000057220459
0.930000066757202
0.920000076293945
0.910000085830688
0.900000095367432
0.890000104904175
0.880000114440918
0.870000123977661
0.860000133514404
0.850000143051147
0.840000152587891
0.830000162124634
0.820000171661377
0.810000181198120
0.800000190734863
0.790000200271606
0.780000209808350
0.770000219345093
0.760000228881836
0.750000238418579
0.740000247955322
0.730000257492065
0.720000267028809
0.710000276565552
0.700000286102295
0.690000295639038
0.680000305175781
0.670000314712524
0.660000324249268
0.650000333786011
0.640000343322754
0.630000352859497
0.620000362396240
0.610000371932983
0.600000381469727
0.590000391006470
0.580000400543213
0.570000410079956
0.560000419616699
0.550000429153442
0.540000438690186
0.530000448226929
0.520000457763672
0.510000467300415
0.500000476837158
0.490000486373901
0.480000495910645
0.470000505447388
0.460000514984131
0.450000524520874
0.440000534057617
0.430000543594360
0.420000553131104
0.410000562667847
0.400000572204590
0.390000581741333
0.380000591278076
0.370000600814819
0.360000610351563
0.350000619888306
0.340000629425049
0.330000638961792
0.320000648498535
0.310000658035278
0.300000667572021
0.290000677108765
0.280000686645508
0.270000696182251
0.260000705718994
0.250000715255737
0.240000709891319
0.230000704526901
0.220000699162483
0.210000693798065
0.200000688433647
0.190000683069229
0.180000677704811
0.170000672340393
0.160000666975975
0.150000661611557
0.140000656247139
0.130000650882721
0.120000652968884
0.110000655055046
0.100000657141209
0.090000659227371
0.080000661313534
0.070000663399696
0.060000661760569
0.050000660121441
0.040000658482313
0.030000658705831
0.020000658929348
0.010000659152865
0.000000659152875
as you can see the float value 0.1 is printed as 0.1 is 0.100000001490116

precision is critical in numerical calculations - for further discussion see
INFO: Precision and Accuracy in Floating-Point Calculations
 

Thnx horace,

It means its precision is for 7 digits after decimal point.

Regars,
Naveed
 

it is the precision of all the digits not just after the decimal point.
Consider pi = 3.1415926535897932384626433 - if float was 6 digits only 3.14159 would be significant.
in header file <float.h> there are constants that specify the precisions and other information for float types, e.g. FLT_DIG and DBL_DIG
cfloat (float.h) - C++ Reference

consider the following program
Code:
#include <stdio.h>
#include <float.h>

int main()
{
    float pi=3.1415926535897932384626433f;
    double pid = 3.1415926535897932384626433;
    printf("FLT_DIG %d DBL_DIG %d \n", FLT_DIG, DBL_DIG );
    printf("float  pi %2.20f\n", pi);
    printf("double pi %2.20f\n", pid);
}
when run using gcc on a Windows PCgives
Code:
FLT_DIG 6 DBL_DIG 15 
float  pi 3.141592741012573
double pi 3.141592653589793
you can see although the float pi is printed to 20 places after the decimal point only the first 5 or 6 digits are significant, the rest are rubbish.
the double pi is significant to about 15 figures.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top