2016-05-13 59 views
0

假设我有一个值乘以91,875并使用Round to even(又名银行家舍入)四舍五入的值的列表。以下是前20个倍数:如何选择正确的倍数?

0   0 
91,875  92 
183,75  184 
275,625  276 
367,5  368 
459,375  459 
551,25  551 
643,125  643 
735   735 
826,875  827 
918,75  919 
1010,625 1011 
1102,5  1102 
1194,375 1194 
1286,25  1286 
1378,125 1378 
1470  1470 
1561,875 1562 
1653,75  1654 
1745,625 1746 

假设在这些值之间,我的系统接收其他值。我需要检查其中哪些是我的原始91,875步骤的多个,哪些不是。

例子。从列表我:

3583 (before rounding it was 91,875 * 39 = 3583,125) 
3584 
在这种情况下

,我知道,选择值仅为3583,因为:

lrint(91,875 * 39) = 3583 

并放弃3584,这只是在3583和之间的值下一步:

lrint(91,875 * 39) = 3583 
lrint(91,875 * 40) = 3675 

我该如何选择它?当我得到这些值时,我没有3940。我试着:

int times = round(currentSample/samplesPerPulse); 
double diff = abs(samplesPerPulse * times - currentSample); 

if (diff < 1) { 
    ... do somethings 
} 

其中currentSample是3583和3584和samplesPerPulse 91875,但即使3584 diff低于1

+0

为什么不是你把你怀疑的价值除以91,875,再乘以回合,并将结果与​​输入进行比较? – GMichael

+0

@迈克尔:当我得到我的价值观时,我没有“39”和“40”,所以我不知道“我应该增加多少”! – markzzz

+3

lrint(floor(input/91875)* 91875)== input? (打印(输入/ 91875)* 91875)==输入? – GMichael

回答

1

请尝试以下方法:

if((lrint(floor(currentSample/samplesPerPulse) * samplesPerPulse) == currentSample) or 
    (lrint(ceil(currentSample/samplesPerPulse) * samplesPerPulse) == currentSample) 
) 
{ 
    .... do something 
}