33 #include <vnl/vnl_math.h>
40 #include <vxl_version.h>
103 #define itkTemplateFloatingToIntegerMacro(name) \
104 template< typename TReturn, typename TInput > \
105 inline TReturn name(TInput x) \
108 if ( sizeof( TReturn ) <= 4 ) \
110 return static_cast< TReturn >( Detail::name##_32(x) ); \
112 else if ( sizeof( TReturn ) <= 8 ) \
114 return static_cast< TReturn >( Detail::name##_64(x) ); \
118 return static_cast< TReturn >( Detail::name##_base< TReturn, TInput >(x) ); \
175 template<
typename TReturn,
typename TInput >
176 inline TReturn
Round(TInput x) {
return RoundHalfIntegerUp< TReturn, TInput >(x); }
204 #undef itkTemplateFloatingToIntegerMacro
206 template<
typename TReturn,
typename TInput >
209 #ifdef ITK_USE_CONCEPT_CHECKING
212 #endif // ITK_USE_CONCEPT_CHECKING
214 auto ret =
static_cast< TReturn
>( x );
215 if (
sizeof( TReturn ) >
sizeof( TInput )
216 && !( !itk::NumericTraits< TReturn >::is_signed && itk::NumericTraits< TInput >::is_signed ) )
222 else if (
sizeof( TReturn ) >=
sizeof( TInput ) )
224 if ( itk::NumericTraits< TInput >::IsPositive(x) != itk::NumericTraits< TReturn >::IsPositive(ret) )
230 else if ( static_cast< TInput >( ret ) != x
231 || ( itk::NumericTraits< TInput >::IsPositive(x) != itk::NumericTraits< TReturn >::IsPositive(ret) ) )
246 template <
typename T>
247 inline typename Detail::FloatIEEE<T>::IntType
262 template <
typename T>
268 return representOutput.
asFloat;
301 template <
typename T>
309 const T absDifference = std::abs(x1 - x2);
310 if ( absDifference <= maxAbsoluteDifference )
315 #if defined(__APPLE__) && (__clang_major__ == 3) && (__clang_minor__ == 0) && defined(NDEBUG) && defined(__x86_64__)
318 double x1fAsULP =
static_cast<double>(x1f.
AsULP());
319 double x2fAsULP =
static_cast<double>(x2f.
AsULP());
320 double ulps = x1fAsULP - x2fAsULP;
325 return ulps <= static_cast<double>(maxUlps);
333 return ulps <= maxUlps;
348 template <
typename TFloatType1,
typename TFloatType2>
351 return FloatAlmostEqual<double>(x1, x2);
354 template <
typename TFloatType1,
typename TFloatType2>
358 return FloatAlmostEqual<double>(x1, x2);
361 template <
typename TFloatType1,
typename TFloatType2>
365 return FloatAlmostEqual<float>(x1, x2);
368 template <
typename TFloatType1,
typename TFloatType2>
372 return FloatAlmostEqual<float>(x1, x2);
375 template <
typename TFloatType1,
typename TFloatType2>
379 return FloatAlmostEqual<float>(x1, x2);
385 template <
typename TFloatType,
typename TIntType>
388 return FloatAlmostEqual<TFloatType> (floatingVariable, integerVariable);
394 template <
typename TIntType,
typename TFloatType>
403 template <
typename TSignedInt,
typename TUn
signedInt>
406 if(signedVariable < 0)
return false;
407 if( unsignedVariable > static_cast< size_t >(itk::NumericTraits<TSignedInt>::max()) )
return false;
408 return signedVariable ==
static_cast< TSignedInt
>(unsignedVariable);
414 template <
typename TUn
signedInt,
typename TSignedInt>
423 template <
typename TIntegerType1,
typename TIntegerType2>
433 template<
bool TInput1IsIntger,
bool TInput1IsSigned,
bool TInput2IsInteger,
bool TInput2IsSigned>
448 struct AlmostEqualsFunctionSelector <false, true, true, true>
455 struct AlmostEqualsFunctionSelector <false, true, true,false>
462 struct AlmostEqualsFunctionSelector <true, false, false, true>
469 struct AlmostEqualsFunctionSelector <true, true, false, true>
476 struct AlmostEqualsFunctionSelector<true, true, true, false>
483 struct AlmostEqualsFunctionSelector<true, false, true, true>
490 struct AlmostEqualsFunctionSelector<true, true, true, true>
497 struct AlmostEqualsFunctionSelector<true, false, true, false>
505 template<
typename TInputType1,
typename TInputType2>
506 struct AlmostEqualsScalarImplementer
508 static constexpr
bool TInputType1IsInteger = itk::NumericTraits<TInputType1>::IsInteger;
509 static constexpr
bool TInputType1IsSigned = itk::NumericTraits<TInputType1>::IsSigned;
510 static constexpr
bool TInputType2IsInteger = itk::NumericTraits<TInputType2>::IsInteger;
511 static constexpr
bool TInputType2IsSigned = itk::NumericTraits<TInputType2>::IsSigned;
513 using SelectedVersion =
typename AlmostEqualsFunctionSelector< TInputType1IsInteger, TInputType1IsSigned,
514 TInputType2IsInteger, TInputType2IsSigned >::SelectedVersion;
520 template <
typename TScalarType1,
typename TScalarType2>
522 AlmostEqualsScalarComparer( TScalarType1 x1, TScalarType2 x2 )
524 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion:: template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
531 struct AlmostEqualsScalarVsScalar
533 template <
typename TScalarType1,
typename TScalarType2>
535 AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
537 return AlmostEqualsScalarComparer(x1, x2);
543 struct AlmostEqualsComplexVsComplex
545 template <
typename TComplexType1,
typename TComplexType2>
547 AlmostEqualsFunction(TComplexType1 x1, TComplexType2 x2)
549 return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer( x1.imag(), x2.imag() );
556 struct AlmostEqualsScalarVsComplex
558 template <
typename TScalarType,
typename TComplexType>
560 AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
562 if( !AlmostEqualsScalarComparer( complexVariable.imag(), itk::NumericTraits< typename itk::NumericTraits< TComplexType >::ValueType >::ZeroValue() ) )
566 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
570 struct AlmostEqualsComplexVsScalar
572 template <
typename TComplexType,
typename TScalarType>
574 AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
576 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
583 template <
bool T1IsComplex,
bool T2IsComplex >
584 struct AlmostEqualsComplexChooser
586 using ChosenVersion = AlmostEqualsScalarVsScalar;
590 struct AlmostEqualsComplexChooser< true, true >
592 using ChosenVersion = AlmostEqualsComplexVsComplex;
596 struct AlmostEqualsComplexChooser< false, true >
598 using ChosenVersion = AlmostEqualsScalarVsComplex;
602 struct AlmostEqualsComplexChooser< true, false>
604 using ChosenVersion = AlmostEqualsComplexVsScalar;
612 template <
typename T1,
typename T2>
613 struct AlmostEqualsComplexImplementer
618 using ChosenVersion =
typename AlmostEqualsComplexChooser< T1IsComplex, T2IsComplex >::ChosenVersion;
667 template <
typename T1,
typename T2>
671 return Detail::AlmostEqualsComplexImplementer<T1,T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
675 template <
typename T1,
typename T2>
705 template <
typename TInput1,
typename TInput2>
710 CLANG_SUPPRESS_Wfloat_equal
716 template <
typename TInput1,
typename TInput2>
728 ITKCommon_EXPORT
bool IsPrime(
unsigned short n );
729 ITKCommon_EXPORT
bool IsPrime(
unsigned int n );
730 ITKCommon_EXPORT
bool IsPrime(
unsigned long n );
731 ITKCommon_EXPORT
bool IsPrime(
unsigned long long n );
753 using vnl_math::angle_0_to_2pi;
754 using vnl_math::angle_minuspi_to_pi;
755 using vnl_math::rnd_halfinttoeven;
756 using vnl_math::rnd_halfintup;
758 using vnl_math::floor;
759 using vnl_math::ceil;
761 using vnl_math::sgn0;
762 using vnl_math::remainder_truncated;
763 using vnl_math::remainder_floored;
766 using vnl_math::cube;
767 using vnl_math::squared_magnitude;
772 #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 constexpr double pi_over_2
TReturn Round(TInput x)
Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
RoundHalfIntegerUp(TInput x)
Round towards nearest integer.
static constexpr double ln10
Define numeric traits for std::vector.
static constexpr double two_over_sqrtpi
static constexpr double euler
euler constant
static constexpr double pi_over_4
typename FloatIEEETraits< T >::IntType IntType
static constexpr double sqrt1_2
static constexpr double log2e
static constexpr double one_over_sqrt2pi
static bool AlmostEqualsFunction(TIntType integerVariable, TFloatType floatingVariable)
static constexpr double log10e
static constexpr double pi
static constexpr float float_sqrteps
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 bool AlmostEqualsFunction(TUnsignedInt unsignedVariable, TSignedInt signedVariable)
static constexpr double one_over_pi
static bool AlmostEqualsFunction(TFloatType floatingVariable, TIntType integerVariable)
Detail::FloatIEEE< T >::IntType FloatDifferenceULP(T x1, T x2)
Return the signed distance in ULPs (units in the last place) between two floats.
static constexpr double two_over_pi
#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.
static bool AlmostEqualsFunction(TIntegerType1 x1, TIntegerType2 x2)
static constexpr float float_eps
static constexpr double eps
static constexpr double ln2
static constexpr double sqrt2pi
static constexpr double deg_per_rad
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
static constexpr double twopi
Ceil(TInput x)
Round towards plus infinity.
AlmostEqualsPlainOldEquals SelectedVersion
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 constexpr double e
The base of the natural logarithm or Euler's number
static constexpr double sqrt1_3
static bool AlmostEqualsFunction(TFloatType1 x1, TFloatType2 x2)
static constexpr double sqrteps
static constexpr double pi_over_180
bool NotAlmostEquals(T1 x1, T2 x2)
static bool AlmostEqualsFunction(double x1, double x2)
RoundHalfIntegerToEven(TInput x)
Round towards nearest integer.
#define itkConceptMacro(name, concept)
static constexpr double sqrt2
ITKCommon_EXPORT bool IsPrime(unsigned short n)