c - Issue with floating point representation -


int x=25,i;  float *p=(float *)&x; printf("%f\n",*p); 

i understand bit representation floating point numbers , int different, no matter value store, answer 0.000000. shouldn't other value depending on floating point representation?

for purposes of discussion, we're going assume both int , float 32 bits wide. we're going assume ieee-754 floats.

floating point values represented sign * βexp * signficand. 32-bit binary floats, β 2, exponent exp ranges -126 127, , significand normalized binary fraction, such there single leading non-zero bit before radix point. example, binary integer representation of 25

110012

while binary floating point representation of 25.0 be:

1.10012 * 24 // normalized 

the ieee-754 encoding 32-bit float is

s eeeeeeee fffffffffffffffffffffff 

where s denotes sign bit, e denotes exponent bits, , f denotes significand (fraction) bits. exponent encoded using "excess 127" notation, meaning exponent value of 127 (011111112) represents 0, while 1 (000000012) represents -126 , 254 (111111102) represents 127. leading bit of significand not explicitly stored, 25.0 encoded as

0 10000011 10010000000000000000000 // exponent 131-127 = 4 

however, happens when map bit pattern 32-bit integer value 25 onto 32-bit floating point format? wind following:

0 00000000 00000000000000000011001 

it turns out in ieee-754 floats, exponent value 000000002 reserved representing 0.0 , subnormal (or denormal) numbers. subnormal number number close 0 can't represented 1.??? * 2exp, because exponent have smaller can encode in 8 bits. such numbers interpreted 0.??? * 2-126, many leading 0s necessary.

in case, adds 0.000000000000000000110012 * 2-126, gives 3.50325 * 10-44.

you'll have map large integer values (in excess of 224) see other 0 out bunch of decimal places. and, keith says, undefined behavior anyway.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -