Comment

Comment by ardel95

The correct way most people would write this for positive integers is:

if (a < b)

  return a + (b - a) / 2;
else

  return b + (a - b) / 2;

This method is just more efficient (for places where it matters) as it avoids divisions and branches. But for a vast, vast majority of use-case that tiny efficiency gain doesn’t really matter.
parent ardel95 3 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton