52 template <
typename T>
inline const T&
Clamp(
const T& lower,
const T& value,
const T& upper )
56 else if( upper < value )
62 #ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION 63 _Pragma(
"GCC diagnostic push" ) \
64 _Pragma(
"GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
72 template <
typename fp_type,
typename ret_type =
int>
75 using max_ret =
long long int;
76 fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
78 if( std::numeric_limits<ret_type>::max() < ret ||
79 std::numeric_limits<ret_type>::lowest() > ret )
81 kimathLogDebug(
"Overflow KiROUND converting value %f to %s",
double( v ),
82 typeid( ret_type ).
name() );
86 return ret_type( max_ret( ret ) );
89 #ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION 90 _Pragma(
"GCC diagnostic pop" )
98 T
rescale( T aNumerator, T aValue, T aDenominator )
100 return aNumerator * aValue / aDenominator;
103 template <
typename T>
106 return ( T( 0 ) < val) - ( val < T( 0 ) );
111 int rescale(
int aNumerator,
int aValue,
int aDenominator );
114 int64_t
rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
void kimathLogDebug(const char *aFormatString,...)
Helper to avoid directly including wx/log.h for the templated functions in kimath.
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
const T & Clamp(const T &lower, const T &value, const T &upper)
Limit value within the range lower <= value <= upper.