浮点精度
本文深入描述浮点float类型在内存的存储方式,和为什么浮点计算时精度会缺失。
通过一下程序我们不断可以看到类型转换的的实质。 而且可以验证float在内存中的存储。
#include <stdio.h>
?int
main(
void
)
{
????????
int
i =
65536
;
????????
float
f = *(
float
*)(&i);
????????
printf(
"float num : %.30f\n"
,f);
?????????
float
f2 =
7.0
;
????????
printf(
"float f2 is: %.5f\n"
,f2);
????????
int
s = *(
int
*)&f2;
????????
printf(
"float to int is: %d \n"
,s);
}