objective c - Searching For Closest float values in an array -


i have problem trying pick closest float value out of array. here example data;

the numbers dealing share mirroring characteristic.

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

if search -8.8 expect returned -9.

if searched 8.8 expect returned 9.

in past when searching arrays closest values go through array keeping track of absolute value each array element minus value wanted. smallest value win.

that method presents problem here me tho because @ least 2 numbers in array "closest" (in above example 9 & -9)

your array sorted, binary search should amenable reduce candidate set 2 array values @ max. can conceive of 1 challenge arise if original array contains floats of differ less machine precision.

how deal best situation depend on application (if isn't esoteric in first place); note values indistinguishable test value form contiguous subsequence in array, heuristic might pick middle element of subsequence.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -