聘我网

新概念招聘3.0

浮点精度丢失?

vote up0vote downstar
#include <stdio.h>
#define N 51200000
int main()
{
 float f = 0.0f;
 for(int i = 0; i < N; i++)
  f += 1.0f;
 fprintf(stdout, "%f\n", f);
 return 0;
}

期望的结果是51200000,结果却是16777216,why?

 

1 个答复

vote up0vote downcheck

单精度浮点数有效位只有23bit

511999999需要26bit

使用double就够了,52bit有效位。

各种浮点数的精度参考此处

更新

关于这个问题有专门的介绍:

Unit in the last place

链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.