我有一个看起来像这样的方法:
float * mutate(float* organism){
int i;
float sign = 1;
static float newOrg[INPUTS] = {0};
for (i = 0;i<INPUTS;i++){
if (rand() % 2 == 0) {
sign = 1;
} else {
sign = -1;
}
float temp = (organism[i] + sign);
printf("bf: %f af: %f diff: %f sign: %f sign2: %f temp: %f\n\n",
organism[i], (organism[i] + sign), (organism[i] + sign)-organism[i],
sign, sign+sign, temp);
newOrg[i] = organism[i] + sign;
}
return newOrg;
}
When sign
前两个不为0"%f"
s 是相同的,第三个是 0,将总和放入变量中也没有帮助。这让我很困惑!如果需要,我可以发布完整的代码。
Output:
bf: 117810016.000000 af: 117810016.000000 diff: 0.000000 sign: 1.000000 sign2: 2.000000 temp: 117810016.000000
有限精度float
.
A typical float
can only represent about 232 different numbers. 117,810,016.0 and 1.0 are two of them. 117,810,017.0 is not. So the C sum of 117810016.0 + 1.0
results in the "best" answer of 117810016.0
.
Using a higher precision type like double
often will extend the range of +1 exact math, but even that will not be exact with large enough values (typically about 9.0*10e15 or 253).
If code is to retain using float
, suggest limiting organism[i]
to values to the inclusive range or ±8,388,608.0 (223).
也许代码可以简单地使用整数类型来完成此任务,例如long long
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)