gpt4 book ai didi

c++ - 如何选择合适的倍数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:18:34 25 4
gpt4 key购买 nike

假设我有一个值列表,这些值乘以 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 为 91,875,但即使是 3584,diff 也低于 1。

最佳答案

请尝试以下操作:

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

关于c++ - 如何选择合适的倍数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com