find_closest(9)
π find_closest β from the Kernel API Manual, bestie π
Okay so listen, this one is from the kernel's own little API manual and it's genuinely so useful it's not even funny. It's called find_closest and it does exactly one thing, but it does it beautifully.
β¨ NAME β¨
find_closest β locate the closest element in a sorted array. That's it. That's the whole gig. Main character energy, zero notes. π
π¦ SYNOPSIS π¦
find_closest (x , a , as );
Yes bestie, that's the whole call. Three args, no drama. β¨
πΈ ARGUMENTS πΈ
xβ the reference value. Like, the thing you're measuring everything else against. The vibe-setter, if you will. πaβ the array you're digging through to find the closest element. Must be sorted in ascending order, no cap β if it's not sorted, that's not a find_closest problem, that's a you problem. πasβ size ofa. Yeah it's spelled kind of cursed, but that's the parameter name and we respect it. π
π§ DESCRIPTION π§
Returns the index of the element closest to x. That's it! That's the function! You give it a reference value and a sorted array and it hands you back the index of whichever element is nearest. Iconic behaviour, honestly. π₯
β NOTE β
β οΈ Okay this part actually matters, so pay attention bestie: if you're using an array of negative numbers (or mixed positive numbers), then be sure thatxis of a signed-type to get good results. Don't come crying to me when your unsignedxdoes something unhinged. π Signed type. Do it. Periodt.
π SEE ALSO π
Head over to the kernel file ./include/linux/util_macros.h β that's where the family lives. And say hi to the whole crew while you're there:
Squad goals fr fr π«Ά
Okay that's the whole page bestie, you're basically a kernel dev now. This one lives in ./include/linux and the manual's dated October 2026 β so it's giving future-iconic. Slay, go find your closest element, and remember: sorted ascending, signed x. Mwah. ππ
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms