c - Floats being Inexact -


i puzzled. have no explanation why test passes when using double data type fails when using float data type. consider following snippet of code.

float total = 0.00;  ( int = 0; < 100; i++ ) total += 0.01; 

one anticipate total 1.00, equal 0.99. why case? compiled both gcc , clang, both compilers have same result.

the value 0.01 in decimal expressed series: a1*(1/2) + a2*(1/2)^2 + a3*(1/2)^4 + etc. an 0 or one.

i leave figure out specific values of a1, a2 , how many fractional bits (an) required. in cases decimal fraction cannot represented finite series of (1/2)^n values.

for series sum 0.01 in decimal requires an go beyond number of bits stored in float (full word of bits minus number of bits sign , exponent). since double has more bits 0.01 decimal can/might/maybe (you calculation) precisely defined.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -