56#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
57 _Pragma(
"GCC diagnostic push" ) \
58 _Pragma(
"GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
67template <
typename in_type =
long long int,
typename ret_type =
int>
70 if constexpr( std::is_same_v<in_type, long long int> && std::is_same_v<ret_type, int> )
72 if( v > std::numeric_limits<int>::max() )
76 return std::numeric_limits<int>::max();
78 else if( v < std::numeric_limits<int>::lowest() )
82 return std::numeric_limits<int>::lowest();
99template <
typename fp_type,
typename ret_type =
int>
100constexpr ret_type
KiROUND( fp_type v,
bool aQuiet =
false )
102 using max_ret =
long long int;
103 fp_type ret = v < 0 ? v - 0.5 : v + 0.5;
105 if( ret > std::numeric_limits<ret_type>::max() )
112 return std::numeric_limits<ret_type>::max() - 1;
114 else if( ret < std::numeric_limits<ret_type>::lowest() )
121 if( std::numeric_limits<ret_type>::is_signed )
122 return std::numeric_limits<ret_type>::lowest() + 1;
126#if __cplusplus >= 202302L
127 else if constexpr( std::is_floating_point_v<fp_type> )
129 if( std::isnan( v ) )
141 return ret_type( max_ret( ret ) );
144#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
145 _Pragma(
"GCC diagnostic pop" )
153T
rescale( T aNumerator, T aValue, T aDenominator )
155 return aNumerator * aValue / aDenominator;
161 return ( T( 0 ) < val) - ( val < T( 0 ) );
166int rescale(
int aNumerator,
int aValue,
int aDenominator );
169int64_t
rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
181typename std::enable_if<std::is_floating_point<T>::value,
bool>::type
182equals( T aFirst, T aSecond, T aEpsilon = std::numeric_limits<T>::epsilon() )
184 const T diff =
std::abs( aFirst - aSecond );
186 if( diff < aEpsilon )
193 T largest = aFirst > aSecond ? aFirst : aSecond;
195 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 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 ret_type KiCheckedCast(in_type v)
Perform a cast between numerical types.