Skip to content

Instantly share code, notes, and snippets.

@goldsborough
Created October 16, 2015 22:33
Show Gist options
  • Save goldsborough/f3e6b1d635e8899bd83f to your computer and use it in GitHub Desktop.
Save goldsborough/f3e6b1d635e8899bd83f to your computer and use it in GitHub Desktop.
Overflow-safe method to determine the greater of two values without comparison operators.
template<typename T>
T get_max(const T& first, const T& second)
{
double first_half = first / 2.0;
double second_half = second / 2.0;
bool sign = std::signbit(first_half - second_half);
return (sign * second) + (!sign * first);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment