diff options
author | Alexander Fokin <apfokin@gmail.com> | 2022-02-10 16:45:38 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:38 +0300 |
commit | 863a59a65247c24db7cb06789bc5cf79d04da32f (patch) | |
tree | 139dc000c8cd4a40f5659e421b7c75135d080307 /util/generic/ymath.h | |
parent | f64e95a9eb9ab03240599eb9581c5a9102426a96 (diff) | |
download | ydb-863a59a65247c24db7cb06789bc5cf79d04da32f.tar.gz |
Restoring authorship annotation for Alexander Fokin <apfokin@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'util/generic/ymath.h')
-rw-r--r-- | util/generic/ymath.h | 126 |
1 files changed, 63 insertions, 63 deletions
diff --git a/util/generic/ymath.h b/util/generic/ymath.h index 9ff9ae2abe..aabb1b0295 100644 --- a/util/generic/ymath.h +++ b/util/generic/ymath.h @@ -7,46 +7,46 @@ #include <cfloat> #include <cstdlib> -#include "typetraits.h" -#include "utility.h" - +#include "typetraits.h" +#include "utility.h" + constexpr double PI = M_PI; constexpr double M_LOG2_10 = 3.32192809488736234787; // log2(10) constexpr double M_LN2_INV = M_LOG2E; // 1 / ln(2) == log2(e) -/** - * \returns Absolute value of the provided argument. - */ -template <class T> +/** + * \returns Absolute value of the provided argument. + */ +template <class T> constexpr T Abs(T value) { return std::abs(value); -} - -/** - * @returns Base 2 logarithm of the provided double - * precision floating point value. - */ -inline double Log2(double value) { - return log(value) * M_LN2_INV; -} - -/** - * @returns Base 2 logarithm of the provided - * floating point value. - */ -inline float Log2(float value) { - return logf(value) * static_cast<float>(M_LN2_INV); -} - -/** - * @returns Base 2 logarithm of the provided integral value. - */ +} + +/** + * @returns Base 2 logarithm of the provided double + * precision floating point value. + */ +inline double Log2(double value) { + return log(value) * M_LN2_INV; +} + +/** + * @returns Base 2 logarithm of the provided + * floating point value. + */ +inline float Log2(float value) { + return logf(value) * static_cast<float>(M_LN2_INV); +} + +/** + * @returns Base 2 logarithm of the provided integral value. + */ template <class T> inline std::enable_if_t<std::is_integral<T>::value, double> -Log2(T value) { - return Log2(static_cast<double>(value)); -} - +Log2(T value) { + return Log2(static_cast<double>(value)); +} + /** Returns 2^x */ double Exp2(double); float Exp2f(float); @@ -100,10 +100,10 @@ inline double Erf(double x) { } #endif -/** - * @returns Natural logarithm of the absolute value - * of the gamma function of provided argument. - */ +/** + * @returns Natural logarithm of the absolute value + * of the gamma function of provided argument. + */ inline double LogGamma(double x) noexcept { #if defined(_glibc_) int sign; @@ -118,7 +118,7 @@ inline double LogGamma(double x) noexcept { #else extern double LogGammaImpl(double); return LogGammaImpl(x); -#endif +#endif } /** @@ -146,32 +146,32 @@ T Power(T x, Int n) { } return result; }; - -/** - * Compares two floating point values and returns true if they are considered equal. - * The two numbers are compared in a relative way, where the exactness is stronger - * the smaller the numbers are. - * - * Note that comparing values where either one is 0.0 will not work. - * The solution to this is to compare against values greater than or equal to 1.0. - * - * @code - * // Instead of comparing with 0.0 - * FuzzyEquals(0.0, 1.0e-200); // This will return false - * // Compare adding 1 to both values will fix the problem - * FuzzyEquals(1 + 0.0, 1 + 1.0e-200); // This will return true - * @endcode - */ -inline bool FuzzyEquals(double p1, double p2, double eps = 1.0e-13) { - return (Abs(p1 - p2) <= eps * Min(Abs(p1), Abs(p2))); -} - -/** - * @see FuzzyEquals(double, double, double) - */ -inline bool FuzzyEquals(float p1, float p2, float eps = 1.0e-6) { - return (Abs(p1 - p2) <= eps * Min(Abs(p1), Abs(p2))); -} + +/** + * Compares two floating point values and returns true if they are considered equal. + * The two numbers are compared in a relative way, where the exactness is stronger + * the smaller the numbers are. + * + * Note that comparing values where either one is 0.0 will not work. + * The solution to this is to compare against values greater than or equal to 1.0. + * + * @code + * // Instead of comparing with 0.0 + * FuzzyEquals(0.0, 1.0e-200); // This will return false + * // Compare adding 1 to both values will fix the problem + * FuzzyEquals(1 + 0.0, 1 + 1.0e-200); // This will return true + * @endcode + */ +inline bool FuzzyEquals(double p1, double p2, double eps = 1.0e-13) { + return (Abs(p1 - p2) <= eps * Min(Abs(p1), Abs(p2))); +} + +/** + * @see FuzzyEquals(double, double, double) + */ +inline bool FuzzyEquals(float p1, float p2, float eps = 1.0e-6) { + return (Abs(p1 - p2) <= eps * Min(Abs(p1), Abs(p2))); +} namespace NUtilMathPrivate { template <bool IsSigned> |