ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkMath.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18/*=========================================================================
19 *
20 * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21 *
22 * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23 *
24 * For complete copyright, license and disclaimer of warranty information
25 * please refer to the NOTICE file at the top of the ITK source tree.
26 *
27 *=========================================================================*/
28#ifndef itkMath_h
29#define itkMath_h
30
31#include <cassert>
32#include <complex>
33#include <cmath>
34#include <type_traits>
35#include "itkMathDetail.h"
36#include "itkConceptChecking.h"
37#include <vnl/vnl_math.h>
38
39/* Only maintain backwards compatibility with old versions
40 * of VXL back to the point where vnl_math:: was introduced
41 * versions of VXL where only vnl_math_ was available are not
42 * supported.
43 */
44#include <vxl_version.h>
45
46namespace itk::Math
47{
48// These constants originate from VXL's vnl_math.h. They have been
49// moved here to improve visibility, and to ensure that the constants
50// are available during compile time ( as opposed to static ITK_CONSTEXPR
51// member variables ).
52
53
55static constexpr double e = vnl_math::e;
57static constexpr double log2e = vnl_math::log2e;
59static constexpr double log10e = vnl_math::log10e;
61static constexpr double ln2 = vnl_math::ln2;
63static constexpr double ln10 = vnl_math::ln10;
65static constexpr double pi = vnl_math::pi;
67static constexpr double twopi = vnl_math::twopi;
69static constexpr double pi_over_2 = vnl_math::pi_over_2;
71static constexpr double pi_over_4 = vnl_math::pi_over_4;
73static constexpr double pi_over_180 = vnl_math::pi_over_180;
75static constexpr double one_over_pi = vnl_math::one_over_pi;
77static constexpr double two_over_pi = vnl_math::two_over_pi;
79static constexpr double deg_per_rad = vnl_math::deg_per_rad;
81static constexpr double sqrt2pi = vnl_math::sqrt2pi;
83static constexpr double two_over_sqrtpi = vnl_math::two_over_sqrtpi;
85static constexpr double one_over_sqrt2pi = vnl_math::one_over_sqrt2pi;
87static constexpr double sqrt2 = vnl_math::sqrt2;
89static constexpr double sqrt1_2 = vnl_math::sqrt1_2;
91static constexpr double sqrt1_3 = vnl_math::sqrt1_3;
93static constexpr double euler = vnl_math::euler;
94
95//: IEEE double machine precision
96static constexpr double eps = vnl_math::eps;
97static constexpr double sqrteps = vnl_math::sqrteps;
98//: IEEE single machine precision
99static constexpr float float_eps = vnl_math::float_eps;
100static constexpr float float_sqrteps = vnl_math::float_sqrteps;
101
105#define itkTemplateFloatingToIntegerMacro(name) \
106 template <typename TReturn, typename TInput> \
107 inline constexpr auto name(TInput x) \
108 { \
109 if constexpr (sizeof(TReturn) <= 4) \
110 { \
111 return static_cast<TReturn>(Detail::name##_32(x)); \
112 } \
113 else if constexpr (sizeof(TReturn) <= 8) \
114 { \
115 return static_cast<TReturn>(Detail::name##_64(x)); \
116 } \
117 else \
118 { \
119 return static_cast<TReturn>(Detail::name##_base<TReturn, TInput>(x)); \
120 } \
121 }
122
143
144
167
175template <typename TReturn, typename TInput>
176inline constexpr auto
177Round(TInput x)
178{
180}
181
195
196
207
208#undef itkTemplateFloatingToIntegerMacro
209
210template <typename TReturn, typename TInput>
211inline auto
213{
214 itkConceptMacro(OnlyDefinedForIntegerTypes1, (itk::Concept::IsInteger<TReturn>));
215 itkConceptMacro(OnlyDefinedForIntegerTypes2, (itk::Concept::IsInteger<TInput>));
216
217 auto ret = static_cast<TReturn>(x);
218 if constexpr (sizeof(TReturn) > sizeof(TInput) &&
220 {
221 // if the output type is bigger and we are not converting a signed
222 // integer to an unsigned integer then we have no problems
223 return ret;
224 }
225 else if constexpr (sizeof(TReturn) >= sizeof(TInput))
226 {
228 {
229 itk::RangeError _e(__FILE__, __LINE__);
230 throw _e;
231 }
232 }
233 else if (static_cast<TInput>(ret) != x ||
235 {
236 itk::RangeError _e(__FILE__, __LINE__);
237 throw _e;
238 }
239 return ret;
240}
241
249template <typename T>
250inline constexpr typename Detail::FloatIEEE<T>::IntType
252{
253 const Detail::FloatIEEE<T> x1f(x1);
254 const Detail::FloatIEEE<T> x2f(x2);
255 return x1f.AsULP() - x2f.AsULP();
256}
257
265template <typename T>
266inline constexpr T
268{
269 const Detail::FloatIEEE<T> representInput(x);
270 const Detail::FloatIEEE<T> representOutput(representInput.asInt + ulps);
271 return representOutput.asFloat;
272}
273
304template <typename T>
305inline bool
307 T x2,
308 typename Detail::FloatIEEE<T>::IntType maxUlps = 4,
309 typename Detail::FloatIEEE<T>::FloatType maxAbsoluteDifference = 0.1 *
311{
312 // Check if the numbers are really close -- needed
313 // when comparing numbers near zero.
314 const T absDifference = std::abs(x1 - x2);
315 if (absDifference <= maxAbsoluteDifference)
316 {
317 return true;
318 }
319
320 // This check for different signs is necessary for several reasons, see the blog link above.
321 // Subtracting the signed-magnitude representation of floats using twos-complement
322 // math isn't particularly meaningful, and the subtraction would produce a 33-bit
323 // result and overflow an int.
324 if (std::signbit(x1) != std::signbit(x2))
325 {
326 return false;
327 }
328
329 const typename Detail::FloatIEEE<T>::IntType ulps = std::abs(FloatDifferenceULP(x1, x2));
330 return ulps <= maxUlps;
331}
332
333// The following code cannot be moved to the itkMathDetail.h file without introducing circular dependencies
334namespace Detail // The Detail namespace holds the templates used by AlmostEquals
335{
336// The following structs and templates are used to choose
337// which version of the AlmostEquals function
338// should be implemented base on input parameter types
339
340// Structs for choosing AlmostEquals function
341
343{
344 template <typename TFloatType1, typename TFloatType2>
345 static bool
346 AlmostEqualsFunction(TFloatType1 x1, TFloatType2 x2)
347 {
348 return FloatAlmostEqual<double>(x1, x2);
349 }
350
351 template <typename TFloatType1, typename TFloatType2>
352 static bool
353 AlmostEqualsFunction(double x1, double x2)
354 {
355 return FloatAlmostEqual<double>(x1, x2);
356 }
357
358 template <typename TFloatType1, typename TFloatType2>
359 static bool
360 AlmostEqualsFunction(double x1, float x2)
361 {
362 return FloatAlmostEqual<float>(x1, x2);
363 }
364
365 template <typename TFloatType1, typename TFloatType2>
366 static bool
367 AlmostEqualsFunction(float x1, double x2)
368 {
369 return FloatAlmostEqual<float>(x1, x2);
370 }
371
372 template <typename TFloatType1, typename TFloatType2>
373 static bool
374 AlmostEqualsFunction(float x1, float x2)
375 {
376 return FloatAlmostEqual<float>(x1, x2);
377 }
378};
379
381{
382 template <typename TFloatType, typename TIntType>
383 static bool
384 AlmostEqualsFunction(TFloatType floatingVariable, TIntType integerVariable)
385 {
386 return FloatAlmostEqual<TFloatType>(floatingVariable, integerVariable);
387 }
388};
389
391{
392 template <typename TIntType, typename TFloatType>
393 static bool
394 AlmostEqualsFunction(TIntType integerVariable, TFloatType floatingVariable)
395 {
396 return AlmostEqualsFloatVsInteger::AlmostEqualsFunction(floatingVariable, integerVariable);
397 }
398};
399
401{
402 template <typename TSignedInt, typename TUnsignedInt>
403 static bool
404 AlmostEqualsFunction(TSignedInt signedVariable, TUnsignedInt unsignedVariable)
405 {
406 if (signedVariable < 0)
407 {
408 return false;
409 }
410 if (unsignedVariable > static_cast<size_t>(itk::NumericTraits<TSignedInt>::max()))
411 {
412 return false;
413 }
414 return signedVariable == static_cast<TSignedInt>(unsignedVariable);
415 }
416};
417
419{
420 template <typename TUnsignedInt, typename TSignedInt>
421 static bool
422 AlmostEqualsFunction(TUnsignedInt unsignedVariable, TSignedInt signedVariable)
423 {
424 return AlmostEqualsSignedVsUnsigned::AlmostEqualsFunction(signedVariable, unsignedVariable);
425 }
426};
427
429{
430 template <typename TIntegerType1, typename TIntegerType2>
431 static bool
432 AlmostEqualsFunction(TIntegerType1 x1, TIntegerType2 x2)
433 {
434 return x1 == x2;
435 }
436};
437// end of structs that choose the specific AlmostEquals function
438
439// Selector structs, these select the correct case based on its types
440// input1 is int? input 1 is signed? input2 is int? input 2 is signed?
441template <bool TInput1IsIntger, bool TInput1IsSigned, bool TInput2IsInteger, bool TInput2IsSigned>
446
448template <>
449struct AlmostEqualsFunctionSelector<false, true, false, true>
450// floating type v floating type
451{
453};
454
455template <>
456struct AlmostEqualsFunctionSelector<false, true, true, true>
457// float vs int
458{
459 using SelectedVersion = AlmostEqualsFloatVsInteger;
460};
461
462template <>
463struct AlmostEqualsFunctionSelector<false, true, true, false>
464// float vs unsigned int
465{
466 using SelectedVersion = AlmostEqualsFloatVsInteger;
467};
468
469template <>
470struct AlmostEqualsFunctionSelector<true, false, false, true>
471// unsigned int vs float
472{
473 using SelectedVersion = AlmostEqualsIntegerVsFloat;
474};
475
476template <>
477struct AlmostEqualsFunctionSelector<true, true, false, true>
478// int vs float
479{
480 using SelectedVersion = AlmostEqualsIntegerVsFloat;
481};
482
483template <>
484struct AlmostEqualsFunctionSelector<true, true, true, false>
485// signed vs unsigned
486{
487 using SelectedVersion = AlmostEqualsSignedVsUnsigned;
488};
489
490template <>
491struct AlmostEqualsFunctionSelector<true, false, true, true>
492// unsigned vs signed
493{
494 using SelectedVersion = AlmostEqualsUnsignedVsSigned;
495};
496
497template <>
498struct AlmostEqualsFunctionSelector<true, true, true, true>
499// signed vs signed
500{
501 using SelectedVersion = AlmostEqualsPlainOldEquals;
502};
503
504template <>
505struct AlmostEqualsFunctionSelector<true, false, true, false>
506// unsigned vs unsigned
507{
508 using SelectedVersion = AlmostEqualsPlainOldEquals;
509};
510// end of AlmostEqualsFunctionSelector structs
511
512// The implementor tells the selector what to do
513template <typename TInputType1, typename TInputType2>
514struct AlmostEqualsScalarImplementer
515{
516 static constexpr bool TInputType1IsInteger = std::is_integral_v<TInputType1>;
517 static constexpr bool TInputType1IsSigned = std::is_signed_v<TInputType1>;
518 static constexpr bool TInputType2IsInteger = std::is_integral_v<TInputType2>;
519 static constexpr bool TInputType2IsSigned = std::is_signed_v<TInputType2>;
520
521 using SelectedVersion = typename AlmostEqualsFunctionSelector<TInputType1IsInteger,
522 TInputType1IsSigned,
523 TInputType2IsInteger,
524 TInputType2IsSigned>::SelectedVersion;
525};
526
527// The AlmostEqualsScalarComparer returns the result of an
528// approximate comparison between two scalar values of
529// potentially different data types.
530template <typename TScalarType1, typename TScalarType2>
531inline bool
532AlmostEqualsScalarComparer(TScalarType1 x1, TScalarType2 x2)
533{
534 return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion::
535 template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
536}
537
538// The following structs are used to evaluate approximate comparisons between
539// complex and scalar values of potentially different types.
540
541// Comparisons between scalar types use the AlmostEqualsScalarComparer function.
542struct AlmostEqualsScalarVsScalar
543{
544 template <typename TScalarType1, typename TScalarType2>
545 static bool
546 AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
547 {
548 return AlmostEqualsScalarComparer(x1, x2);
549 }
550};
551
552// Comparisons between two complex values compare the real and imaginary components
553// separately with the AlmostEqualsScalarComparer function.
554struct AlmostEqualsComplexVsComplex
555{
556 template <typename TComplexType1, typename TComplexType2>
557 static bool
558 AlmostEqualsFunction(TComplexType1 x1, TComplexType2 x2)
559 {
560 return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer(x1.imag(), x2.imag());
561 }
562};
563
564// Comparisons between complex and scalar values first check to see if the imaginary component
565// of the complex value is approximately 0. Then a ScalarComparison is done between the real
566// part of the complex number and the scalar value.
567struct AlmostEqualsScalarVsComplex
568{
569 template <typename TScalarType, typename TComplexType>
570 static bool
571 AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
572 {
573 if (!AlmostEqualsScalarComparer(complexVariable.imag(), typename itk::NumericTraits<TComplexType>::ValueType{}))
574 {
575 return false;
576 }
577 return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
578 }
579};
580
581struct AlmostEqualsComplexVsScalar
582{
583 template <typename TComplexType, typename TScalarType>
584 static bool
585 AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
586 {
587 return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
588 }
589};
590
591// The AlmostEqualsComplexChooser structs choose the correct case
592// from the input parameter types' IsComplex property
593// The default case is scalar vs scalar
594template <bool T1IsComplex, bool T2IsComplex> // Default is false, false
595struct AlmostEqualsComplexChooser
596{
597 using ChosenVersion = AlmostEqualsScalarVsScalar;
598};
599
600template <>
601struct AlmostEqualsComplexChooser<true, true>
602{
603 using ChosenVersion = AlmostEqualsComplexVsComplex;
604};
605
606template <>
607struct AlmostEqualsComplexChooser<false, true>
608{
609 using ChosenVersion = AlmostEqualsScalarVsComplex;
610};
611
612template <>
613struct AlmostEqualsComplexChooser<true, false>
614{
615 using ChosenVersion = AlmostEqualsComplexVsScalar;
616};
617// End of AlmostEqualsComplexChooser structs.
618
619// The AlmostEqualsComplexImplementer determines which of the input
620// parameters are complex and which are real, and sends that information
621// to the AlmostEqualsComplexChooser structs to determine the proper
622// type of evaluation.
623template <typename T1, typename T2>
624struct AlmostEqualsComplexImplementer
625{
626 static constexpr bool T1IsComplex = NumericTraits<T1>::IsComplex;
627 static constexpr bool T2IsComplex = NumericTraits<T2>::IsComplex;
628
629 using ChosenVersion = typename AlmostEqualsComplexChooser<T1IsComplex, T2IsComplex>::ChosenVersion;
630};
632
633} // end namespace Detail
634
676
677// The AlmostEquals function
678template <typename T1, typename T2>
679inline bool
680AlmostEquals(T1 x1, T2 x2)
681{
682 return Detail::AlmostEqualsComplexImplementer<T1, T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
683}
684
685// The NotAlmostEquals function
686template <typename T1, typename T2>
687inline bool
688NotAlmostEquals(T1 x1, T2 x2)
689{
690 return !AlmostEquals(x1, x2);
691}
692
693
714
715// The ExactlyEquals function
716template <typename TInput1, typename TInput2>
717inline constexpr bool
718ExactlyEquals(const TInput1 & x1, const TInput2 & x2)
719{
720 ITK_GCC_PRAGMA_PUSH
721 ITK_GCC_SUPPRESS_Wfloat_equal
722 return x1 == x2;
723 ITK_GCC_PRAGMA_POP
724}
725
726// The NotExactlyEquals function
727template <typename TInput1, typename TInput2>
728inline constexpr bool
729NotExactlyEquals(const TInput1 & x1, const TInput2 & x2)
730{
731 return !ExactlyEquals(x1, x2);
732}
733
734
740template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
741constexpr bool
743{
744 // An Introduction to the Theory of Numbers — G.H. Hardy & E.M. Wright, 6th ed., Oxford University Press.
745 // See Chapter 1 (Elementary Results on Primes). The observation that primes > 3 lie in residue classes
746 // +/1 mod 6 follows directly from eliminating multiples of 2 and 3.
747 if (n <= 1)
748 {
749 return false;
750 }
751 if (n == 2 || n == 3)
752 {
753 return true;
754 }
755 if (n % 2 == 0 || n % 3 == 0)
756 {
757 return false;
758 }
759 // 6k +/- 1, and avoid overflow via i <= n / i
760 for (T x = 5; x <= n / x; x += 6)
761 {
762 if (n % x == 0 || n % (x + 2) == 0)
763 {
764 return false;
765 }
766 }
767 return true;
768}
769
770
773template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
774constexpr T
776{
777 T v = 2;
778 while (v <= n)
779 {
780 if (n % v == 0 && IsPrime(v))
781 {
782 n /= v;
783 }
784 else
785 {
786 v += 1;
787 }
788 }
789 return v;
790}
791
792
796template <typename TReturnType = uintmax_t>
797constexpr auto
798UnsignedProduct(const uintmax_t a, const uintmax_t b) noexcept
799{
800 static_assert(std::is_unsigned_v<TReturnType>, "UnsignedProduct only supports unsigned return types");
801
802 // Note that numeric overflow is not "undefined behavior", for unsigned numbers.
803 // This function checks if the result of a*b is mathematically correct.
804 return (a == 0) || (b == 0) ||
805 (((static_cast<TReturnType>(a * b) / a) == b) && ((static_cast<TReturnType>(a * b) / b) == a))
806 ? static_cast<TReturnType>(a * b)
807 : (assert(!"UnsignedProduct overflow!"), 0);
808}
809
810
818template <typename TReturnType = uintmax_t>
819constexpr auto
820UnsignedPower(const uintmax_t base, const uintmax_t exponent) noexcept -> TReturnType
821{
822 static_assert(std::is_unsigned_v<TReturnType>, "UnsignedPower only supports unsigned return types");
823
824 // Uses recursive function calls because C++11 does not support other ways of
825 // iterations for a constexpr function.
826 return (exponent == 0) ? (assert(base > 0), 1)
827 : (exponent == 1) ? base
829 UnsignedPower<TReturnType>(base, (exponent + 1) / 2));
830}
831
832
833/*==========================================
834 * Alias the vnl_math functions in the itk::Math
835 * namespace. If possible, use the std:: equivalents
836 */
837using std::isnan;
838using std::isinf;
839using std::isfinite;
840using std::isnormal;
841using std::cbrt;
842using std::hypot;
843using vnl_math::angle_0_to_2pi;
844using vnl_math::angle_minuspi_to_pi;
845using vnl_math::rnd_halfinttoeven;
846using vnl_math::rnd_halfintup;
847using vnl_math::rnd;
848using vnl_math::floor;
849using vnl_math::ceil;
850using vnl_math::sgn;
851using vnl_math::sgn0;
852using vnl_math::remainder_truncated;
853using vnl_math::remainder_floored;
854using vnl_math::sqr;
855using vnl_math::cube;
856using vnl_math::squared_magnitude;
857
858
878template <typename T>
879constexpr auto
880Absolute(T x) noexcept
881{
882 if constexpr (std::is_same_v<T, bool>)
883 {
884 // std::abs does not provide abs for bool, but ITK depends on bool support for abs.
885 return x;
886 }
887 else if constexpr (std::is_integral_v<T>)
888 {
889 if constexpr (std::is_signed_v<T>)
890 {
891 // Using promotion to std::int32_t instead of std::make_unsigned_t<T>
892 // to avoid potential overflow when T is a signed 8bit minimum (value -128)
893 // or signed 15 bit minimum (value -32768).
894 // Note that -(-128) is still -128 for an 8-bit signed char.
895 if constexpr (sizeof(T) <= sizeof(int))
896 {
897#if __cplusplus >= 202302L
898 return static_cast<std::make_unsigned_t<T>>(std::abs(static_cast<int>(x)));
899#else
900 return static_cast<std::make_unsigned_t<T>>((x < T(0)) ? -x : x);
901#endif
902 }
903 else if constexpr (sizeof(T) <= sizeof(long int))
904 {
905#if __cplusplus >= 202302L
906 return static_cast<std::make_unsigned_t<T>>(std::abs(static_cast<long int>(x)));
907#else
908 return static_cast<std::make_unsigned_t<T>>((x < T(0)) ? -x : x);
909#endif
910 }
911 else if constexpr (sizeof(T) == sizeof(long long int))
912 {
913 // NOTE: overflow is not resolved if LONG_LONG_INT_MIN value is converted
914#if __cplusplus >= 202302L
915 return static_cast<std::make_unsigned_t<T>>(std::abs(static_cast<long long int>(x)));
916#else
917 return static_cast<std::make_unsigned_t<T>>((x < T(0)) ? -x : x);
918#endif
919 }
920 }
921 else
922 { // In C++17, the std::abs() integer overloads are only for : int, long, and long long.
923 // unsigned type values std::abs() are supported through implicit type conversions to
924 // the next larger sized integer type. The 'unsigned long long' failed due to an
925 // lack of larger sized signed integer type to be promoted to.
926 // type of x | default std::abs() called
927 // uint8_t | std::abs( (int32_t) x)
928 // uint16_t | std::abs( (int32_t) x)
929 // uint32_t | std::abs( (int64_t) x)
930 // uint64_t | // compiler error
931 // This explicit override provides direct support for all unsigned integer types with type promotion.
932 return x;
933 }
934 }
935 else if constexpr (std::is_floating_point_v<T>) // floating point or complex<> types
936 { // floating point std::abs is constexpr in c++23 and later
937#if __cplusplus >= 202302L
938 return std::abs(x);
939#else
940 // Note: +0.0 and -0.0 are considered equal, according to ExactlyEquals. They are both considered equal to T{}.
941 // And they both have the same absolute value: +0.0.
942 return ExactlyEquals(x, T{}) ? T{} : (x < 0) ? -x : x;
943#endif
944 }
945}
946template <typename T>
947auto
948Absolute(const std::complex<T> & x) noexcept
949{
950 // std::abs<T>(std::complex<T>) is not constexpr in in any proposed c++ standard
951 return std::abs<T>(x);
952}
953
954#if !defined(ITK_FUTURE_LEGACY_REMOVE)
955// itk::Math::Absolute has different behavior than std::abs. The use of
956// abs() in the ITK context without namespace resolution can be confusing
957// The abs() version is provided for backwards compatibility.
958template <typename T>
959auto
960abs(T x) noexcept
961{
962 return Absolute(x);
963}
964#endif
965
966} // namespace itk::Math
967
968#endif // end of itkMath.h
Define additional traits for native types such as int or float.
static constexpr T max(const T &)
static bool IsPositive(T val)
#define itkConceptMacro(name, concept)
#define itkTemplateFloatingToIntegerMacro(name)
Definition itkMath.h:105
RoundHalfIntegerToEven(TInput x) template< TReturn
Round towards nearest integer.
constexpr T GreatestPrimeFactor(T n)
Definition itkMath.h:775
bool AlmostEquals(T1 x1, T2 x2)
Provide consistent equality checks between values of potentially different scalar or complex types.
Definition itkMath.h:680
static constexpr double sqrt2pi
Definition itkMath.h:81
constexpr auto UnsignedPower(const uintmax_t base, const uintmax_t exponent) noexcept -> TReturnType
Definition itkMath.h:820
static constexpr double sqrt1_2
Definition itkMath.h:89
static constexpr double eps
Definition itkMath.h:96
constexpr auto Absolute(T x) noexcept
Returns the absolute value of a number.
Definition itkMath.h:880
TInput TInput auto CastWithRangeCheck(TInput x)
Definition itkMath.h:212
bool NotAlmostEquals(T1 x1, T2 x2)
Definition itkMath.h:688
TInput Ceil(TInput x) template< typename TReturn
static constexpr double pi_over_2
Definition itkMath.h:69
constexpr bool IsPrime(T n)
Definition itkMath.h:742
static constexpr double one_over_sqrt2pi
Definition itkMath.h:85
static constexpr double pi_over_4
Definition itkMath.h:71
TInput RoundHalfIntegerUp(TInput x) template< typename TReturn
Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
constexpr bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potentially different types.
Definition itkMath.h:718
static constexpr double sqrteps
Definition itkMath.h:97
static constexpr double two_over_sqrtpi
Definition itkMath.h:83
static constexpr double one_over_pi
Definition itkMath.h:75
constexpr auto UnsignedProduct(const uintmax_t a, const uintmax_t b) noexcept
Definition itkMath.h:798
static constexpr double ln2
Definition itkMath.h:61
Floor(TInput x) template< TReturn
Round towards minus infinity.
static constexpr double euler
euler constant
Definition itkMath.h:93
TInput TInput constexpr auto Round(TInput x)
Definition itkMath.h:177
static constexpr float float_sqrteps
Definition itkMath.h:100
static constexpr float float_eps
Definition itkMath.h:99
static constexpr double ln10
Definition itkMath.h:63
static constexpr double twopi
Definition itkMath.h:67
static constexpr double sqrt2
Definition itkMath.h:87
constexpr bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition itkMath.h:729
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.
Definition itkMath.h:306
static constexpr double pi
Definition itkMath.h:65
static constexpr double deg_per_rad
Definition itkMath.h:79
static constexpr double two_over_pi
Definition itkMath.h:77
static constexpr double e
Definition itkMath.h:55
static constexpr double sqrt1_3
Definition itkMath.h:91
static constexpr double pi_over_180
Definition itkMath.h:73
constexpr Detail::FloatIEEE< T >::IntType FloatDifferenceULP(T x1, T x2)
Return the signed distance in ULPs (units in the last place) between two floats.
Definition itkMath.h:251
constexpr T FloatAddULP(T x, typename Detail::FloatIEEE< T >::IntType ulps)
Add the given ULPs (units in the last place) to a float.
Definition itkMath.h:267
static constexpr double log10e
Definition itkMath.h:59
static constexpr double log2e
Definition itkMath.h:57
static bool AlmostEqualsFunction(double x1, double x2)
Definition itkMath.h:353
static bool AlmostEqualsFunction(float x1, double x2)
Definition itkMath.h:367
static bool AlmostEqualsFunction(TFloatType1 x1, TFloatType2 x2)
Definition itkMath.h:346
static bool AlmostEqualsFunction(double x1, float x2)
Definition itkMath.h:360
static bool AlmostEqualsFunction(float x1, float x2)
Definition itkMath.h:374
static bool AlmostEqualsFunction(TFloatType floatingVariable, TIntType integerVariable)
Definition itkMath.h:384
AlmostEqualsPlainOldEquals SelectedVersion
Definition itkMath.h:444
static bool AlmostEqualsFunction(TIntType integerVariable, TFloatType floatingVariable)
Definition itkMath.h:394
static bool AlmostEqualsFunction(TIntegerType1 x1, TIntegerType2 x2)
Definition itkMath.h:432
static bool AlmostEqualsFunction(TSignedInt signedVariable, TUnsignedInt unsignedVariable)
Definition itkMath.h:404
static bool AlmostEqualsFunction(TUnsignedInt unsignedVariable, TSignedInt signedVariable)
Definition itkMath.h:422
constexpr IntType AsULP() const
typename FloatIEEETraits< T >::IntType IntType