如何选择合适的倍数?

How can I choose the right multiple?

假设我有一个值列表,这些值乘以 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

我怎样才能select呢?当我得到这些值时,我没有 3940。我试过:

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

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

其中 currentSample 是 3583 和 3584,samplesPerPulse 91,875,但即使是 3584 diff 也小于 1.

请尝试以下操作:

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