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;
105#if __cplusplus >= 202302L
106 else if constexpr( std::is_floating_point_v<fp_type> )
108 if( std::isnan( v ) )
117 return ret_type( max_ret( ret ) );
120#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
121 _Pragma(
"GCC diagnostic pop" )
129T
rescale( T aNumerator, T aValue, T aDenominator )
131 return aNumerator * aValue / aDenominator;
137 return ( T( 0 ) < val) - ( val < T( 0 ) );
142int rescale(
int aNumerator,
int aValue,
int aDenominator );
145int64_t
rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
157typename std::enable_if<std::is_floating_point<T>::value,
bool>::type
158equals( T aFirst, T aSecond, T aEpsilon = std::numeric_limits<T>::epsilon() )
160 T diff =
std::abs( aFirst - aSecond );
162 if( diff < aEpsilon )
169 T largest = aFirst > aSecond ? aFirst : aSecond;
171 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.