hus*_*sik -6 c++ floating-point
在这里,我从浮点变量中减去128.0和129.0倍.
#include "stdafx.h"
#include<stdlib.h> //is this the problem? Or am i doing something wrong?
int main()
{
float d1=3.0e9;
printf("\n before: %f \n",d1);
for(int i=0;i<2000000;i++) d1=d1-128.0; //doesnt change!
printf("\n after : %f \n",d1);
for(int i=0;i<2000000;i++) d1=d1-129.0; //does change!
printf("\n after2: %f \n",d1);
//is 129 is the minimum step for sub/add ? Isnt this wrong?
//Is this about exponential part 10^9 ?
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:

问题:为什么这个浮点数不会因为小于129的操作数添加/ subbing而改变?因为我选择初始浮动值3.0e9?
当我选择初始值3.0e10时,初始化和两次减法都不起作用.
当我选择初始值3.0e8时,最小变化为17.所以16不会改变.:(
所以,谢谢你的回答.当初始值变小时,最小步长会根据精度变小.
VC++ 2010表达.windows xp 32位.奔腾-M
用double而不是float.
然后了解"精确"的概念.
类型float适用于大约9位小数.这里有32位浮点的完整描述: