57#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
58 _Pragma(
"GCC diagnostic push" ) \
59 _Pragma(
"GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
68template <
typename in_type =
long long int,
typename ret_type =
int>
71 if constexpr( std::is_same_v<in_type, long long int> && std::is_same_v<ret_type, int> )
73 if( v > std::numeric_limits<int>::max() )
77 return std::numeric_limits<int>::max();
79 else if( v < std::numeric_limits<int>::lowest() )
83 return std::numeric_limits<int>::lowest();
101template <
typename fp_type,
typename ret_type =
int>
102constexpr ret_type
KiROUND( fp_type v,
bool aQuiet =
false )
104 using limits = std::numeric_limits<ret_type>;
106#if __cplusplus >= 202302L
107 if constexpr( std::is_floating_point_v<fp_type> )
109 if( std::isnan( v ) )
119 long long rounded = std::llround( v );
120 long long clamped = std::clamp<long long>( rounded,
121 static_cast<long long>( limits::lowest() ),
122 static_cast<long long>( limits::max() ) );
124 if( !aQuiet && clamped != rounded )
127 return static_cast<ret_type
>( clamped );
130#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
131 _Pragma(
"GCC diagnostic pop" )
141 return aNumerator * aValue / aDenominator;
147 return (
T( 0 ) < val) - ( val <
T( 0 ) );
152int rescale(
int aNumerator,
int aValue,
int aDenominator );
155int64_t
rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
167typename std::enable_if<std::is_floating_point<T>::value,
bool>::type
168equals(
T aFirst,
T aSecond,
T aEpsilon = std::numeric_limits<T>::epsilon() )
170 const T diff =
std::abs( aFirst - aSecond );
172 if( diff < aEpsilon )
179 T largest = aFirst > aSecond ? aFirst : aSecond;
181 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.
constexpr int sign(T val)
void kimathLogOverflow(double v, const char *aTypeName)
Workaround to avoid the empty-string conversion issue in wxWidgets.
constexpr ret_type KiROUND(fp_type v, bool aQuiet=false)
Round a numeric value to an integer using "round halfway cases away from zero" and clamp the result t...
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 ret_type KiCheckedCast(in_type v)
Perform a cast between numerical types.