A function that Identifies Whether What Number is the input Closest

I have an input number.

E.g. : 0.75

I need to identify what number is the input closest to based on a given list ( 0.083, 0.25, 0.5 )

Is there a built-in Coda function for this ? In the example above, the function should return 0.5

what about the below logic. You evaluate each number using formulamap,

cheers, Christiaan

3 Likes

Built-in no, but you can compose a formula for this.

If your list is MyList and your input is Input, it could be like this:

MyList.Nth(
  Sequence(1, MyList.Count()).FormulaMap(
    List(AbsoluteValue(MyList.Nth(CurrentValue) - Input), CurrentValue)
  ).Sort(true).First().Last()
)

Explanation:

  • Sequence(1, MyList.Count()) iterates not on the elements of the list themselves, but on indices 1, 2, 3 etc.

  • Hence MyList.Nth(CurrentValue) to then actually get the value at that index.

  • For each index, compose a list of two elements: the absolute distance between numbers and the index of the number that resulted in that difference.

  • Sort(true) will then sort those lists, and since the first element is the distance, it will sort it properly, ascending.

  • All that’s left is to take the first (smallest) list, extract its second element (the index) and pass it to myList.Nth() to get the actual number.

2 Likes

This works for me :

thisRow.[Dummy 2].Nth(WithName(thisRow.[Dummy 2].FormulaMap(AbsoluteValue(CurrentValue-.75)).Min(), Minimum, thisRow.[Dummy 2].FormulaMap(AbsoluteValue(CurrentValue-.75)).Find(Minimum)))

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.