37 #ifndef VIGRA_NUMERICTRAITS_HXX
38 #define VIGRA_NUMERICTRAITS_HXX
44 #include "metaprogramming.hxx"
45 #include "sized_int.hxx"
468 template <
typename s,
typename t>
469 inline static t clamp_integer_to_unsigned(s value, t maximum) {
473 (value >= static_cast<s>(maximum) ? maximum : static_cast<t>(value));
476 template <
typename s,
typename t>
477 inline static t clamp_integer_to_signed(s value, t minimum, t maximum) {
479 value <= static_cast<s>(minimum) ?
481 (value >= static_cast<s>(maximum) ? maximum :
static_cast<t
>(value));
484 template <
typename s,
typename t>
485 inline static t clamp_float_to_unsigned(s value, t maximum) {
489 (value >= static_cast<s>(maximum) ? maximum : static_cast<t>(value + 0.5));
492 template <
typename s,
typename t>
493 inline static t clamp_float_to_signed(s value, t minimum, t maximum) {
495 return value >=
static_cast<s
>(maximum) ? maximum : static_cast<t>(value + 0.5);
497 return value <= static_cast<s>(minimum) ? minimum : static_cast<t>(value - 0.5);
502 struct Error_NumericTraits_not_specialized_for_this_case { };
503 struct Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char { };
508 typedef Error_NumericTraits_not_specialized_for_this_case Type;
509 typedef Error_NumericTraits_not_specialized_for_this_case Promote;
510 typedef Error_NumericTraits_not_specialized_for_this_case UnsignedPromote;
511 typedef Error_NumericTraits_not_specialized_for_this_case RealPromote;
512 typedef Error_NumericTraits_not_specialized_for_this_case ComplexPromote;
513 typedef Error_NumericTraits_not_specialized_for_this_case ValueType;
515 typedef Error_NumericTraits_not_specialized_for_this_case isScalar;
516 typedef Error_NumericTraits_not_specialized_for_this_case isIntegral;
517 typedef Error_NumericTraits_not_specialized_for_this_case isSigned;
518 typedef Error_NumericTraits_not_specialized_for_this_case isOrdered;
519 typedef Error_NumericTraits_not_specialized_for_this_case isComplex;
523 struct NumericTraits<char>
525 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char Type;
526 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char Promote;
527 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char UnsignedPromote;
528 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char RealPromote;
529 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char ComplexPromote;
530 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char ValueType;
532 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isScalar;
533 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isIntegral;
534 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isSigned;
535 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isOrdered;
536 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isComplex;
541 struct NumericTraits<bool>
545 typedef unsigned int UnsignedPromote;
546 typedef double RealPromote;
547 typedef std::complex<RealPromote> ComplexPromote;
548 typedef Type ValueType;
550 typedef VigraTrueType isIntegral;
551 typedef VigraTrueType isScalar;
552 typedef VigraFalseType isSigned;
553 typedef VigraTrueType isOrdered;
554 typedef VigraFalseType isComplex;
556 static bool zero() {
return false; }
557 static bool one() {
return true; }
558 static bool nonZero() {
return true; }
559 static bool min() {
return false; }
560 static bool max() {
return true; }
562 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
563 enum { minConst = false , maxConst =
true };
565 static const bool minConst =
false;
566 static const bool maxConst =
true;
569 static Promote toPromote(
bool v) {
return v ? 1 : 0; }
570 static RealPromote toRealPromote(
bool v) {
return v ? 1.0 : 0.0; }
571 static bool fromPromote(Promote v) {
572 return (v == 0) ?
false :
true;
574 static bool fromRealPromote(RealPromote v) {
575 return (v == 0.0) ?
false :
true;
581 struct NumericTraits<signed char>
583 typedef signed char Type;
585 typedef unsigned int UnsignedPromote;
586 typedef double RealPromote;
587 typedef std::complex<RealPromote> ComplexPromote;
588 typedef Type ValueType;
590 typedef VigraTrueType isIntegral;
591 typedef VigraTrueType isScalar;
592 typedef VigraTrueType isSigned;
593 typedef VigraTrueType isOrdered;
594 typedef VigraFalseType isComplex;
596 static signed char zero() {
return 0; }
597 static signed char one() {
return 1; }
598 static signed char nonZero() {
return 1; }
599 static signed char min() {
return SCHAR_MIN; }
600 static signed char max() {
return SCHAR_MAX; }
602 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
603 enum { minConst = SCHAR_MIN, maxConst = SCHAR_MIN };
605 static const signed char minConst = SCHAR_MIN;
606 static const signed char maxConst = SCHAR_MIN;
609 static Promote toPromote(
signed char v) {
return v; }
610 static RealPromote toRealPromote(
signed char v) {
return v; }
611 static signed char fromPromote(Promote v) {
612 return detail::clamp_integer_to_signed<Promote, signed char>(v, SCHAR_MIN, SCHAR_MAX);
614 static signed char fromRealPromote(RealPromote v) {
615 return detail::clamp_float_to_signed<RealPromote, signed char>(v, SCHAR_MIN, SCHAR_MAX);
620 struct NumericTraits<unsigned char>
622 typedef unsigned char Type;
624 typedef unsigned int UnsignedPromote;
625 typedef double RealPromote;
626 typedef std::complex<RealPromote> ComplexPromote;
627 typedef Type ValueType;
629 typedef VigraTrueType isIntegral;
630 typedef VigraTrueType isScalar;
631 typedef VigraFalseType isSigned;
632 typedef VigraTrueType isOrdered;
633 typedef VigraFalseType isComplex;
635 static unsigned char zero() {
return 0; }
636 static unsigned char one() {
return 1; }
637 static unsigned char nonZero() {
return 1; }
638 static unsigned char min() {
return 0; }
639 static unsigned char max() {
return UCHAR_MAX; }
641 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
642 enum { minConst = 0, maxConst = UCHAR_MAX };
644 static const unsigned char minConst = 0;
645 static const unsigned char maxConst = UCHAR_MAX;
648 static Promote toPromote(
unsigned char v) {
return v; }
649 static RealPromote toRealPromote(
unsigned char v) {
return v; }
650 static unsigned char fromPromote(Promote v) {
651 return detail::clamp_integer_to_unsigned<Promote, unsigned char>(v, UCHAR_MAX);
653 static unsigned char fromRealPromote(RealPromote v) {
654 return detail::clamp_float_to_unsigned<RealPromote, unsigned char>(v, UCHAR_MAX);
659 struct NumericTraits<short int>
661 typedef short int Type;
663 typedef unsigned int UnsignedPromote;
664 typedef double RealPromote;
665 typedef std::complex<RealPromote> ComplexPromote;
666 typedef Type ValueType;
668 typedef VigraTrueType isIntegral;
669 typedef VigraTrueType isScalar;
670 typedef VigraTrueType isSigned;
671 typedef VigraTrueType isOrdered;
672 typedef VigraFalseType isComplex;
674 static short int zero() {
return 0; }
675 static short int one() {
return 1; }
676 static short int nonZero() {
return 1; }
677 static short int min() {
return SHRT_MIN; }
678 static short int max() {
return SHRT_MAX; }
680 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
681 enum { minConst = SHRT_MIN, maxConst = SHRT_MAX };
683 static const short int minConst = SHRT_MIN;
684 static const short int maxConst = SHRT_MAX;
687 static Promote toPromote(
short int v) {
return v; }
688 static RealPromote toRealPromote(
short int v) {
return v; }
689 static short int fromPromote(Promote v) {
690 return detail::clamp_integer_to_signed<Promote, short int>(v, SHRT_MIN, SHRT_MAX);
692 static short int fromRealPromote(RealPromote v) {
693 return detail::clamp_float_to_signed<RealPromote, short int>(v, SHRT_MIN, SHRT_MAX);
698 struct NumericTraits<short unsigned int>
700 typedef short unsigned int Type;
702 typedef unsigned int UnsignedPromote;
703 typedef double RealPromote;
704 typedef std::complex<RealPromote> ComplexPromote;
705 typedef Type ValueType;
707 typedef VigraTrueType isIntegral;
708 typedef VigraTrueType isScalar;
709 typedef VigraFalseType isSigned;
710 typedef VigraTrueType isOrdered;
711 typedef VigraFalseType isComplex;
713 static short unsigned int zero() {
return 0; }
714 static short unsigned int one() {
return 1; }
715 static short unsigned int nonZero() {
return 1; }
716 static short unsigned int min() {
return 0; }
717 static short unsigned int max() {
return USHRT_MAX; }
719 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
720 enum { minConst = 0, maxConst = USHRT_MAX };
722 static const short unsigned int minConst = 0;
723 static const short unsigned int maxConst = USHRT_MAX;
726 static Promote toPromote(
short unsigned int v) {
return v; }
727 static RealPromote toRealPromote(
short unsigned int v) {
return v; }
728 static short unsigned int fromPromote(Promote v) {
729 return detail::clamp_integer_to_unsigned<Promote, short unsigned int>(v, USHRT_MAX);
731 static short unsigned int fromRealPromote(RealPromote v) {
732 return detail::clamp_float_to_unsigned<RealPromote, short unsigned int>(v, USHRT_MAX);
737 struct NumericTraits<int>
741 typedef unsigned int UnsignedPromote;
742 typedef double RealPromote;
743 typedef std::complex<RealPromote> ComplexPromote;
744 typedef Type ValueType;
746 typedef VigraTrueType isIntegral;
747 typedef VigraTrueType isScalar;
748 typedef VigraTrueType isSigned;
749 typedef VigraTrueType isOrdered;
750 typedef VigraFalseType isComplex;
752 static int zero() {
return 0; }
753 static int one() {
return 1; }
754 static int nonZero() {
return 1; }
755 static int min() {
return INT_MIN; }
756 static int max() {
return INT_MAX; }
758 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
759 enum { minConst = INT_MIN, maxConst = INT_MAX };
761 static const int minConst = INT_MIN;
762 static const int maxConst = INT_MAX;
765 static Promote toPromote(
int v) {
return v; }
766 static RealPromote toRealPromote(
int v) {
return v; }
767 static int fromPromote(Promote v) {
return v; }
768 static int fromRealPromote(RealPromote v) {
769 return detail::clamp_float_to_signed<RealPromote, int>(v, INT_MIN, INT_MAX);
774 struct NumericTraits<unsigned int>
776 typedef unsigned int Type;
777 typedef unsigned int Promote;
778 typedef unsigned int UnsignedPromote;
779 typedef double RealPromote;
780 typedef std::complex<RealPromote> ComplexPromote;
781 typedef Type ValueType;
783 typedef VigraTrueType isIntegral;
784 typedef VigraTrueType isScalar;
785 typedef VigraFalseType isSigned;
786 typedef VigraTrueType isOrdered;
787 typedef VigraFalseType isComplex;
789 static unsigned int zero() {
return 0; }
790 static unsigned int one() {
return 1; }
791 static unsigned int nonZero() {
return 1; }
792 static unsigned int min() {
return 0; }
793 static unsigned int max() {
return UINT_MAX; }
795 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
796 enum { minConst = 0, maxConst = UINT_MAX };
798 static const unsigned int minConst = 0;
799 static const unsigned int maxConst = UINT_MAX;
802 static Promote toPromote(
unsigned int v) {
return v; }
803 static RealPromote toRealPromote(
unsigned int v) {
return v; }
804 static unsigned int fromPromote(Promote v) {
return v; }
805 static unsigned int fromRealPromote(RealPromote v) {
806 return detail::clamp_float_to_unsigned<RealPromote, unsigned int>(v, UINT_MAX);
811 struct NumericTraits<long>
814 typedef long Promote;
815 typedef unsigned long UnsignedPromote;
816 typedef double RealPromote;
817 typedef std::complex<RealPromote> ComplexPromote;
818 typedef Type ValueType;
820 typedef VigraTrueType isIntegral;
821 typedef VigraTrueType isScalar;
822 typedef VigraTrueType isSigned;
823 typedef VigraTrueType isOrdered;
824 typedef VigraFalseType isComplex;
826 static long zero() {
return 0; }
827 static long one() {
return 1; }
828 static long nonZero() {
return 1; }
829 static long min() {
return LONG_MIN; }
830 static long max() {
return LONG_MAX; }
832 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
833 enum { minConst = LONG_MIN, maxConst = LONG_MAX };
835 static const long minConst = LONG_MIN;
836 static const long maxConst = LONG_MAX;
839 static Promote toPromote(
long v) {
return v; }
840 static RealPromote toRealPromote(
long v) {
return static_cast<RealPromote
>(v); }
841 static long fromPromote(Promote v) {
return v; }
842 static long fromRealPromote(RealPromote v) {
843 return detail::clamp_float_to_signed<RealPromote, long>(v, LONG_MIN, LONG_MAX);
848 struct NumericTraits<unsigned long>
850 typedef unsigned long Type;
851 typedef unsigned long Promote;
852 typedef unsigned long UnsignedPromote;
853 typedef double RealPromote;
854 typedef std::complex<RealPromote> ComplexPromote;
855 typedef Type ValueType;
857 typedef VigraTrueType isIntegral;
858 typedef VigraTrueType isScalar;
859 typedef VigraFalseType isSigned;
860 typedef VigraTrueType isOrdered;
861 typedef VigraFalseType isComplex;
863 static unsigned long zero() {
return 0; }
864 static unsigned long one() {
return 1; }
865 static unsigned long nonZero() {
return 1; }
866 static unsigned long min() {
return 0; }
867 static unsigned long max() {
return ULONG_MAX; }
869 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
870 enum { minConst = 0, maxConst = ULONG_MAX };
872 static const unsigned long minConst = 0;
873 static const unsigned long maxConst = ULONG_MAX;
876 static Promote toPromote(
unsigned long v) {
return v; }
877 static RealPromote toRealPromote(
unsigned long v) {
return static_cast<RealPromote
>(v); }
878 static unsigned long fromPromote(Promote v) {
return v; }
879 static unsigned long fromRealPromote(RealPromote v) {
880 return detail::clamp_float_to_unsigned<RealPromote, unsigned long>(v, ULONG_MAX);
886 struct NumericTraits<long long>
888 typedef long long Type;
889 typedef long long Promote;
890 typedef unsigned long long UnsignedPromote;
891 typedef double RealPromote;
892 typedef std::complex<RealPromote> ComplexPromote;
893 typedef Type ValueType;
895 typedef VigraTrueType isIntegral;
896 typedef VigraTrueType isScalar;
897 typedef VigraTrueType isSigned;
898 typedef VigraTrueType isOrdered;
899 typedef VigraFalseType isComplex;
901 static long long zero() {
return 0; }
902 static long long one() {
return 1; }
903 static long long nonZero() {
return 1; }
904 static long long min() {
return LLONG_MIN; }
905 static long long max() {
return LLONG_MAX; }
907 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
908 enum { minConst = LLONG_MIN, maxConst = LLONG_MAX };
910 static const long long minConst = LLONG_MIN;
911 static const long long maxConst = LLONG_MAX;
914 static Promote toPromote(
long long v) {
return v; }
915 static RealPromote toRealPromote(
long long v) {
return (RealPromote)v; }
916 static long long fromPromote(Promote v) {
return v; }
917 static long long fromRealPromote(RealPromote v) {
918 return detail::clamp_float_to_signed<RealPromote, long long>(v, LLONG_MIN, LLONG_MAX);
923 struct NumericTraits<unsigned long long>
925 typedef unsigned long long Type;
926 typedef unsigned long long Promote;
927 typedef unsigned long long UnsignedPromote;
928 typedef double RealPromote;
929 typedef std::complex<RealPromote> ComplexPromote;
930 typedef Type ValueType;
932 typedef VigraTrueType isIntegral;
933 typedef VigraTrueType isScalar;
934 typedef VigraFalseType isSigned;
935 typedef VigraTrueType isOrdered;
936 typedef VigraFalseType isComplex;
938 static unsigned long long zero() {
return 0; }
939 static unsigned long long one() {
return 1; }
940 static unsigned long long nonZero() {
return 1; }
941 static unsigned long long min() {
return 0; }
942 static unsigned long long max() {
return ULLONG_MAX; }
944 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
945 enum { minConst = 0, maxConst = ULLONG_MAX };
947 static const unsigned long long minConst = 0;
948 static const unsigned long long maxConst = ULLONG_MAX;
951 static Promote toPromote(
unsigned long long v) {
return v; }
952 static RealPromote toRealPromote(
unsigned long long v) {
return (RealPromote)v; }
953 static unsigned long long fromPromote(Promote v) {
return v; }
954 static unsigned long long fromRealPromote(RealPromote v) {
955 return detail::clamp_float_to_unsigned<RealPromote, unsigned long long>(v, ULLONG_MAX);
961 struct NumericTraits<float>
964 typedef float Promote;
965 typedef float UnsignedPromote;
966 typedef float RealPromote;
967 typedef std::complex<RealPromote> ComplexPromote;
968 typedef Type ValueType;
970 typedef VigraFalseType isIntegral;
971 typedef VigraTrueType isScalar;
972 typedef VigraTrueType isSigned;
973 typedef VigraTrueType isOrdered;
974 typedef VigraFalseType isComplex;
976 static float zero() {
return 0.0; }
977 static float one() {
return 1.0; }
978 static float nonZero() {
return 1.0; }
979 static float epsilon() {
return FLT_EPSILON; }
980 static float smallestPositive() {
return FLT_MIN; }
981 static float min() {
return -FLT_MAX; }
982 static float max() {
return FLT_MAX; }
984 static Promote toPromote(
float v) {
return v; }
985 static RealPromote toRealPromote(
float v) {
return v; }
986 static float fromPromote(Promote v) {
return v; }
987 static float fromRealPromote(RealPromote v) {
return v; }
991 struct NumericTraits<double>
994 typedef double Promote;
995 typedef double UnsignedPromote;
996 typedef double RealPromote;
997 typedef std::complex<RealPromote> ComplexPromote;
998 typedef Type ValueType;
1000 typedef VigraFalseType isIntegral;
1001 typedef VigraTrueType isScalar;
1002 typedef VigraTrueType isSigned;
1003 typedef VigraTrueType isOrdered;
1004 typedef VigraFalseType isComplex;
1006 static double zero() {
return 0.0; }
1007 static double one() {
return 1.0; }
1008 static double nonZero() {
return 1.0; }
1009 static double epsilon() {
return DBL_EPSILON; }
1010 static double smallestPositive() {
return DBL_MIN; }
1011 static double min() {
return -DBL_MAX; }
1012 static double max() {
return DBL_MAX; }
1014 static Promote toPromote(
double v) {
return v; }
1015 static RealPromote toRealPromote(
double v) {
return v; }
1016 static double fromPromote(Promote v) {
return v; }
1017 static double fromRealPromote(RealPromote v) {
return v; }
1021 struct NumericTraits<long double>
1023 typedef long double Type;
1024 typedef long double Promote;
1025 typedef long double UnsignedPromote;
1026 typedef long double RealPromote;
1027 typedef std::complex<RealPromote> ComplexPromote;
1028 typedef Type ValueType;
1030 typedef VigraFalseType isIntegral;
1031 typedef VigraTrueType isScalar;
1032 typedef VigraTrueType isSigned;
1033 typedef VigraTrueType isOrdered;
1034 typedef VigraFalseType isComplex;
1036 static long double zero() {
return 0.0; }
1037 static long double one() {
return 1.0; }
1038 static long double nonZero() {
return 1.0; }
1039 static long double epsilon() {
return LDBL_EPSILON; }
1040 static long double smallestPositive() {
return LDBL_MIN; }
1041 static long double min() {
return -LDBL_MAX; }
1042 static long double max() {
return LDBL_MAX; }
1044 static Promote toPromote(
long double v) {
return v; }
1045 static RealPromote toRealPromote(
long double v) {
return v; }
1046 static long double fromPromote(Promote v) {
return v; }
1047 static long double fromRealPromote(RealPromote v) {
return v; }
1050 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
1053 struct NumericTraits<std::complex<T> >
1055 typedef std::complex<T> Type;
1056 typedef std::complex<typename NumericTraits<T>::Promote> Promote;
1057 typedef std::complex<typename NumericTraits<T>::UnsignedPromote> UnsignedPromote;
1058 typedef std::complex<typename NumericTraits<T>::RealPromote> RealPromote;
1059 typedef std::complex<RealPromote> ComplexPromote;
1060 typedef T ValueType;
1062 typedef VigraFalseType isIntegral;
1063 typedef VigraFalseType isScalar;
1064 typedef typename NumericTraits<T>::isSigned isSigned;
1065 typedef VigraFalseType isOrdered;
1066 typedef VigraTrueType isComplex;
1068 static Type zero() {
return Type(0.0); }
1069 static Type one() {
return Type(1.0); }
1070 static Type nonZero() {
return one(); }
1071 static Type epsilon() {
return Type(NumericTraits<T>::epsilon()); }
1072 static Type smallestPositive() {
return Type(NumericTraits<T>::smallestPositive()); }
1074 static Promote toPromote(Type
const & v) {
return v; }
1075 static Type fromPromote(Promote
const & v) {
return v; }
1076 static Type fromRealPromote(RealPromote v) {
return Type(v); }
1079 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
1088 struct SquareRootTraits
1091 typedef typename NumericTraits<T>::RealPromote SquareRootResult;
1092 typedef typename NumericTraits<T>::RealPromote SquareRootArgument;
1102 struct Error_NormTraits_not_specialized_for_this_case { };
1108 typedef Error_NormTraits_not_specialized_for_this_case SquaredNormType;
1109 typedef Error_NormTraits_not_specialized_for_this_case NormType;
1112 #define VIGRA_DEFINE_NORM_TRAITS(T) \
1113 template <> struct NormTraits<T> { \
1115 typedef NumericTraits<T>::Promote SquaredNormType; \
1116 typedef T NormType; \
1119 VIGRA_DEFINE_NORM_TRAITS(
bool)
1120 VIGRA_DEFINE_NORM_TRAITS(
signed char)
1121 VIGRA_DEFINE_NORM_TRAITS(
unsigned char)
1122 VIGRA_DEFINE_NORM_TRAITS(
short)
1123 VIGRA_DEFINE_NORM_TRAITS(
unsigned short)
1124 VIGRA_DEFINE_NORM_TRAITS(
int)
1125 VIGRA_DEFINE_NORM_TRAITS(
unsigned int)
1126 VIGRA_DEFINE_NORM_TRAITS(
long)
1127 VIGRA_DEFINE_NORM_TRAITS(
unsigned long)
1128 VIGRA_DEFINE_NORM_TRAITS(
float)
1129 VIGRA_DEFINE_NORM_TRAITS(
double)
1130 VIGRA_DEFINE_NORM_TRAITS(
long double)
1133 VIGRA_DEFINE_NORM_TRAITS(
long long)
1134 VIGRA_DEFINE_NORM_TRAITS(
unsigned long long)
1137 #undef VIGRA_DEFINE_NORM_TRAITS
1139 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
1142 struct NormTraits<std::complex<T> >
1144 typedef std::complex<T> Type;
1145 typedef typename NormTraits<T>::SquaredNormType SquaredNormType;
1146 typedef typename SquareRootTraits<SquaredNormType>::SquareRootResult NormType;
1149 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
1159 template <
class T,
class U>
1165 typedef typename SizeToType<sizeof(*typeToSize(t() + u()))>::result Promote;
1166 static Promote toPromote(T t) {
return Promote(t); }
1167 static Promote toPromote(U u) {
return Promote(u); }
1172 struct PromoteType<T, T>
1176 typedef typename SizeToType<sizeof(*typeToSize(t() + t()))>::result Promote;
1177 static Promote toPromote(T t) {
return Promote(t); }
1182 struct Error_PromoteTraits_not_specialized_for_this_case { };
1184 template<
class A,
class B>
1185 struct PromoteTraits
1187 typedef Error_PromoteTraits_not_specialized_for_this_case Promote;
1190 #include "promote_traits.hxx"
1192 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
1195 struct PromoteTraits<std::complex<T>, std::complex<T> >
1197 typedef std::complex<typename PromoteTraits<T, T>::Promote> Promote;
1198 static Promote toPromote(std::complex<T>
const & v) {
return v; }
1201 template <
class T1,
class T2>
1202 struct PromoteTraits<std::complex<T1>, std::complex<T2> >
1204 typedef std::complex<typename PromoteTraits<T1, T2>::Promote> Promote;
1205 static Promote toPromote(std::complex<T1>
const & v) {
return v; }
1206 static Promote toPromote(std::complex<T2>
const & v) {
return v; }
1209 template <
class T1,
class T2>
1210 struct PromoteTraits<std::complex<T1>, T2 >
1212 typedef std::complex<typename PromoteTraits<T1, T2>::Promote> Promote;
1213 static Promote toPromote(std::complex<T1>
const & v) {
return v; }
1214 static Promote toPromote(T2
const & v) {
return Promote(v); }
1217 template <
class T1,
class T2>
1218 struct PromoteTraits<T1, std::complex<T2> >
1220 typedef std::complex<typename PromoteTraits<T1, T2>::Promote> Promote;
1221 static Promote toPromote(T1
const & v) {
return Promote(v); }
1222 static Promote toPromote(std::complex<T2>
const & v) {
return v; }
1230 struct RequiresExplicitCast {
1232 static U
const & cast(U
const & v)
1236 #if !defined(_MSC_VER) || _MSC_VER >= 1300
1237 # define VIGRA_SPECIALIZED_CAST(type) \
1239 struct RequiresExplicitCast<type> { \
1240 static type cast(float v) \
1241 { return NumericTraits<type>::fromRealPromote(v); } \
1242 static type cast(double v) \
1243 { return NumericTraits<type>::fromRealPromote(v); } \
1244 static type cast(type v) \
1246 template <class U> \
1247 static type cast(U v) \
1248 { return static_cast<type>(v); } \
1252 # define VIGRA_SPECIALIZED_CAST(type) \
1254 struct RequiresExplicitCast<type> { \
1255 static type cast(float v) \
1256 { return NumericTraits<type>::fromRealPromote(v); } \
1257 static type cast(double v) \
1258 { return NumericTraits<type>::fromRealPromote(v); } \
1259 static type cast(signed char v) \
1261 static type cast(unsigned char v) \
1263 static type cast(short v) \
1265 static type cast(unsigned short v) \
1267 static type cast(int v) \
1269 static type cast(unsigned int v) \
1271 static type cast(long v) \
1273 static type cast(unsigned long v) \
1279 VIGRA_SPECIALIZED_CAST(
signed char)
1280 VIGRA_SPECIALIZED_CAST(
unsigned char)
1281 VIGRA_SPECIALIZED_CAST(
short)
1282 VIGRA_SPECIALIZED_CAST(
unsigned short)
1283 VIGRA_SPECIALIZED_CAST(
int)
1284 VIGRA_SPECIALIZED_CAST(
unsigned int)
1285 VIGRA_SPECIALIZED_CAST(
long)
1286 VIGRA_SPECIALIZED_CAST(
unsigned long)
1289 struct RequiresExplicitCast<
bool> {
1291 static bool cast(U v)
1292 {
return v == NumericTraits<U>::zero()
1298 struct RequiresExplicitCast<float> {
1299 static float cast(
int v)
1300 {
return (
float)v; }
1302 static float cast(
unsigned int v)
1303 {
return (
float)v; }
1305 static float cast(
long v)
1306 {
return (
float)v; }
1308 static float cast(
unsigned long v)
1309 {
return (
float)v; }
1311 static float cast(
long long v)
1312 {
return (
float)v; }
1314 static float cast(
unsigned long long v)
1315 {
return (
float)v; }
1317 static float cast(
double v)
1318 {
return (
float)v; }
1320 static float cast(
long double v)
1321 {
return (
float)v; }
1329 struct RequiresExplicitCast<double> {
1330 static double cast(
Int64 v)
1331 {
return (
double)v; }
1333 static double cast(
UInt64 v)
1334 {
return (
double)v; }
1341 #undef VIGRA_SPECIALIZED_CAST
1349 #endif // VIGRA_NUMERICTRAITS_HXX