Skip to content

Instantly share code, notes, and snippets.

@paddy74
Last active May 12, 2020 23:39
Show Gist options
  • Save paddy74/4031b30b2b6823a4d328246d74c07e34 to your computer and use it in GitHub Desktop.
Save paddy74/4031b30b2b6823a4d328246d74c07e34 to your computer and use it in GitHub Desktop.
Compare floats while safely handling invisible float precision overflow
#pragma once
#include <limits>
/**
* Check if a and b are within epsilon range of eachother.
*
* Based on this post: https://www.reddit.com/r/ProgrammerHumor/comments/ggwxjp/the_dwindling_sanity_of_valves_programmers/fq646zf?utm_source=share&utm_medium=web2x
**/
template<typename T> // Numeric type
bool epsilonCompare(T a, T b)
{
using epsilon = std::numeric_limits<double>::epsilon();
return (a - epsilon < b && res + epsilon > b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment