37 #ifndef VIGRA_NUMERICTRAITS_HXX
38 #define VIGRA_NUMERICTRAITS_HXX
44 #include "metaprogramming.hxx"
45 #include "sized_int.hxx"
475 struct Error_NumericTraits_not_specialized_for_this_case { };
476 struct Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char { };
481 typedef Error_NumericTraits_not_specialized_for_this_case Type;
482 typedef Error_NumericTraits_not_specialized_for_this_case Promote;
483 typedef Error_NumericTraits_not_specialized_for_this_case UnsignedPromote;
484 typedef Error_NumericTraits_not_specialized_for_this_case RealPromote;
485 typedef Error_NumericTraits_not_specialized_for_this_case ComplexPromote;
486 typedef Error_NumericTraits_not_specialized_for_this_case ValueType;
488 typedef Error_NumericTraits_not_specialized_for_this_case isScalar;
489 typedef Error_NumericTraits_not_specialized_for_this_case isIntegral;
490 typedef Error_NumericTraits_not_specialized_for_this_case isSigned;
491 typedef Error_NumericTraits_not_specialized_for_this_case isOrdered;
492 typedef Error_NumericTraits_not_specialized_for_this_case isComplex;
496 struct NumericTraits<char>
498 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char Type;
499 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char Promote;
500 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char UnsignedPromote;
501 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char RealPromote;
502 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char ComplexPromote;
503 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char ValueType;
505 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isScalar;
506 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isIntegral;
507 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isSigned;
508 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isOrdered;
509 typedef Error_NumericTraits_char_is_not_a_numeric_type__use_signed_char_or_unsigned_char isComplex;
514 struct NumericTraits<bool>
518 typedef unsigned int UnsignedPromote;
519 typedef double RealPromote;
520 typedef std::complex<RealPromote> ComplexPromote;
521 typedef Type ValueType;
523 typedef VigraTrueType isIntegral;
524 typedef VigraTrueType isScalar;
525 typedef VigraFalseType isSigned;
526 typedef VigraTrueType isOrdered;
527 typedef VigraFalseType isComplex;
529 static bool zero() {
return false; }
530 static bool one() {
return true; }
531 static bool nonZero() {
return true; }
532 static bool min() {
return false; }
533 static bool max() {
return true; }
535 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
536 enum { minConst = false , maxConst =
true };
538 static const bool minConst =
false;
539 static const bool maxConst =
true;
542 static Promote toPromote(
bool v) {
return v ? 1 : 0; }
543 static RealPromote toRealPromote(
bool v) {
return v ? 1.0 : 0.0; }
544 static bool fromPromote(Promote v) {
545 return (v == 0) ?
false :
true;
547 static bool fromRealPromote(RealPromote v) {
548 return (v == 0.0) ?
false :
true;
554 struct NumericTraits<signed char>
556 typedef signed char Type;
558 typedef unsigned int UnsignedPromote;
559 typedef double RealPromote;
560 typedef std::complex<RealPromote> ComplexPromote;
561 typedef Type ValueType;
563 typedef VigraTrueType isIntegral;
564 typedef VigraTrueType isScalar;
565 typedef VigraTrueType isSigned;
566 typedef VigraTrueType isOrdered;
567 typedef VigraFalseType isComplex;
569 static signed char zero() {
return 0; }
570 static signed char one() {
return 1; }
571 static signed char nonZero() {
return 1; }
572 static signed char min() {
return SCHAR_MIN; }
573 static signed char max() {
return SCHAR_MAX; }
575 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
576 enum { minConst = SCHAR_MIN, maxConst = SCHAR_MIN };
578 static const signed char minConst = SCHAR_MIN;
579 static const signed char maxConst = SCHAR_MIN;
582 static Promote toPromote(
signed char v) {
return v; }
583 static RealPromote toRealPromote(
signed char v) {
return v; }
584 static signed char fromPromote(Promote v) {
585 return ((v < SCHAR_MIN) ? SCHAR_MIN : (v > SCHAR_MAX) ? SCHAR_MAX : v);
587 static signed char fromRealPromote(RealPromote v) {
589 ? ((v < (RealPromote)SCHAR_MIN)
591 : static_cast<signed char>(v - 0.5))
592 : (v > (RealPromote)SCHAR_MAX)
594 : static_cast<signed char>(v + 0.5));
599 struct NumericTraits<unsigned char>
601 typedef unsigned char Type;
603 typedef unsigned int UnsignedPromote;
604 typedef double RealPromote;
605 typedef std::complex<RealPromote> ComplexPromote;
606 typedef Type ValueType;
608 typedef VigraTrueType isIntegral;
609 typedef VigraTrueType isScalar;
610 typedef VigraFalseType isSigned;
611 typedef VigraTrueType isOrdered;
612 typedef VigraFalseType isComplex;
614 static unsigned char zero() {
return 0; }
615 static unsigned char one() {
return 1; }
616 static unsigned char nonZero() {
return 1; }
617 static unsigned char min() {
return 0; }
618 static unsigned char max() {
return UCHAR_MAX; }
620 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
621 enum { minConst = 0, maxConst = UCHAR_MAX };
623 static const unsigned char minConst = 0;
624 static const unsigned char maxConst = UCHAR_MAX;
627 static Promote toPromote(
unsigned char v) {
return v; }
628 static RealPromote toRealPromote(
unsigned char v) {
return v; }
629 static unsigned char fromPromote(Promote
const & v) {
632 : (v > (Promote)UCHAR_MAX)
636 static unsigned char fromRealPromote(RealPromote
const & v) {
637 return Type((v < 0.0)
639 : ((v > (RealPromote)UCHAR_MAX)
646 struct NumericTraits<short int>
648 typedef short int Type;
650 typedef unsigned int UnsignedPromote;
651 typedef double RealPromote;
652 typedef std::complex<RealPromote> ComplexPromote;
653 typedef Type ValueType;
655 typedef VigraTrueType isIntegral;
656 typedef VigraTrueType isScalar;
657 typedef VigraTrueType isSigned;
658 typedef VigraTrueType isOrdered;
659 typedef VigraFalseType isComplex;
661 static short int zero() {
return 0; }
662 static short int one() {
return 1; }
663 static short int nonZero() {
return 1; }
664 static short int min() {
return SHRT_MIN; }
665 static short int max() {
return SHRT_MAX; }
667 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
668 enum { minConst = SHRT_MIN, maxConst = SHRT_MAX };
670 static const short int minConst = SHRT_MIN;
671 static const short int maxConst = SHRT_MAX;
674 static Promote toPromote(
short int v) {
return v; }
675 static RealPromote toRealPromote(
short int v) {
return v; }
676 static short int fromPromote(Promote v) {
677 return ((v < SHRT_MIN) ? SHRT_MIN :
678 (v > SHRT_MAX) ? SHRT_MAX : v);
680 static short int fromRealPromote(RealPromote v) {
682 ? ((v < (RealPromote)SHRT_MIN)
684 : static_cast<short int>(v - 0.5))
685 : ((v > (RealPromote)SHRT_MAX)
687 : static_cast<short int>(v + 0.5)));
692 struct NumericTraits<short unsigned int>
694 typedef short unsigned int Type;
696 typedef unsigned int UnsignedPromote;
697 typedef double RealPromote;
698 typedef std::complex<RealPromote> ComplexPromote;
699 typedef Type ValueType;
701 typedef VigraTrueType isIntegral;
702 typedef VigraTrueType isScalar;
703 typedef VigraFalseType isSigned;
704 typedef VigraTrueType isOrdered;
705 typedef VigraFalseType isComplex;
707 static short unsigned int zero() {
return 0; }
708 static short unsigned int one() {
return 1; }
709 static short unsigned int nonZero() {
return 1; }
710 static short unsigned int min() {
return 0; }
711 static short unsigned int max() {
return USHRT_MAX; }
713 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
714 enum { minConst = 0, maxConst = USHRT_MAX };
716 static const short unsigned int minConst = 0;
717 static const short unsigned int maxConst = USHRT_MAX;
720 static Promote toPromote(
short unsigned int v) {
return v; }
721 static RealPromote toRealPromote(
short unsigned int v) {
return v; }
722 static short unsigned int fromPromote(Promote v) {
729 static short unsigned int fromRealPromote(RealPromote v) {
730 return Type((v < 0.0)
732 : ((v > (RealPromote)USHRT_MAX)
739 struct NumericTraits<int>
743 typedef unsigned int UnsignedPromote;
744 typedef double RealPromote;
745 typedef std::complex<RealPromote> ComplexPromote;
746 typedef Type ValueType;
748 typedef VigraTrueType isIntegral;
749 typedef VigraTrueType isScalar;
750 typedef VigraTrueType isSigned;
751 typedef VigraTrueType isOrdered;
752 typedef VigraFalseType isComplex;
754 static int zero() {
return 0; }
755 static int one() {
return 1; }
756 static int nonZero() {
return 1; }
757 static int min() {
return INT_MIN; }
758 static int max() {
return INT_MAX; }
760 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
761 enum { minConst = INT_MIN, maxConst = INT_MAX };
763 static const int minConst = INT_MIN;
764 static const int maxConst = INT_MAX;
767 static Promote toPromote(
int v) {
return v; }
768 static RealPromote toRealPromote(
int v) {
return v; }
769 static int fromPromote(Promote v) {
return v; }
770 static int fromRealPromote(RealPromote v) {
772 ? ((v < (RealPromote)INT_MIN)
774 : static_cast<int>(v - 0.5))
775 : ((v > (RealPromote)INT_MAX)
777 : static_cast<int>(v + 0.5)));
782 struct NumericTraits<unsigned int>
784 typedef unsigned int Type;
785 typedef unsigned int Promote;
786 typedef unsigned int UnsignedPromote;
787 typedef double RealPromote;
788 typedef std::complex<RealPromote> ComplexPromote;
789 typedef Type ValueType;
791 typedef VigraTrueType isIntegral;
792 typedef VigraTrueType isScalar;
793 typedef VigraFalseType isSigned;
794 typedef VigraTrueType isOrdered;
795 typedef VigraFalseType isComplex;
797 static unsigned int zero() {
return 0; }
798 static unsigned int one() {
return 1; }
799 static unsigned int nonZero() {
return 1; }
800 static unsigned int min() {
return 0; }
801 static unsigned int max() {
return UINT_MAX; }
803 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
804 enum { minConst = 0, maxConst = UINT_MAX };
806 static const unsigned int minConst = 0;
807 static const unsigned int maxConst = UINT_MAX;
810 static Promote toPromote(
unsigned int v) {
return v; }
811 static RealPromote toRealPromote(
unsigned int v) {
return v; }
812 static unsigned int fromPromote(Promote v) {
return v; }
813 static unsigned int fromRealPromote(RealPromote v) {
816 : ((v > (RealPromote)UINT_MAX)
818 : static_cast<unsigned int>(v + 0.5)));
823 struct NumericTraits<long>
826 typedef long Promote;
827 typedef unsigned long UnsignedPromote;
828 typedef double RealPromote;
829 typedef std::complex<RealPromote> ComplexPromote;
830 typedef Type ValueType;
832 typedef VigraTrueType isIntegral;
833 typedef VigraTrueType isScalar;
834 typedef VigraTrueType isSigned;
835 typedef VigraTrueType isOrdered;
836 typedef VigraFalseType isComplex;
838 static long zero() {
return 0; }
839 static long one() {
return 1; }
840 static long nonZero() {
return 1; }
841 static long min() {
return LONG_MIN; }
842 static long max() {
return LONG_MAX; }
844 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
845 enum { minConst = LONG_MIN, maxConst = LONG_MAX };
847 static const long minConst = LONG_MIN;
848 static const long maxConst = LONG_MAX;
851 static Promote toPromote(
long v) {
return v; }
852 static RealPromote toRealPromote(
long v) {
return v; }
853 static long fromPromote(Promote v) {
return v; }
854 static long fromRealPromote(RealPromote v) {
856 ? ((v < (RealPromote)LONG_MIN)
858 : static_cast<long>(v - 0.5))
859 : ((v > (RealPromote)LONG_MAX)
861 : static_cast<long>(v + 0.5)));
866 struct NumericTraits<unsigned long>
868 typedef unsigned long Type;
869 typedef unsigned long Promote;
870 typedef unsigned long UnsignedPromote;
871 typedef double RealPromote;
872 typedef std::complex<RealPromote> ComplexPromote;
873 typedef Type ValueType;
875 typedef VigraTrueType isIntegral;
876 typedef VigraTrueType isScalar;
877 typedef VigraFalseType isSigned;
878 typedef VigraTrueType isOrdered;
879 typedef VigraFalseType isComplex;
881 static unsigned long zero() {
return 0; }
882 static unsigned long one() {
return 1; }
883 static unsigned long nonZero() {
return 1; }
884 static unsigned long min() {
return 0; }
885 static unsigned long max() {
return ULONG_MAX; }
887 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
888 enum { minConst = 0, maxConst = ULONG_MAX };
890 static const unsigned long minConst = 0;
891 static const unsigned long maxConst = ULONG_MAX;
894 static Promote toPromote(
unsigned long v) {
return v; }
895 static RealPromote toRealPromote(
unsigned long v) {
return v; }
896 static unsigned long fromPromote(Promote v) {
return v; }
897 static unsigned long fromRealPromote(RealPromote v) {
900 : ((v > (RealPromote)ULONG_MAX)
902 : static_cast<unsigned long>(v + 0.5)));
908 struct NumericTraits<long long>
910 typedef long long Type;
911 typedef long long Promote;
912 typedef unsigned long long UnsignedPromote;
913 typedef double RealPromote;
914 typedef std::complex<RealPromote> ComplexPromote;
915 typedef Type ValueType;
917 typedef VigraTrueType isIntegral;
918 typedef VigraTrueType isScalar;
919 typedef VigraTrueType isSigned;
920 typedef VigraTrueType isOrdered;
921 typedef VigraFalseType isComplex;
923 static long long zero() {
return 0; }
924 static long long one() {
return 1; }
925 static long long nonZero() {
return 1; }
926 static long long min() {
return LLONG_MIN; }
927 static long long max() {
return LLONG_MAX; }
929 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
930 enum { minConst = LLONG_MIN, maxConst = LLONG_MAX };
932 static const long long minConst = LLONG_MIN;
933 static const long long maxConst = LLONG_MAX;
936 static Promote toPromote(
long long v) {
return v; }
937 static RealPromote toRealPromote(
long long v) {
return (RealPromote)v; }
938 static long long fromPromote(Promote v) {
return v; }
939 static long long fromRealPromote(RealPromote v) {
941 ? ((v < (RealPromote)LLONG_MIN)
943 : static_cast<long long>(v - 0.5))
944 : ((v > (RealPromote)LLONG_MAX)
946 : static_cast<long long>(v + 0.5)));
951 struct NumericTraits<unsigned long long>
953 typedef unsigned long long Type;
954 typedef unsigned long long Promote;
955 typedef unsigned long long UnsignedPromote;
956 typedef double RealPromote;
957 typedef std::complex<RealPromote> ComplexPromote;
958 typedef Type ValueType;
960 typedef VigraTrueType isIntegral;
961 typedef VigraTrueType isScalar;
962 typedef VigraFalseType isSigned;
963 typedef VigraTrueType isOrdered;
964 typedef VigraFalseType isComplex;
966 static unsigned long long zero() {
return 0; }
967 static unsigned long long one() {
return 1; }
968 static unsigned long long nonZero() {
return 1; }
969 static unsigned long long min() {
return 0; }
970 static unsigned long long max() {
return ULLONG_MAX; }
972 #ifdef NO_INLINE_STATIC_CONST_DEFINITION
973 enum { minConst = 0, maxConst = ULLONG_MAX };
975 static const unsigned long long minConst = 0;
976 static const unsigned long long maxConst = ULLONG_MAX;
979 static Promote toPromote(
unsigned long long v) {
return v; }
980 static RealPromote toRealPromote(
unsigned long long v) {
return (RealPromote)v; }
981 static unsigned long long fromPromote(Promote v) {
return v; }
982 static unsigned long long fromRealPromote(RealPromote v) {
985 : ((v > (RealPromote)ULLONG_MAX)
987 : static_cast<unsigned long long>(v + 0.5)));
993 struct NumericTraits<float>
996 typedef float Promote;
997 typedef float UnsignedPromote;
998 typedef float RealPromote;
999 typedef std::complex<RealPromote> ComplexPromote;
1000 typedef Type ValueType;
1002 typedef VigraFalseType isIntegral;
1003 typedef VigraTrueType isScalar;
1004 typedef VigraTrueType isSigned;
1005 typedef VigraTrueType isOrdered;
1006 typedef VigraFalseType isComplex;
1008 static float zero() {
return 0.0; }
1009 static float one() {
return 1.0; }
1010 static float nonZero() {
return 1.0; }
1011 static float epsilon() {
return FLT_EPSILON; }
1012 static float smallestPositive() {
return FLT_MIN; }
1013 static float min() {
return -FLT_MAX; }
1014 static float max() {
return FLT_MAX; }
1016 static Promote toPromote(
float v) {
return v; }
1017 static RealPromote toRealPromote(
float v) {
return v; }
1018 static float fromPromote(Promote v) {
return v; }
1019 static float fromRealPromote(RealPromote v) {
return v; }
1023 struct NumericTraits<double>
1025 typedef double Type;
1026 typedef double Promote;
1027 typedef double UnsignedPromote;
1028 typedef double RealPromote;
1029 typedef std::complex<RealPromote> ComplexPromote;
1030 typedef Type ValueType;
1032 typedef VigraFalseType isIntegral;
1033 typedef VigraTrueType isScalar;
1034 typedef VigraTrueType isSigned;
1035 typedef VigraTrueType isOrdered;
1036 typedef VigraFalseType isComplex;
1038 static double zero() {
return 0.0; }
1039 static double one() {
return 1.0; }
1040 static double nonZero() {
return 1.0; }
1041 static double epsilon() {
return DBL_EPSILON; }
1042 static double smallestPositive() {
return DBL_MIN; }
1043 static double min() {
return -DBL_MAX; }
1044 static double max() {
return DBL_MAX; }
1046 static Promote toPromote(
double v) {
return v; }
1047 static RealPromote toRealPromote(
double v) {
return v; }
1048 static double fromPromote(Promote v) {
return v; }
1049 static double fromRealPromote(RealPromote v) {
return v; }
1053 struct NumericTraits<long double>
1055 typedef long double Type;
1056 typedef long double Promote;
1057 typedef long double UnsignedPromote;
1058 typedef long double RealPromote;
1059 typedef std::complex<RealPromote> ComplexPromote;
1060 typedef Type ValueType;
1062 typedef VigraFalseType isIntegral;
1063 typedef VigraTrueType isScalar;
1064 typedef VigraTrueType isSigned;
1065 typedef VigraTrueType isOrdered;
1066 typedef VigraFalseType isComplex;
1068 static long double zero() {
return 0.0; }
1069 static long double one() {
return 1.0; }
1070 static long double nonZero() {
return 1.0; }
1071 static long double epsilon() {
return LDBL_EPSILON; }
1072 static long double smallestPositive() {
return LDBL_MIN; }
1073 static long double min() {
return -LDBL_MAX; }
1074 static long double max() {
return LDBL_MAX; }
1076 static Promote toPromote(
long double v) {
return v; }
1077 static RealPromote toRealPromote(
long double v) {
return v; }
1078 static long double fromPromote(Promote v) {
return v; }
1079 static long double fromRealPromote(RealPromote v) {
return v; }
1082 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
1085 struct NumericTraits<std::complex<T> >
1087 typedef std::complex<T> Type;
1088 typedef std::complex<typename NumericTraits<T>::Promote> Promote;
1089 typedef std::complex<typename NumericTraits<T>::UnsignedPromote> UnsignedPromote;
1090 typedef std::complex<typename NumericTraits<T>::RealPromote> RealPromote;
1091 typedef std::complex<RealPromote> ComplexPromote;
1092 typedef T ValueType;
1094 typedef VigraFalseType isIntegral;
1095 typedef VigraFalseType isScalar;
1096 typedef typename NumericTraits<T>::isSigned isSigned;
1097 typedef VigraFalseType isOrdered;
1098 typedef VigraTrueType isComplex;
1100 static Type zero() {
return Type(0.0); }
1101 static Type one() {
return Type(1.0); }
1102 static Type nonZero() {
return one(); }
1103 static Type epsilon() {
return Type(NumericTraits<T>::epsilon()); }
1104 static Type smallestPositive() {
return Type(NumericTraits<T>::smallestPositive()); }
1106 static Promote toPromote(Type
const & v) {
return v; }
1107 static Type fromPromote(Promote
const & v) {
return v; }
1108 static Type fromRealPromote(RealPromote v) {
return Type(v); }
1111 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
1120 struct SquareRootTraits
1123 typedef typename NumericTraits<T>::RealPromote SquareRootResult;
1124 typedef typename NumericTraits<T>::RealPromote SquareRootArgument;
1134 struct Error_NormTraits_not_specialized_for_this_case { };
1140 typedef Error_NormTraits_not_specialized_for_this_case SquaredNormType;
1141 typedef Error_NormTraits_not_specialized_for_this_case NormType;
1144 #define VIGRA_DEFINE_NORM_TRAITS(T) \
1145 template <> struct NormTraits<T> { \
1147 typedef NumericTraits<T>::Promote SquaredNormType; \
1148 typedef T NormType; \
1151 VIGRA_DEFINE_NORM_TRAITS(
bool)
1152 VIGRA_DEFINE_NORM_TRAITS(
signed char)
1153 VIGRA_DEFINE_NORM_TRAITS(
unsigned char)
1154 VIGRA_DEFINE_NORM_TRAITS(
short)
1155 VIGRA_DEFINE_NORM_TRAITS(
unsigned short)
1156 VIGRA_DEFINE_NORM_TRAITS(
int)
1157 VIGRA_DEFINE_NORM_TRAITS(
unsigned int)
1158 VIGRA_DEFINE_NORM_TRAITS(
long)
1159 VIGRA_DEFINE_NORM_TRAITS(
unsigned long)
1160 VIGRA_DEFINE_NORM_TRAITS(
float)
1161 VIGRA_DEFINE_NORM_TRAITS(
double)
1162 VIGRA_DEFINE_NORM_TRAITS(
long double)
1165 VIGRA_DEFINE_NORM_TRAITS(
long long)
1166 VIGRA_DEFINE_NORM_TRAITS(
unsigned long long)
1169 #undef VIGRA_DEFINE_NORM_TRAITS
1171 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
1174 struct NormTraits<std::complex<T> >
1176 typedef std::complex<T> Type;
1177 typedef typename NormTraits<T>::SquaredNormType SquaredNormType;
1178 typedef typename SquareRootTraits<SquaredNormType>::SquareRootResult NormType;
1181 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
1191 template <
class T,
class U>
1197 typedef typename SizeToType<sizeof(*typeToSize(t() + u()))>::result Promote;
1198 static Promote toPromote(T t) {
return Promote(t); }
1199 static Promote toPromote(U u) {
return Promote(u); }
1204 struct PromoteType<T, T>
1208 typedef typename SizeToType<sizeof(*typeToSize(t() + t()))>::result Promote;
1209 static Promote toPromote(T t) {
return Promote(t); }
1214 struct Error_PromoteTraits_not_specialized_for_this_case { };
1216 template<
class A,
class B>
1217 struct PromoteTraits
1219 typedef Error_PromoteTraits_not_specialized_for_this_case Promote;
1222 #include "promote_traits.hxx"
1224 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
1227 struct PromoteTraits<std::complex<T>, std::complex<T> >
1229 typedef std::complex<typename PromoteTraits<T, T>::Promote> Promote;
1230 static Promote toPromote(std::complex<T>
const & v) {
return v; }
1233 template <
class T1,
class T2>
1234 struct PromoteTraits<std::complex<T1>, std::complex<T2> >
1236 typedef std::complex<typename PromoteTraits<T1, T2>::Promote> Promote;
1237 static Promote toPromote(std::complex<T1>
const & v) {
return v; }
1238 static Promote toPromote(std::complex<T2>
const & v) {
return v; }
1241 template <
class T1,
class T2>
1242 struct PromoteTraits<std::complex<T1>, T2 >
1244 typedef std::complex<typename PromoteTraits<T1, T2>::Promote> Promote;
1245 static Promote toPromote(std::complex<T1>
const & v) {
return v; }
1246 static Promote toPromote(T2
const & v) {
return Promote(v); }
1249 template <
class T1,
class T2>
1250 struct PromoteTraits<T1, std::complex<T2> >
1252 typedef std::complex<typename PromoteTraits<T1, T2>::Promote> Promote;
1253 static Promote toPromote(T1
const & v) {
return Promote(v); }
1254 static Promote toPromote(std::complex<T2>
const & v) {
return v; }
1262 struct RequiresExplicitCast {
1264 static U
const & cast(U
const & v)
1268 #if !defined(_MSC_VER) || _MSC_VER >= 1300
1269 # define VIGRA_SPECIALIZED_CAST(type) \
1271 struct RequiresExplicitCast<type> { \
1272 static type cast(float v) \
1273 { return NumericTraits<type>::fromRealPromote(v); } \
1274 static type cast(double v) \
1275 { return NumericTraits<type>::fromRealPromote(v); } \
1276 static type cast(type v) \
1278 template <class U> \
1279 static type cast(U v) \
1280 { return static_cast<type>(v); } \
1284 # define VIGRA_SPECIALIZED_CAST(type) \
1286 struct RequiresExplicitCast<type> { \
1287 static type cast(float v) \
1288 { return NumericTraits<type>::fromRealPromote(v); } \
1289 static type cast(double v) \
1290 { return NumericTraits<type>::fromRealPromote(v); } \
1291 static type cast(signed char v) \
1293 static type cast(unsigned char v) \
1295 static type cast(short v) \
1297 static type cast(unsigned short v) \
1299 static type cast(int v) \
1301 static type cast(unsigned int v) \
1303 static type cast(long v) \
1305 static type cast(unsigned long v) \
1311 VIGRA_SPECIALIZED_CAST(
signed char)
1312 VIGRA_SPECIALIZED_CAST(
unsigned char)
1313 VIGRA_SPECIALIZED_CAST(
short)
1314 VIGRA_SPECIALIZED_CAST(
unsigned short)
1315 VIGRA_SPECIALIZED_CAST(
int)
1316 VIGRA_SPECIALIZED_CAST(
unsigned int)
1317 VIGRA_SPECIALIZED_CAST(
long)
1318 VIGRA_SPECIALIZED_CAST(
unsigned long)
1321 struct RequiresExplicitCast<
bool> {
1323 static bool cast(U v)
1324 {
return v == NumericTraits<U>::zero()
1330 struct RequiresExplicitCast<float> {
1331 static float cast(
int v)
1332 {
return (
float)v; }
1334 static float cast(
unsigned int v)
1335 {
return (
float)v; }
1337 static float cast(
long v)
1338 {
return (
float)v; }
1340 static float cast(
unsigned long v)
1341 {
return (
float)v; }
1343 static float cast(
long long v)
1344 {
return (
float)v; }
1346 static float cast(
unsigned long long v)
1347 {
return (
float)v; }
1349 static float cast(
double v)
1350 {
return (
float)v; }
1352 static float cast(
long double v)
1353 {
return (
float)v; }
1361 struct RequiresExplicitCast<double> {
1362 static double cast(
Int64 v)
1363 {
return (
double)v; }
1365 static double cast(
UInt64 v)
1366 {
return (
double)v; }
1373 #undef VIGRA_SPECIALIZED_CAST
1381 #endif // VIGRA_NUMERICTRAITS_HXX