gpt4 book ai didi

numbers - "Nearly divisible"

转载 作者:行者123 更新时间:2023-12-04 01:08:46 26 4
gpt4 key购买 nike

我想检查一个浮点值是否“接近”32 的倍数。例如64.1“几乎”可以被 32 整除,63.9 也是如此。

现在我正在这样做:

#define NEARLY_DIVISIBLE 0.1f
float offset = fmodf( val, 32.0f ) ;
if( offset < NEARLY_DIVISIBLE )
{
// its near from above
}
// if it was 63.9, then the remainder would be large, so add some then and check again
else if( fmodf( val + 2*NEARLY_DIVISIBLE, 32.0f ) < NEARLY_DIVISIBLE )
{
// its near from below
}

有更好的方法来做到这一点吗?

最佳答案

好吧,您可以通过再减去 32 次来删除第二个 fmodf 以从下面获得 mod。

  if( offset < NEARLY_DIVISIBLE )
{
// it's near from above
}
else if( offset-32.0f>-1*NEARLY_DIVISIBLE)
{
// it's near from below
}

关于numbers - "Nearly divisible",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2489992/

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