c++ - floating point representation in memory is just not clear for me -
my task get fraction of float store in int. seemed easy. did test:
float f = 3.1415f; printf("int pres. of float: %d\n" "int: %d", *((int *)&f), 31415);
output:
int pres. of float: 1078529622
int: 31415
i changed them base 2 see 31415 present.
0100 0000 0100 1001 0000 1110 0101 0110 - 3.1415 0111 1010 1011 0111 - 31415
i don't know do. how fraction simple integer?
if take 2.5, instead of 3.1415, because it's easy understand...
so assumption 2.5 , 25 should have same binary format. not case. 25 = 0x19 = 11001. 2.5 = 10.1. not @ same thing.
if feel doing same sort of math 3.1415, goes this:
3 = 11 (i can that) 1/8 = 0.001 0.1415 - 0.125 = 0.0165 1/64 = 0.000001 0.0165 - 0.015625 = 0.000875 1/2048 = 0.00000000001
and still have fractions of 0.1415 left deal @ point, rough result 11.00100100001.
now, if compare binary output (starting @ mantissa part), , inserting decimal point, , take account floating point numbers "ignore first one":
11.00100100001 1.00100100001110
seems scribbles above aren't off... ;)
Comments
Post a Comment