Good way to latch/quantize to nearest value within a table?

Hey all,
Just curious if there’s a fairly standard/efficient way to take a single value (any type, i/a/k) and quantize it to the nearest possible value within an f-table. I’m still wrapping my head around how to parse/access tables and am curious if this is something fairly standard. I’d imagine it is for quantizing to particular scales/tunings/note values but am having a hard time finding a good method of doing this from the docs alone. Probably missing something obvious here so figured I’d ask.

As always, much thanks in advance. Really loving this language. That you people much more smart in this field than me for making such an amazing tool for us mere mortals! It’s honestly been pretty mind blowing with the amount of possibilities this allows you once you get some of the basic concepts down.

Cheers!

I’m not sure of any clever way to do this. I would probably write a UDO that would search the table contents and return the best match? Might not be all that efficient.

Something like (untested!!).

opcode FindClosest ii,i
    iTable, iValue xin
    iClosest = "GET_MAX_VALUE_FROM_TABLE"
    iLen = ftlen(iTable)
    iCnt init 0
    while iCnt < iLen
        if tab:i(iCnt, iTable) >= iValue && tab:i(iCnt, iTable) < iClosest) then
            iClosest = tab:i(iCnt, iTable) 
        endif
        iCnt += 1
    od
    xout iClosest
endop

Also, it might be easier to do this with arrays. I couldn’t find a quick way to get the largest number from a function table, but one exists for arrays:

Well it’s good to know that at least it isn’t something standard that I missed in the docs haha. I’m thinking most efficient way is to probably break up tables by octave ranges maybe, so then you’re just searching within a designated range which would reduce the amount of searching it would have to do each time the value changed as if you were trying to quantize say an LFO or something that’s moving quickly you probably wouldn’t want to search a table with every possible quantized note.

Thanks for the response. I’ll keep pondering this for a bit and post a solution if I come up with something that might be useful to anyone else trying to do this.