简体   繁体   English

在数组中搜索最近的float值

[英]Searching For Closest float values in an array

I have a problem trying to pick the closest float value out of an array. 我在尝试从数组中选择最接近的float值时遇到问题。 Here is some example data; 这是一些示例数据;

The numbers that I will be dealing with always share this mirroring characteristic. 我要处理的数字始终具有这种镜像特性。

{-9,-3,-1,0,1,3,9}

If I search for -8.8 I would expect to be returned -9. 如果我搜索-8.8,则期望返回-9。

If I searched for 8.8 I would expect to be returned 9. 如果我搜索8.8,则期望返回9。

In the past when searching arrays for closest values I would go through the array keeping track of the absolute value for each array element minus the value I wanted. 过去,在数组中搜索最接近的值时,我会遍历数组,跟踪每个数组元素的绝对值减去所需的值。 The Smallest value would win. 最小的价值将获胜。

That method presents a problem here for me tho because at least 2 numbers in the array would be "closest" (in my above example 9 & -9) 该方法对我来说是一个问题,因为数组中至少有两个数字是“最接近的”(在我上面的示例9和-9中)

your array will always be sorted, so binary search should be amenable to reduce the candidate set to 2 array values at max. 您的数组将始终被排序,因此二进制搜索应适合于将候选集最大减少为2个数组值。 i can only conceive of one challenge which will arise if the original array contains floats some of which differ by less than the machine precision. 我只能想到如果原始数组包含浮点数,其中一些浮点数的差异小于机器精度,就会出现一个挑战。

how to deal best with this situation will depend on your application (if it isn't esoteric in the first place); 如何最好地处理这种情况将取决于您的应用程序(如果不是一开始就很深奥的话); note however that all the values indistinguishable from your test value will form a contiguous subsequence in your array, so as a heuristic you might just pick the middle element of this subsequence. 但是请注意,所有与测试值无法区分的值都将在数组中形成一个连续的子序列,因此作为启发式方法,您可以选择该子序列的中间元素。

The fact that your array is mirrored makes this easy. 阵列已镜像的事实使此操作变得容易。 You can initially ignore the sign of your search value and, as you mentioned, just find the closest absolute value. 您最初可以忽略搜索值的符号,并且如上所述,您只需找到最接近的绝对值即可。 Then fix the sign. 然后固定标志。

This is Python, but it should be close enough to pseudocode that you can translate it to whatever you need. 这是Python,但是它应该与伪代码足够接近,可以将其转换为所需的任何东西。

def find_closest(search_val):
    smallest_diff = float(inf)
    closest_val = 0
    # Search only positive half of array
    for val in [0, 1, 3, 9]:
        # Treat search_val as positive for now
        diff = abs(val - abs(search_val))
        if diff < smallest_diff:
            smallest_diff = diff
            closest_val = val
    # Correct sign of result
    if search_val < 0:
        closest_val = -closest_val
    return closest_val

Since, as you have mentioned, your numbers are always mirrored around zero, then only examine the negative numbers (assuming that you array is sorted). 如前所述,既然您的数字始终镜像在零附近,那么仅检查负数(假设数组已排序)。 You will avoid the mentioned problem. 您将避免提到的问题。 What about 0.5? 0.5呢? It too has two numbers that are at equal distance. 它也有两个等距的数字。 How would you break the tie? 你怎么打领带?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM