47 static const double e = 2.7182818284590452354;
49 static const double log2e = 1.4426950408889634074;
51 static const double log10e = 0.43429448190325182765;
53 static const double ln2 = 0.69314718055994530942;
55 static const double ln10 = 2.30258509299404568402;
57 static const double pi = 3.14159265358979323846;
59 static const double pi_over_2 = 1.57079632679489661923;
61 static const double pi_over_4 = 0.78539816339744830962;
71 static const double sqrt2 = 1.41421356237309504880;
73 static const double sqrt1_2 = 0.70710678118654752440;
78 #define itkTemplateFloatingToIntegerMacro(name) \
79 template< typename TReturn, typename TInput > \
80 inline TReturn name(TInput x) \
83 if ( sizeof( TReturn ) <= 4 ) \
85 return static_cast< TReturn >( Detail::name##_32(x) ); \
87 else if ( sizeof( TReturn ) <= 8 ) \
89 return static_cast< TReturn >( Detail::name##_64(x) ); \
93 return static_cast< TReturn >( Detail::name##_base< TReturn, TInput >(x) ); \
150 template<
typename TReturn,
typename TInput >
151 inline TReturn
Round(TInput x) {
return RoundHalfIntegerUp< TReturn, TInput >(x); }
179 #undef itkTemplateFloatingToIntegerMacro
181 template<
typename TReturn,
typename TInput >
184 #ifdef ITK_USE_CONCEPT_CHECKING
187 #endif // ITK_USE_CONCEPT_CHECKING
189 TReturn ret =
static_cast< TReturn
>( x );
190 if (
sizeof( TReturn ) >
sizeof( TInput )
197 else if (
sizeof( TReturn ) >=
sizeof( TInput ) )
205 else if ( static_cast< TInput >( ret ) != x
220 template <
typename T>
221 inline typename Detail::FloatIEEE<T>::IntType
259 template <
typename T>
267 const T absDifference = std::abs(x1 - x2);
268 if ( absDifference <= maxAbsoluteDifference )
273 #if defined(__APPLE__) && (__clang_major__ == 3) && (__clang_minor__ == 0) && defined(NDEBUG) && defined(__x86_64__)
276 double x1fAsULP =
static_cast<double>(x1f.
AsULP());
277 double x2fAsULP =
static_cast<double>(x2f.
AsULP());
278 double ulps = x1fAsULP - x2fAsULP;
283 return ulps <= static_cast<double>(maxUlps);
291 return ulps <= maxUlps;
306 template <
typename TFloatType1,
typename TFloatType2>
309 return FloatAlmostEqual<double>(x1, x2);
312 template <
typename TFloatType1,
typename TFloatType2>
316 return FloatAlmostEqual<double>(x1, x2);
319 template <
typename TFloatType1,
typename TFloatType2>
323 return FloatAlmostEqual<float>(x1, x2);
326 template <
typename TFloatType1,
typename TFloatType2>
330 return FloatAlmostEqual<float>(x1, x2);
333 template <
typename TFloatType1,
typename TFloatType2>
337 return FloatAlmostEqual<float>(x1, x2);
343 template <
typename TFloatType,
typename TIntType>
346 return FloatAlmostEqual<TFloatType> (floatingVariable, integerVariable);
352 template <
typename TIntType,
typename TFloatType>
361 template <
typename TSignedInt,
typename TUn
signedInt>
364 if(signedVariable < 0)
return false;
366 return signedVariable ==
static_cast< TSignedInt
>(unsignedVariable);
372 template <
typename TUn
signedInt,
typename TSignedInt>
381 template <
typename TIntegerType1,
typename TIntegerType2>
391 template<
bool TInput1IsIntger,
bool TInput1IsSigned,
bool TInput2IsInteger,
bool TInput2IsSigned>
406 struct AlmostEqualsFunctionSelector <false, true, true, true>
413 struct AlmostEqualsFunctionSelector <false, true, true,false>
420 struct AlmostEqualsFunctionSelector <true, false, false, true>
427 struct AlmostEqualsFunctionSelector <true, true, false, true>
434 struct AlmostEqualsFunctionSelector<true, true, true, false>
441 struct AlmostEqualsFunctionSelector<true, false, true, true>
448 struct AlmostEqualsFunctionSelector<true, true, true, true>
455 struct AlmostEqualsFunctionSelector<true, false, true, false>
463 template<
typename TInputType1,
typename TInputType2>
464 struct AlmostEqualsScalarImplementer
471 typedef typename AlmostEqualsFunctionSelector< TInputType1IsInteger, TInputType1IsSigned,
472 TInputType2IsInteger, TInputType2IsSigned >::SelectedVersion SelectedVersion;
478 template <
typename TScalarType1,
typename TScalarType2>
480 AlmostEqualsScalarComparer( TScalarType1 x1, TScalarType2 x2 )
482 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion:: template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
489 struct AlmostEqualsScalarVsScalar
491 template <
typename TScalarType1,
typename TScalarType2>
493 AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
495 return AlmostEqualsScalarComparer(x1, x2);
501 struct AlmostEqualsComplexVsComplex
503 template <
typename TComplexType1,
typename TComplexType2>
505 AlmostEqualsFunction(TComplexType1 x1, TComplexType2 x2)
507 return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer( x1.imag(), x2.imag() );
514 struct AlmostEqualsScalarVsComplex
516 template <
typename TScalarType,
typename TComplexType>
518 AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
524 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
528 struct AlmostEqualsComplexVsScalar
530 template <
typename TComplexType,
typename TScalarType>
532 AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
534 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
541 template <
bool T1IsComplex,
bool T2IsComplex >
542 struct AlmostEqualsComplexChooser
544 typedef AlmostEqualsScalarVsScalar ChosenVersion;
548 struct AlmostEqualsComplexChooser< true, true >
550 typedef AlmostEqualsComplexVsComplex ChosenVersion;
554 struct AlmostEqualsComplexChooser< false, true >
556 typedef AlmostEqualsScalarVsComplex ChosenVersion;
560 struct AlmostEqualsComplexChooser< true, false>
562 typedef AlmostEqualsComplexVsScalar ChosenVersion;
570 template <
typename T1,
typename T2>
571 struct AlmostEqualsComplexImplementer
573 static const bool T1IsComplex = NumericTraits< T1 >::IsComplex;
574 static const bool T2IsComplex = NumericTraits< T2 >::IsComplex;
576 typedef typename AlmostEqualsComplexChooser< T1IsComplex, T2IsComplex >::ChosenVersion ChosenVersion;
625 template <
typename T1,
typename T2>
629 return Detail::AlmostEqualsComplexImplementer<T1,T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
633 template <
typename T1,
typename T2>
663 template <
typename TInput1,
typename TInput2>
668 CLANG_SUPPRESS_Wfloat_equal
674 template <
typename TInput1,
typename TInput2>
686 ITKCommon_EXPORT
bool IsPrime(
unsigned short n );
687 ITKCommon_EXPORT
bool IsPrime(
unsigned int n );
688 ITKCommon_EXPORT
bool IsPrime(
unsigned long n );
689 ITKCommon_EXPORT
bool IsPrime(
unsigned long long n );
702 #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 const double two_over_pi
TReturn Round(TInput x)
Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
RoundHalfIntegerUp(TInput x)
Round towards nearest integer.
static bool AlmostEqualsFunction(TIntType integerVariable, TFloatType floatingVariable)
static const double two_over_sqrtpi
static const double one_over_sqrt2pi
Floor(TInput x)
Round towards minus infinity.
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 const double e
The base of the natural logarithm or Euler's number
static bool IsPositive(T val)
static bool AlmostEqualsFunction(TUnsignedInt unsignedVariable, TSignedInt signedVariable)
static bool AlmostEqualsFunction(TFloatType floatingVariable, TIntType integerVariable)
static const double pi_over_2
Detail::FloatIEEE< T >::IntType FloatDifferenceULP(T x1, T x2)
Return the signed distance in ULPs (units in the last place) between two floats.
#define itkTemplateFloatingToIntegerMacro(name)
static const double sqrt2
static const double log10e
FloatIEEETraits< T >::IntType IntType
static bool AlmostEqualsFunction(TIntegerType1 x1, TIntegerType2 x2)
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
static const double log2e
Ceil(TInput x)
Round towards plus infinity.
static const double one_over_pi
static bool AlmostEqualsFunction(float x1, float x2)
bool AlmostEquals(T1 x1, T2 x2)
Provide consistent equality checks between values of potentially different scalar or complex types...
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 const double pi_over_4
AlmostEqualsPlainOldEquals SelectedVersion
static bool AlmostEqualsFunction(TFloatType1 x1, TFloatType2 x2)
Define additional traits for native types such as int or float.
bool NotAlmostEquals(T1 x1, T2 x2)
static const double sqrt1_2
static bool AlmostEqualsFunction(double x1, double x2)
RoundHalfIntegerToEven(TInput x)
Round towards nearest integer.
#define itkConceptMacro(name, concept)
ITKCommon_EXPORT bool IsPrime(unsigned short n)