find_closest(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… 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 of a. 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 that x is of a signed-type to get good results. Don't come crying to me when your unsigned x does 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