33 #include <vnl/vnl_math.h>
40 #include <vxl_version.h>
41 #if VXL_VERSION_DATE_FULL <= 20121114
42 # error "VXL version must support vnl_math:: namespace versions of functions"
106 #define itkTemplateFloatingToIntegerMacro(name) \
107 template< typename TReturn, typename TInput > \
108 inline TReturn name(TInput x) \
111 if ( sizeof( TReturn ) <= 4 ) \
113 return static_cast< TReturn >( Detail::name##_32(x) ); \
115 else if ( sizeof( TReturn ) <= 8 ) \
117 return static_cast< TReturn >( Detail::name##_64(x) ); \
121 return static_cast< TReturn >( Detail::name##_base< TReturn, TInput >(x) ); \
178 template<
typename TReturn,
typename TInput >
179 inline TReturn
Round(TInput x) {
return RoundHalfIntegerUp< TReturn, TInput >(x); }
207 #undef itkTemplateFloatingToIntegerMacro
209 template<
typename TReturn,
typename TInput >
212 #ifdef ITK_USE_CONCEPT_CHECKING
215 #endif // ITK_USE_CONCEPT_CHECKING
217 TReturn ret =
static_cast< TReturn
>( x );
218 if (
sizeof( TReturn ) >
sizeof( TInput )
225 else if (
sizeof( TReturn ) >=
sizeof( TInput ) )
233 else if ( static_cast< TInput >( ret ) != x
249 template <
typename T>
250 inline typename Detail::FloatIEEE<T>::IntType
265 template <
typename T>
271 return representOutput.
asFloat;
304 template <
typename T>
312 const T absDifference =
std::abs(x1 - x2);
313 if ( absDifference <= maxAbsoluteDifference )
318 #if defined(__APPLE__) && (__clang_major__ == 3) && (__clang_minor__ == 0) && defined(NDEBUG) && defined(__x86_64__)
321 double x1fAsULP =
static_cast<double>(x1f.
AsULP());
322 double x2fAsULP =
static_cast<double>(x2f.
AsULP());
323 double ulps = x1fAsULP - x2fAsULP;
328 return ulps <= static_cast<double>(maxUlps);
336 return ulps <= maxUlps;
351 template <
typename TFloatType1,
typename TFloatType2>
354 return FloatAlmostEqual<double>(x1, x2);
357 template <
typename TFloatType1,
typename TFloatType2>
361 return FloatAlmostEqual<double>(x1, x2);
364 template <
typename TFloatType1,
typename TFloatType2>
368 return FloatAlmostEqual<float>(x1, x2);
371 template <
typename TFloatType1,
typename TFloatType2>
375 return FloatAlmostEqual<float>(x1, x2);
378 template <
typename TFloatType1,
typename TFloatType2>
382 return FloatAlmostEqual<float>(x1, x2);
388 template <
typename TFloatType,
typename TIntType>
391 return FloatAlmostEqual<TFloatType> (floatingVariable, integerVariable);
397 template <
typename TIntType,
typename TFloatType>
406 template <
typename TSignedInt,
typename TUn
signedInt>
409 if(signedVariable < 0)
return false;
411 return signedVariable ==
static_cast< TSignedInt
>(unsignedVariable);
417 template <
typename TUn
signedInt,
typename TSignedInt>
426 template <
typename TIntegerType1,
typename TIntegerType2>
436 template<
bool TInput1IsIntger,
bool TInput1IsSigned,
bool TInput2IsInteger,
bool TInput2IsSigned>
451 struct AlmostEqualsFunctionSelector <false, true, true, true>
458 struct AlmostEqualsFunctionSelector <false, true, true,false>
465 struct AlmostEqualsFunctionSelector <true, false, false, true>
472 struct AlmostEqualsFunctionSelector <true, true, false, true>
479 struct AlmostEqualsFunctionSelector<true, true, true, false>
486 struct AlmostEqualsFunctionSelector<true, false, true, true>
493 struct AlmostEqualsFunctionSelector<true, true, true, true>
500 struct AlmostEqualsFunctionSelector<true, false, true, false>
508 template<
typename TInputType1,
typename TInputType2>
509 struct AlmostEqualsScalarImplementer
516 typedef typename AlmostEqualsFunctionSelector< TInputType1IsInteger, TInputType1IsSigned,
517 TInputType2IsInteger, TInputType2IsSigned >::SelectedVersion SelectedVersion;
523 template <
typename TScalarType1,
typename TScalarType2>
525 AlmostEqualsScalarComparer( TScalarType1 x1, TScalarType2 x2 )
527 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion:: template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
534 struct AlmostEqualsScalarVsScalar
536 template <
typename TScalarType1,
typename TScalarType2>
538 AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
540 return AlmostEqualsScalarComparer(x1, x2);
546 struct AlmostEqualsComplexVsComplex
548 template <
typename TComplexType1,
typename TComplexType2>
550 AlmostEqualsFunction(TComplexType1 x1, TComplexType2 x2)
552 return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer( x1.imag(), x2.imag() );
559 struct AlmostEqualsScalarVsComplex
561 template <
typename TScalarType,
typename TComplexType>
563 AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
569 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
573 struct AlmostEqualsComplexVsScalar
575 template <
typename TComplexType,
typename TScalarType>
577 AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
579 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
586 template <
bool T1IsComplex,
bool T2IsComplex >
587 struct AlmostEqualsComplexChooser
589 typedef AlmostEqualsScalarVsScalar ChosenVersion;
593 struct AlmostEqualsComplexChooser< true, true >
595 typedef AlmostEqualsComplexVsComplex ChosenVersion;
599 struct AlmostEqualsComplexChooser< false, true >
601 typedef AlmostEqualsScalarVsComplex ChosenVersion;
605 struct AlmostEqualsComplexChooser< true, false>
607 typedef AlmostEqualsComplexVsScalar ChosenVersion;
615 template <
typename T1,
typename T2>
616 struct AlmostEqualsComplexImplementer
618 static ITK_CONSTEXPR_VAR
bool T1IsComplex = NumericTraits< T1 >::IsComplex;
619 static ITK_CONSTEXPR_VAR
bool T2IsComplex = NumericTraits< T2 >::IsComplex;
621 typedef typename AlmostEqualsComplexChooser< T1IsComplex, T2IsComplex >::ChosenVersion ChosenVersion;
670 template <
typename T1,
typename T2>
674 return Detail::AlmostEqualsComplexImplementer<T1,T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
678 template <
typename T1,
typename T2>
708 template <
typename TInput1,
typename TInput2>
713 CLANG_SUPPRESS_Wfloat_equal
719 template <
typename TInput1,
typename TInput2>
731 ITKCommon_EXPORT
bool IsPrime(
unsigned short n );
732 ITKCommon_EXPORT
bool IsPrime(
unsigned int n );
733 ITKCommon_EXPORT
bool IsPrime(
unsigned long n );
734 ITKCommon_EXPORT
bool IsPrime(
unsigned long long n );
750 #if ITK_COMPILED_CXX_STANDARD_VERSION >= 201103L
756 #define ITK_PERFECT_FORWARD_MACRO(new_name, old_name) \
757 template <typename... TArgs> \
758 auto new_name(TArgs&&... args) -> decltype(old_name(std::forward<TArgs>(args)...)) { \
759 return old_name(std::forward<TArgs>(args)...); \
766 ITK_PERFECT_FORWARD_MACRO(isnormal,std::isnormal);
786 #undef ITK_PERFECT_FORWARD_MACRO
792 template<
typename T> T
cbrt(
const T value) {
return vnl_math::cuberoot(value); }
806 inline bool abs(
const bool x) {
return x; }
807 inline unsigned char abs(
const unsigned char x) {
return x; }
811 inline unsigned short abs(
const unsigned short x) {
return x; }
813 inline unsigned int abs(
const unsigned int x) {
return x; }
815 inline unsigned long abs(
const unsigned long x) {
return x; }
816 #if VCL_HAS_LONG_LONG
817 inline unsigned long long abs(
const long long x) {
return vnl_math::abs(x); }
818 inline unsigned long long abs(
const unsigned long long x) {
return x; }
822 inline long double abs(
const long double x) {
return std::abs(x); }
829 #if VCL_HAS_LONG_LONG
831 inline unsigned long long sqr(
const unsigned long long x) {
return vnl_math::sqr(x); }
841 #if VCL_HAS_LONG_LONG
843 inline unsigned long long cube(
const unsigned long long x) {
return vnl_math::cube(x); }
854 #if VCL_HAS_LONG_LONG
862 #endif //If not C++11 features
867 #endif // end of itkMath.h
static bool AlmostEqualsFunction(double x1, float x2)
static bool AlmostEqualsFunction(TSignedInt signedVariable, TUnsignedInt unsignedVariable)
static bool AlmostEqualsFunction(float x1, double x2)
static ITK_CONSTEXPR_VAR double sqrt2
TReturn Round(TInput x)
Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
RoundHalfIntegerUp(TInput x)
Round towards nearest integer.
bool isnan(const T value)
static ITK_CONSTEXPR_VAR double one_over_pi
static ITK_CONSTEXPR_VAR double pi_over_180
static ITK_CONSTEXPR_VAR double one_over_sqrt2pi
T remainder_truncated(const T x, const T y)
static bool AlmostEqualsFunction(TIntType integerVariable, TFloatType floatingVariable)
static ITK_CONSTEXPR_VAR double pi
Floor(TInput x)
Round towards minus infinity.
bool isinf(const T value)
static ITK_CONSTEXPR_VAR double log10e
static ITK_CONSTEXPR_VAR double ln2
static ITK_CONSTEXPR_VAR float float_eps
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potetially different types...
TReturn CastWithRangeCheck(TInput x)
ITKCommon_EXPORT unsigned short GreatestPrimeFactor(unsigned short n)
static bool IsPositive(T val)
static ITK_CONSTEXPR_VAR double two_over_pi
static ITK_CONSTEXPR_VAR double deg_per_rad
static bool AlmostEqualsFunction(TUnsignedInt unsignedVariable, TSignedInt signedVariable)
static ITK_CONSTEXPR_VAR double ln10
static ITK_CONSTEXPR_VAR double pi_over_4
static bool AlmostEqualsFunction(TFloatType floatingVariable, TIntType integerVariable)
T angle_minuspi_to_pi(const T angle)
Detail::FloatIEEE< T >::IntType FloatDifferenceULP(T x1, T x2)
Return the signed distance in ULPs (units in the last place) between two floats.
int rnd_halfinttoeven(const T x)
#define itkTemplateFloatingToIntegerMacro(name)
T FloatAddULP(T x, typename Detail::FloatIEEE< T >::IntType ulps)
Add the given ULPs (units in the last place) to a float.
T remainder_floored(const T x, const T y)
static ITK_CONSTEXPR_VAR double log2e
FloatIEEETraits< T >::IntType IntType
static bool AlmostEqualsFunction(TIntegerType1 x1, TIntegerType2 x2)
static ITK_CONSTEXPR_VAR double sqrteps
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
static ITK_CONSTEXPR_VAR double sqrt1_2
Ceil(TInput x)
Round towards plus infinity.
static bool AlmostEqualsFunction(float x1, float x2)
T angle_0_to_2pi(const T angle)
bool AlmostEquals(T1 x1, T2 x2)
Provide consistent equality checks between values of potentially different scalar or complex types...
unsigned int squared_magnitude(const char x)
bool FloatAlmostEqual(T x1, T x2, typename Detail::FloatIEEE< T >::IntType maxUlps=4, typename Detail::FloatIEEE< T >::FloatType maxAbsoluteDifference=0.1 *itk::NumericTraits< T >::epsilon())
Compare two floats and return if they are effectively equal.
static ITK_CONSTEXPR_VAR double two_over_sqrtpi
AlmostEqualsPlainOldEquals SelectedVersion
static bool AlmostEqualsFunction(TFloatType1 x1, TFloatType2 x2)
static ITK_CONSTEXPR_VAR double sqrt1_3
static ITK_CONSTEXPR_VAR double sqrt2pi
Define additional traits for native types such as int or float.
bool NotAlmostEquals(T1 x1, T2 x2)
T hypot(const T value1, const T value2)
static bool AlmostEqualsFunction(double x1, double x2)
static ITK_CONSTEXPR_VAR float float_sqrteps
static ITK_CONSTEXPR_VAR double eps
static ITK_CONSTEXPR_VAR double e
The base of the natural logarithm or Euler's number
RoundHalfIntegerToEven(TInput x)
Round towards nearest integer.
#define itkConceptMacro(name, concept)
static ITK_CONSTEXPR_VAR double pi_over_2
ITKCommon_EXPORT bool IsPrime(unsigned short n)
bool isfinite(const T value)
static ITK_CONSTEXPR_VAR double twopi
int rnd_halfintup(const T x)
static ITK_CONSTEXPR_VAR double euler
euler constant