53#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
54 _Pragma(
"GCC diagnostic push" ) \
55 _Pragma(
"GCC diagnostic ignored \"-Wimplicit-int-float-conversion\"" )
64template <
typename in_type =
long long int,
typename ret_type =
int>
67 if constexpr( std::is_same_v<in_type, long long int> && std::is_same_v<ret_type, int> )
69 if( v > std::numeric_limits<int>::max() )
73 return std::numeric_limits<int>::max();
75 else if( v < std::numeric_limits<int>::lowest() )
79 return std::numeric_limits<int>::lowest();
97template <
typename fp_type,
typename ret_type =
int>
98constexpr ret_type
KiROUND( fp_type v,
bool aQuiet =
false )
100 using limits = std::numeric_limits<ret_type>;
102#if __cplusplus >= 202302L
103 if constexpr( std::is_floating_point_v<fp_type> )
105 if( std::isnan( v ) )
115 long long rounded = std::llround( v );
116 long long clamped = std::clamp<long long>( rounded,
117 static_cast<long long>( limits::lowest() ),
118 static_cast<long long>( limits::max() ) );
120 if( !aQuiet && clamped != rounded )
123 return static_cast<ret_type
>( clamped );
126#ifdef HAVE_WIMPLICIT_FLOAT_CONVERSION
127 _Pragma(
"GCC diagnostic pop" )
137 return aNumerator * aValue / aDenominator;
143 return (
T( 0 ) < val) - ( val <
T( 0 ) );
148int rescale(
int aNumerator,
int aValue,
int aDenominator );
151int64_t
rescale( int64_t aNumerator, int64_t aValue, int64_t aDenominator );
163typename std::enable_if<std::is_floating_point<T>::value,
bool>::type
164equals(
T aFirst,
T aSecond,
T aEpsilon = std::numeric_limits<T>::epsilon() )
166 const T diff =
std::abs( aFirst - aSecond );
168 if( diff < aEpsilon )
175 T largest = aFirst > aSecond ? aFirst : aSecond;
177 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.