1
0
mirror of https://github.com/jaredtao/TaoQuick.git synced 2025-01-31 21:22:58 +08:00

add MathHelp

This commit is contained in:
jared 2021-04-26 17:47:11 +08:00
parent e2f2105847
commit 8851d9f8fd
2 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,39 @@
#pragma once
#include <cmath>
#include <QtGlobal>
#include <algorithm>
//inRange 检查 value 小于等于 max 大于等于min
//inRange<T>通用模版函数
template<typename T>
static bool inRange(const T &value, const T &min, const T &max)
{
if (min <= value && value <= max) {
return true;
}
return false;
}
//inRange<double> 模版偏特化遇到double时使用std的浮点数比较代替 常规比较。规避精度误差
template<>
static bool inRange<double>(const double &value, const double &min, const double &max)
{
if (std::isgreaterequal(value, min) && std::islessequal(value, max)) {
return true;
}
return false;
}
//inRange<float> 模版偏特化遇到float时使用std的浮点数比较代替 常规比较。规避精度误差
template<>
static bool inRange<float>(const float &value, const float &min, const float &max)
{
if (std::isgreaterequal(value, min) && std::islessequal(value, max)) {
return true;
}
return false;
}
//clamp, 限制 value在 [min - Max]区间.
template<typename T>
static T clamp(const T &value, const T &min, const T &max)
{
return std::max(min,std::min(value, max));
}

View File

@ -11,6 +11,7 @@ HEADERS += \
$$PWD/Common/Package.h \
$$PWD/Common/PropertyHelper.h \
$$PWD/Common/Subject.h \
$$PWD/Common/MathHelp.h \
$$PWD/Frameless/TaoFrameLessView.h \
$$PWD/Logger/Logger.h \
$$PWD/Logger/LoggerTemplate.h \