64template <
typename T>
inline constexpr T
Clamp(
const T& lower,
const T& value,
const T& upper )
68 else if( upper < value )
74#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
75 _Pragma(
"GCC diagnostic push" ) \
76 _Pragma(
"GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
84template <
typename fp_type,
typename ret_type =
int>
87 using max_ret =
long long int;
88 fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
90 if( ret > std::numeric_limits<ret_type>::max() )
94 return std::numeric_limits<ret_type>::max() - 1;
96 else if( ret < std::numeric_limits<ret_type>::lowest() )
100 if( std::numeric_limits<ret_type>::is_signed )
101 return std::numeric_limits<ret_type>::lowest() + 1;
106 return ret_type( max_ret( ret ) );
109#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
110 _Pragma(
"GCC diagnostic pop" )
118T
rescale( T aNumerator, T aValue, T aDenominator )
120 return aNumerator * aValue / aDenominator;
126 return ( T( 0 ) < val) - ( val < T( 0 ) );
131int rescale(
int aNumerator,
int aValue,
int aDenominator );
134int64_t
rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
146typename std::enable_if<std::is_floating_point<T>::value,
bool>::type
147equals( T aFirst, T aSecond, T aEpsilon = std::numeric_limits<T>::epsilon() )
149 T diff =
std::abs( aFirst - aSecond );
151 if( diff < aEpsilon )
158 T largest = aFirst > aSecond ? aFirst : aSecond;
160 if( diff <= largest * aEpsilon )
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
std::enable_if< std::is_floating_point< T >::value, bool >::type equals(T aFirst, T aSecond, T aEpsilon=std::numeric_limits< T >::epsilon())
Template to compare two floating point values for equality within a required epsilon.
void kimathLogOverflow(double v, const char *aTypeName)
Workaround to avoid the empty-string conversion issue in wxWidgets.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
T rescale(T aNumerator, T aValue, T aDenominator)
Scale a number (value) by rational (numerator/denominator).
void kimathLogDebug(const char *aFormatString,...)
Helper to avoid directly including wx/log.h for the templated functions in kimath.
constexpr T Clamp(const T &lower, const T &value, const T &upper)
Limit value within the range lower <= value <= upper.