diff options
Diffstat (limited to 'Swiften/Base')
-rw-r--r-- | Swiften/Base/FloatCompare.h | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Swiften/Base/FloatCompare.h b/Swiften/Base/FloatCompare.h new file mode 100644 index 0000000..152ffff --- /dev/null +++ b/Swiften/Base/FloatCompare.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2012 Yoann Blein + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <cmath> +#include <limits> + +namespace Swift { + bool approximatelyEqual(float a, float b, float epsilon = std::numeric_limits<float>::epsilon()) { + return std::fabs(a - b) <= ( (std::fabs(a) < std::fabs(b) ? std::fabs(b) : std::fabs(a)) * epsilon); + } + + bool essentiallyEqual(float a, float b, float epsilon = std::numeric_limits<float>::epsilon()) { + return std::fabs(a - b) <= ( (std::fabs(a) > std::fabs(b) ? std::fabs(b) : std::fabs(a)) * epsilon); + } + + bool definitelyGreaterThan(float a, float b, float epsilon = std::numeric_limits<float>::epsilon()) { + return (a - b) > ( (std::fabs(a) < std::fabs(b) ? std::fabs(b) : std::fabs(a)) * epsilon); + } + + bool definitelyLessThan(float a, float b, float epsilon = std::numeric_limits<float>::epsilon()) { + return (b - a) > ( (std::fabs(a) < std::fabs(b) ? std::fabs(b) : std::fabs(a)) * epsilon); + } +} |