Last active
May 12, 2020 23:39
-
-
Save paddy74/4031b30b2b6823a4d328246d74c07e34 to your computer and use it in GitHub Desktop.
Compare floats while safely handling invisible float precision overflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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