summaryrefslogtreecommitdiffstats
path: root/src/util/math.h
diff options
context:
space:
mode:
authorNino Miškić-Pletenac <nino.mip@gmail.com>2018-02-10 00:23:51 +0100
committerNino Miškić-Pletenac <nino.mip@gmail.com>2018-02-10 00:23:51 +0100
commit59f19eb5ed18414b2a27efec735802d52180ce29 (patch)
treee05cc7c12bc3f0c3ba95a420e2211d05d1b89159 /src/util/math.h
parent666068d65522afbd8d0a9adcf935733bd469e60c (diff)
parentd2513a6ed996c81def0618a97d3059dfeca02f75 (diff)
Merge commit 'd2513a6ed996c81def0618a97d3059dfeca02f75' into silencedetection
Diffstat (limited to 'src/util/math.h')
-rw-r--r--src/util/math.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/util/math.h b/src/util/math.h
index f2213fa604..646f8b53ac 100644
--- a/src/util/math.h
+++ b/src/util/math.h
@@ -18,6 +18,7 @@
// after our fpclassify hack
#include <algorithm>
+#include <type_traits>
#include "util/assert.h"
#include "util/fpclassify.h"
@@ -66,13 +67,28 @@ inline int roundUpToPowerOf2(int v) {
return power;
}
+inline double roundToFraction(double value, int denominator) {
+ int wholePart = value;
+ double fractionPart = value - wholePart;
+ double numerator = std::round(fractionPart * denominator);
+ return wholePart + numerator / denominator;
+}
+
template <typename T>
inline const T ratio2db(const T a) {
+ static_assert(std::is_same<float, T>::value ||
+ std::is_same<double, T>::value ||
+ std::is_same<long double, T>::value,
+ "ratio2db works only for floating point types");
return log10(a) * 20;
}
template <typename T>
inline const T db2ratio(const T a) {
+ static_assert(std::is_same<float, T>::value ||
+ std::is_same<double, T>::value ||
+ std::is_same<long double, T>::value,
+ "db2ratio works only for floating point types");
return pow(10, a / 20);
}