本文介绍了如何使用FMOD,避免precision问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要归结这个问题最简单的形式:
for(double d=0.0; d<=5.0; d+=0.05) {
if(fmod(d,0.25) is equal 0)
print 'X';
}
This will of course not work since d
will be [0, 0.05000000001, 0.100000000002, ...] causing fmod() to fail. Extreme example is when d=1.999999999998
and fmod(d,0.25) = 1
.
How to tackle this? Here is an editable online example.
解决方案
I'd solve this by simply not using floating point variables in that way:
for (int i = 0; i <= 500; i += 5) {
double d = i / 100.0; // in case you need to use it.
if ((i % 25) == 0)
print 'X';
}
They're usually problematic enough that it's worth a little extra effort in avoiding them.
这篇关于如何使用FMOD,避免precision问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!