Tuesday, 15 May 2012

floating point - How does casting from float to double work in C++? -



floating point - How does casting from float to double work in C++? -

this question has reply here:

strange output in comparing of float float literal 4 answers

the mantissa bits in float variable 23 in total while in double variable 53.

this means digits can represented exactly

a float variable log10(2^23) = 6.92368990027 = 6 in total

and double variable log10(2^53) = 15.9545897702 = 15

let's @ code:

float b = 1.12391; double d = b; cout<<setprecision(15)<<d<<endl;

it prints

1.12390995025635

however code:

double b = 1.12391; double d = b; cout<<setprecision(15)<<d<<endl;

prints 1.12391

can explain why different results? converted float variable of 6 digits double, compiler must know these 6 digits important. why? because i'm not using more digits can't represented correctly in float variable. instead of printing right value decides print else.

converting float double preserves value. reason, in first snippet, d contains approximation float precision of 112391/100000. rational 112391/100000 stored in float format 9428040 / 223. if carry out division, result 1.12390995025634765625: float approximation not close. cout << prints representation 14 digits after decimal point. first omitted digit 7, lastly printed digit, 4, rounded 5.

in sec snippet, d contains approximation double precision of value of 112391/100000, 1.123909999999999964614971759147010743618011474609375 (in other words 5061640657197974 / 252). approximation much closer rational. if printed 14 digits after decimal point, lastly digits zeroes (after rounding because first omitted digit 9). cout << not print trailing zeroes, see 1.12391 output.

because i'm not using more digits can't represented correctly in float variable

when incorrectly apply log10 223 (it should 224), number of decimal digits can stored in float. because float's representation not decimal, digits after these 7 or not zeroes in general. digits happen there in decimal closest binary representation compiler chose number wrote.

c++ floating-point floating-accuracy floating-point-precision

No comments:

Post a Comment