gpt4 book ai didi

c - 类型转换浮点值或使用 math.h floor* 函数?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:49:05 26 4
gpt4 key购买 nike

我正在编写一个 Interpolation Search 的实现在 C 中。

这道题其实很简单,我需要用浮点运算做线性插值来找到正确的索引,最终得到一个整数结果。

特别是我的探测索引是:

t = i + floor((((k-low)/(high-low)) * (j-i)));

其中,i、j、k、t 是无符号整数,high、low 是 double 。

这是否等同于:

t = i + (unsigned int)(((k-low)/(high-low)) * (j-i));

有什么理由让我真的想在简单的 (int) 类型转换上使用 math.h floor* 函数?

最佳答案

当值为 < 0 时它们是不同的。

floor(-1.5) = -2.0
(int)-1.5 = 1

关于c - 类型转换浮点值或使用 math.h floor* 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2573767/

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