KiCad PCB EDA Suite
Loading...
Searching...
No Matches
util.h File Reference
#include <config.h>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <limits>
#include <typeinfo>
#include <type_traits>

Go to the source code of this file.

Functions

void kimathLogDebug (const char *aFormatString,...)
 Helper to avoid directly including wx/log.h for the templated functions in kimath.
 
void kimathLogOverflow (double v, const char *aTypeName)
 Workaround to avoid the empty-string conversion issue in wxWidgets.
 
template<typename in_type = long long int, typename ret_type = int>
constexpr ret_type KiCheckedCast (in_type v)
 Perform a cast between numerical types.
 
template<typename fp_type , typename ret_type = int>
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".
 
template<typename T >
rescale (T aNumerator, T aValue, T aDenominator)
 Scale a number (value) by rational (numerator/denominator).
 
template<typename T >
constexpr int sign (T val)
 
template<>
int rescale (int aNumerator, int aValue, int aDenominator)
 
template<>
int64_t rescale (int64_t aNumerator, int64_t aValue, int64_t aDenominator)
 
template<class T >
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.
 

Function Documentation

◆ equals()

template<class T >
std::enable_if< std::is_floating_point< T >::value, bool >::type equals ( aFirst,
aSecond,
aEpsilon = std::numeric_limits<T>::epsilon() 
)

Template to compare two floating point values for equality within a required epsilon.

Parameters
aFirstvalue to compare.
aSecondvalue to compare.
aEpsilonallowed error.
Returns
true if the values considered equal within the specified epsilon, otherwise false.

Definition at line 182 of file util.h.

References std::abs().

◆ KiCheckedCast()

template<typename in_type = long long int, typename ret_type = int>
constexpr ret_type KiCheckedCast ( in_type  v)
inlineconstexpr

Perform a cast between numerical types.

Will clamp the return value to numerical type limits.

In Debug build an assert fires if will not fit into the return type.

Definition at line 68 of file util.h.

References kimathLogOverflow(), and name.

◆ kimathLogDebug()

void kimathLogDebug ( const char *  aFormatString,
  ... 
)

Helper to avoid directly including wx/log.h for the templated functions in kimath.

Definition at line 44 of file util.cpp.

Referenced by rescale().

◆ kimathLogOverflow()

void kimathLogOverflow ( double  v,
const char *  aTypeName 
)

Workaround to avoid the empty-string conversion issue in wxWidgets.

Definition at line 58 of file util.cpp.

Referenced by KiCheckedCast(), and KiROUND().

◆ KiROUND()

template<typename fp_type , typename ret_type = int>
constexpr ret_type KiROUND ( fp_type  v,
bool  aQuiet = false 
)
constexpr

Round a floating point number to an integer using "round halfway cases away from zero".

In Debug build an assert fires if will not fit into the return type.

Definition at line 100 of file util.h.

References kimathLogOverflow(), and name.

◆ rescale() [1/3]

template<>
int rescale ( int  aNumerator,
int  aValue,
int  aDenominator 
)

Definition at line 66 of file util.cpp.

◆ rescale() [2/3]

template<>
int64_t rescale ( int64_t  aNumerator,
int64_t  aValue,
int64_t  aDenominator 
)

Definition at line 80 of file util.cpp.

References std::abs(), kimathLogDebug(), and sign().

◆ rescale() [3/3]

◆ sign()